← Back to Swift Course | Chapter 12: Generics & Advanced Types | Lesson 4 of 7

Associated Types क्या हैं

एक associated type एक placeholder नाम जैसा है जो एक protocol "कुछ type जो मुझे चाहिए, लेकिन मैं हर conformer को तय करने दूंगा कि यह बिल्कुल क्या है" के लिए उपयोग करता है।
Syntax
swift
protocol ProtocolName {
    associatedtype Item
    mutating func add(_ item: Item)
}

struct TypeName: ProtocolName {
    mutating func add(_ item: Int) { }   // Item inferred as Int
}

Declaring a Protocol with an Associated Type

associatedtype एक protocol को एक placeholder type के संदर्भ में एक requirement describe करने देता है, जिसे हर conforming type concretely specify करता है।

उदाहरण: Declaring a Protocol with an Associated Type

markup
// Define a protocol `Container` describing required behavior
protocol Container {
    associatedtype Item
    mutating func add(_ item: Item)
    var count: Int { get }
}
// Define a struct `IntContainer`
struct IntContainer: Container {
    // Declare a mutable variable `items` (a `[Int]`), initialized to `[]`
    var items: [Int] = []
    mutating func add(_ item: Int) {
        // Append `item` to `items`
        items.append(item)
    }
    var count: Int {
        // Return `items.count` from this function
        return items.count
    }
}
// Declare a mutable variable `container`, initialized to `IntContainer()`
var container = IntContainer()
container.add(5)
container.add(10)
// Print "Count: \(container.count)" to the console
print("Count: \(container.count)")

Swift Infers the Associated Type

Swift अपने आप Item के लिए उपयोग किए concrete type को सिर्फ इस आधार पर infer करता है कि conforming type add(_:) को कैसे implement करता है, बिना एक explicit typealias की ज़रूरत के।

उदाहरण: Swift Infers the Associated Type

markup
// Define a protocol `Container` describing required behavior
protocol Container {
    associatedtype Item
    mutating func add(_ item: Item)
}
// Define a struct `StringContainer`
struct StringContainer: Container {
    // Declare a mutable variable `items` (a `[String]`), initialized to `[]`
    var items: [String] = []
    mutating func add(_ item: String) {
        // Append `item` to `items`
        items.append(item)
    }
}
// Declare a mutable variable `container`, initialized to `StringContainer()`
var container = StringContainer()
container.add("hello")
// Print `container.items` to the console
print(container.items)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक associated type (एक protocol के अंदर उपयोग होने वाला एक placeholder) को एक generic function के placeholder (एक function या type declaration में उपयोग होने वाला) के साथ confuse करना।
  2. एक associated type वाले protocol को सीधे एक plain variable के type के रूप में उपयोग करने की कोशिश करना (जैसे var x: Container); इसे generically या some/any के साथ उपयोग किया जाना चाहिए।
  3. यह भूल जाना कि Swift अक्सर concrete associated type को अपने आप infer कर सकता है इस आधार पर कि conforming type protocol की requirements कैसे implement करता है।
चैप्टर सारांश
  • associatedtype एक protocol की requirements के अंदर उपयोग होने वाला एक placeholder type declare करता है।
  • हर conforming type associated type को अपनी choice के एक concrete type से भरता है।
  • Associated types वाले protocols को some या any के बिना plain standalone variable types के रूप में उपयोग नहीं किया जा सकता।
  • Associated types protocols को generic containers और behaviors describe करने के लिए काफी flexible बनाते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.