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

Generic Types क्या हैं

एक generic type एक custom struct, class, या enum है जिसे आप specify की गई किसी भी तरह की value रखने के लिए बना सकते हैं, एक box जैसा जो आप जो चाहें रख सकता है।
Syntax
swift
struct TypeName<T> {
    var value: T
    var items: [T] = []

    mutating func method(_ item: T) {
        items.append(item)
    }
}

let instance = TypeName<Int>(value: 1)

Defining a Generic Struct

एक generic struct एक placeholder type declare करता है जिसे इसकी stored properties और methods उपयोग कर सकते हैं, उसी struct definition को किसी भी element type के लिए काम करने देते हुए।

उदाहरण: Defining a Generic Struct

markup
// Define a struct `Box`
struct Box<T> {
    var value: T
}
// Declare a constant `intBox`, set to `Box(value: 42)`
let intBox = Box(value: 42)
// Declare a constant `stringBox`, set to `Box(value: "hello")`
let stringBox = Box(value: "hello")
// Print `intBox.value` to the console
print(intBox.value)
// Print `stringBox.value` to the console
print(stringBox.value)

A Generic Stack Type

एक ज़्यादा useful generic type, जैसे एक simple stack, methods define करता है जो इसके placeholder type पर consistently operate करते हैं।

उदाहरण: A Generic Stack Type

markup
// Define a struct `Stack`
struct Stack<Element> {
    private var items: [Element] = []
    mutating func push(_ item: Element) {
        // Append `item` to `items`
        items.append(item)
    }
    mutating func pop() -> Element? {
        // Return `items.popLast()` from this function
        return items.popLast()
    }
}
// Declare a mutable variable `stack`, initialized to `Stack<Int>()`
var stack = Stack<Int>()
stack.push(1)
stack.push(2)
// Print `stack.pop() as Any` to the console
print(stack.pop() as Any)
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. यह भूल जाना कि generic placeholder को type पर खुद declared होना चाहिए (struct Box<T>), सिर्फ इसके methods पर नहीं।
  2. एक generic type को इसका concrete type parameter specify किए (या Swift को infer करने दिए) बिना उपयोग करने की कोशिश करना।
  3. यह मान लेना कि Stack<Int> और Stack<String> जैसे generic structs interchangeable हैं; हर concrete instantiation एक distinct type है।
चैप्टर सारांश
  • एक generic type इसका placeholder type नाम के बाद angle brackets में declare करता है, जैसे struct Box<T>।
  • एक बार एक concrete type से instantiated, जैसे Box<Int>, वह placeholder उस instance के लिए fixed है।
  • Generic types आपको कई element types के लिए एक reusable container या structure लिखने देते हैं।
  • किसी generic type के अंदर methods placeholder type freely उपयोग कर सकते हैं।
🔒

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.