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

Property Wrappers क्या हैं

एक property wrapper reusable magic है जिसे आप किसी property के ऊपर attach करते हैं ताकि हर बार इसे पढ़ने या लिखने पर अपने आप extra behavior जोड़ा जाए।
Syntax
swift
@propertyWrapper
struct WrapperName {
    var wrappedValue: Type {
        get { /* return stored value */ }
        set { /* validate, then store */ }
    }
}

struct Model {
    @WrapperName var property: Type
}

Defining a Simple Property Wrapper

एक property wrapper struct wrappedValue implement करता है, और इसके getter और setter के अंदर रखा कोई भी logic अपने आप चलता है जब भी wrapped property access हो।

उदाहरण: Defining a Simple Property Wrapper

markup
@propertyWrapper
// Define a struct `Clamped`
struct Clamped {
    private var value: Int
    private let range: ClosedRange<Int>
    init(wrappedValue: Int, _ range: ClosedRange<Int>) {
        self.range = range
        self.value = min(max(wrappedValue, range.lowerBound), range.upperBound)
    }
    var wrappedValue: Int {
        get { value }
        set { value = min(max(newValue, range.lowerBound), range.upperBound) }
    }
}
// Define a struct `Settings`
struct Settings {
    @Clamped(0...100) var volume: Int = 50
}
// Declare a mutable variable `settings`, initialized to `Settings()`
var settings = Settings()
settings.volume = 150
// Print `settings.volume` to the console
print(settings.volume)

Using a Property Wrapper on Multiple Properties

एक बार defined, एक property wrapper को अलग-अलग types में कितनी भी properties पर reuse किया जा सकता है, इसके behavior को हर जगह consistently apply करते हुए।

उदाहरण: Using a Property Wrapper on Multiple Properties

markup
@propertyWrapper
// Define a struct `Clamped`
struct Clamped {
    private var value: Int
    private let range: ClosedRange<Int>
    init(wrappedValue: Int, _ range: ClosedRange<Int>) {
        self.range = range
        self.value = min(max(wrappedValue, range.lowerBound), range.upperBound)
    }
    var wrappedValue: Int {
        get { value }
        set { value = min(max(newValue, range.lowerBound), range.upperBound) }
    }
}
// Define a struct `GameCharacter`
struct GameCharacter {
    @Clamped(0...100) var health: Int = 100
    @Clamped(0...10) var level: Int = 1
}
// Declare a mutable variable `hero`, initialized to `GameCharacter()`
var hero = GameCharacter()
hero.health = -20
hero.level = 99
// Print "Health: \(hero.health), Level: \(hero.level)" to the console
print("Health: \(hero.health), Level: \(hero.level)")
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. यह भूल जाना कि एक property wrapper type को एक wrappedValue property declare करना चाहिए; यही required contract है जिस पर @ attribute निर्भर करता है।
  2. Wrapped value (नाम से normally accessed) को wrapper instance खुद (एक leading $ से accessed, projected value कहलाता है) के साथ confuse करना।
  3. किसी computed property पर एक property wrapper apply करना; wrappers सिर्फ stored properties पर apply होते हैं।
चैप्टर सारांश
  • @propertyWrapper किसी struct या class को एक reusable property wrapper के रूप में चिह्नित करता है।
  • Wrapper को एक wrappedValue property expose करनी चाहिए जो Swift transparently उपयोग करता है।
  • किसी property के ऊपर @WrapperName apply करना access को अपने आप wrapper के through route करता है।
  • एक projectedValue ($name से accessed) ज़्यादा advanced उपयोग के लिए wrapper को खुद expose कर सकता है।
🔒

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.