Optional type * Optional type indicates that a variable can have a value or no value * C and objective-C do not have an optional type concept * Only optional types in Swift can be assigned nil* If you declare an optional constant or variable but are not assigned, they are automatically set to nil* format: optional< type > or add after type? The value of the optional type of the number is an enumeration * None No value * Some has value * Because the optional type is ubiquitous in swift, the system makes a syntactic sugar, which is added after the type? Note:* nil cannot be used for non-optional constants and variables. If you have constants or variables in your code that need to deal with missing values, declare them as the appropriate optional type. * Swift's nil is not the same as nil in objective-c. In Objective-c, nil is a pointer to a nonexistent object, so objective-c only object types can be set to nil (basic type not). In Swift, nil is not a pointer-it is a definite value used to denote a missing value. Any type of optional state can be set to nil, not just the object type.
var number:optional<int>
Number = 10
Number = Nil
var number1:int
Number1 = 10
Number1 = Nil
Recommended
var number2:double?
Number2 = 20.1
Number2 = Nil
The value of an optional type in Swift cannot be used as a value of a normal type
If you want to use the value of an optional type, you must unpack the operation
Just add it after the variable/constant! You can unpack it.
Unpack represents the telling system that there must be a value in the variable/constant
var number3:double
Number3 = 3.14
Print (NUMBER2)
Print (number2!)
Print (NUMBER3)
Let sum = number2! + Number3
forced parsing (forced unwrapping) * Init constructor,? indicates that the object * is not necessarily instantiated! means to tell the compiler there must be a value, compile to pass, if the runtime does not have a value will crash the * prompt:? And! Is all just contact with Swift's OC programmer the most painful problem, pre-development to pay more attention to the document and the use of compiler hints to solve (option + click) Note * in Swift development, try not to use forced unpacking, unsafe let URL = Nsurl (string : " http://www.520it.com " ) print (URL) // print (url!) Let URL1 = Nsurl (string : http://www.520it.com/picture/abc.png ) Print (URL1)
Optional binding (optional binding)*no need to consider whether the URL has a value, can enter {} must have a value*It can be used not only to determine whether there are values in an optional type, but also to assign values from an optional type to a constant or variable*optional bindings can be used to prompt in the IF and while statements:*in practical development, the frequency of use is very high note:* Variables in optional bindings/constants can only be used in {} after the IfifLet temp =URL {print (temp)}let value1:int? =TenLet value2:int? = -Let value3:int? = -Let value4:int? = +ifLet Temp1 =value1 {ifLet Temp2 =value2 {ifLet Temp3 =Value3 {ifLet Temp4 =value4 {Let sum= Temp1 + Temp2 + Temp3 +Temp4}} }}
Guard* The Guard statement is in Swift2introduced in. 0, it is used to provide an exit path when a condition is not met* Format: Guard expressionElse{} NOTE:* The variables in the guardconstants can be used behind guard*Guard is typically used to avoid forced unpacking, optimizing the code structure of Func test () {Let value1:int? =TenLet value2:int? = -Let value3:int? = -Let value4:int? = +Guard Let Temp1= value1Else{ return} guard Let Temp2= value2Else{ return} guard Let Temp3= Value3Else{ return} guard Let Temp4= Value4Else{ return} let sum= Temp1 + Temp2 + Temp3 +temp4 Print (sum)}test ()
implicitly resolves an optional type (implicitly unwrapped optionals)*sometimes in the program architecture, after the first assignment, you can determine that an optional type _ always has a value. In this case, it is very inefficient to judge and parse an optional value every time, because you can be sure that it always has a value*implicitly parsing an optional type does not require parsing to get an optional value each time, an implicitly resolved optional type is actually a normal type, but can be used as a non-optional type* Format: Change the optional type to!* let xmgbutton:uibutton!Note:*do not use implicit parsing of optional types if a variable may become nil after that. If you need to determine if it is nil in the lifetime of the variable, use the normal optional type let Intvalue:int?Intvalue=Tenprint (intvalue) print (Intvalue!)ifIntvalue! =Nil {print (Intvalue!)}ifLet temp =intvalue {print (temp)}var doublevalue:double!Doublevalue=Nildoublevalue=3.14print (Doublevalue)
Swift Learning-----Optional types