What happens during wrapping and deployment in Swift?

When a var marked as optional Swift, it wraps it, and when the actual value is required, the expansion is performed.

 var anOptional : String? = "wrapping" print("\(anOptional!) unwrapping") 

What actually happens during the wrapping and unrolling of the optional?

0
source share
2 answers

Optional is an enumeration with two possible cases, .None and .Some . The .Some case has an associated value, which is a wrapped value. For an "expand", it is not necessary to return this bound value. It is as if you did this:

 let anOptional : String? = "wrapping" switch anOptional { case .Some(let theString): println(theString) // wrapping case .None: println("it nil") } 
+4
source

Not necessarily a simple variable, like others, but the fact is that it can have two values, or the value in the optional variable can be nil or "some value". For instance:

 var anOptional : String? println(anOptional) //nil println(anOptional!) //error as optional has no value and we are trying to wrap it and getting the value anOptional = "it has some value"; println(anOptional!) //"it has some value" as it has value and we are wrapping it 

Here is the link for the deployment process http://appventure.me/2014/06/13/swift-optionals-made-simple/

+1
source

Source: https://habr.com/ru/post/1209243/


All Articles