Junior
Mis on wrapped?
sobes.tech AI
Vastus AI-lt
wrapped is a value of an optional type after successfully unwrapping its contents. An optional Optional<<Wrapped>> either holds a value of type Wrapped or is nil. Unwrapping allows access to the Wrapped value if it exists.
Examples of unwrapping:
-
Force unwrapping
!:var optionalString: String? = "Hello" let unwrappedString = optionalString! // Gets the value "Hello" // If optionalString == nil, a crash will occur -
Optional binding
if letorguard let:var anotherOptionalString: String? = "World" if let guaranteedString = anotherOptionalString { // Inside this block, guaranteedString is of type String (unwrapped) print(guaranteedString) } -
Optional chaining
?:class MyClass { var property: String? } var instance: MyClass? = MyClass() instance?.property = "Value" // If instance is not nil, the property is set let retrievedValue = instance?.property // retrievedValue will be String? -
Nil-coalescing operator
??:var yetAnotherOptionalString: String? = nil let defaultValue = yetAnotherOptionalString ?? "Default" // defaultValue will be "Default" (String) // If yetAnotherOptionalString had a value, it would be used as unwrapped.
In the context of Swift, a wrapped value is exactly what was "inside" the optional before it was unwrapped.