A property wrapper is a compiler-synthesized mechanism that introduces a layer of separation between the code that manages how a property is stored and the code that defines the property. By applying theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
@propertyWrapper attribute to a custom type, you define a reusable encapsulation strategy for property access and mutation.
To create a property wrapper, a struct, class, or enum must be annotated with the @propertyWrapper attribute and must implement a non-static property named wrappedValue.
Compiler Desugaring
When you apply a property wrapper to a declaration, the Swift compiler automatically generates a backing storage variable and routes all access to the original property through the wrapper’swrappedValue.
private) are omitted since they do not apply to local scopes.
Initialization Semantics
Property wrappers support implicit initialization at the call site if the wrapper type defines aninit(wrappedValue:) initializer. When a default value is assigned to the wrapped property, the compiler translates this assignment into a call to this specific initializer.
Projected Values
A property wrapper can optionally expose a secondary API by defining aprojectedValue property. The compiler synthesizes access to this projected value by prefixing the original property name with a dollar sign ($).
The projectedValue can return any type, including self (the wrapper instance itself), allowing external code to interact with the wrapper’s internal state or auxiliary methods.
Architectural Constraints
- Protocols: Property wrappers cannot be applied to property requirements inside a protocol declaration (e.g.,
protocol P { @Wrapper var x: Int { get } }is invalid). - Property Modifiers: A property wrapper cannot be applied to
lazy,weak,unowned, or computed properties. - Access Control:
- Backing Storage: When applied to a type’s property, the synthesized backing storage (
_propertyName) is alwaysprivate. When applied to a local variable, access control modifiers do not apply. - Wrapped and Projected Values: The synthesized properties (
propertyNameand$propertyName) share the access level of the original property declaration. Consequently, the wrapper’s underlyingwrappedValueandprojectedValueimplementations must be at least as accessible as the wrapped property. If a property is declaredpublicbut the wrapper’sprojectedValueisinternal, the compiler does not silently downgrade the projection’s access level; instead, it emits a compile-time error.
- Backing Storage: When applied to a type’s property, the synthesized backing storage (
Master Swift with Deep Grasping Methodology!Learn More





