Optional some comprehension and summary:
optional, as a type, can either store a value or be empty (that is, nil in swift);
is actually an enumeration type that contains nil or a value, as follows;
enum optional<t>: _Reflectable, nilliteralconvertible {case None case Some (T)/ /...}
It's only two States. , which contains a value or null;
Optional effectively resolves empty null;
We can solve the problem by judging whether an object is empty, implementing an unpacking process by means of the IF let A = B (optional), and using a in the condition, and no longer unpacking;
for anyobject, It is possible to inadvertently write unsafe code, because it is not known Anyobject object type until run time,
When an object of type Anyobject calls a method, and the object does not exist (the optional object Call method is implicitly expanded), as follows;
let timesincenow = myobject.timeintervalsincenow? (); ----at this point, when the method does not exist, Timesincenow is presumed to be the corresponding type and is given a nil value;
;
for implicit unpacking: when accessed, the compiler automatically helps us to complete the insertion after the variable !
The behavior of
When I am explicitly determined in the project that the property is bound to be initialized, and when it is not empty in scope, it can be used when declaring properties! The
property mainly ui control , in general, the control is not Empty. This avoids the complex unpacking process and the judgment process;
The source of the implicit unpacking is from Baidu:
Wang Wei explained in his swift tip:
It's all a pot of history. Because all types of variables in cocoa in object-c can point to nil, some of the cocoa APIs are declared as specific types when parameters or returns are returned, but may be nil in certain cases, There is also another part of the API that will never accept or return Nil. In objective-c, these two cases are not differentiated, because sending a message to nil in OC is allowed, and the result is that nothing happens, and in the automation tool of the Cocoa API from OC to Swift's module declaration, It is not possible to determine the existence of nil, and therefore cannot determine which types should be the actual types, and which should be declared as Optional.
In this automated transformation, the simplest and most brutal response is to Optional, and then let the user judge and use the Optional Binding. Although this is the safest way, but it is a very troublesome thing for the user, I guess no one likes to use an API every time between Optional and normal type. At this point, the Optional of implicit unpacking emerged as a compromise scheme. The biggest benefit of using implicit unpacking Optional is that we can directly make property access and method calls to the APIs we can confirm. But keep in mind that implicit unpacking does not mean "this variable will not be nil, you can use it with confidence" this hint, can only say that Swift through this feature gives us a simple but dangerous way to Use.
Of course the implicit unpacking is also problematic, attempting to access an empty implicit unpacking optional, You will encounter a runtime error;
The most common problem in your project is to invoke the control proactively in an asynchronous thread (primarily a network request), which crashes when the object is destroyed and the thread delays execution, and actively determines whether the object is empty before invoking the Object.
for Optional properties and methods, You can use the chain (Optional Chaining) for secure access, as in anyobject;
Let B : Int?
If let A = b?. The use of sum () {a};-----to get rid of many unnecessary judgments and values to streamline the Code.
swift can use ??
To set a default value;
Setting the default value is convenient in the project;
When you encapsulate a method and target a different caller?? Can effectively solve the caller identity judgment Problem.
Column:
Func addnum (a:int, b:int, c:int = nil), int{if c = = Nil {return A + b} else (return A + b-c)}
and you When this method is called, the system provides an automatic two func ;--------
About OC Method invocation: an object application in OC can be null, and swift must be non-null, so it must be guaranteed to be non-null when using OC objects in swift;
Ziven to commemorate the brain cells that I died on the development road.
Optional in Swift