Tags: blog http color using IO strong for AR
The swift language uses VAR to define variables, but unlike other languages, Swift does not automatically assign initial values to variables.
This means that the variable does not have a default value, so you must initialize the variable before it is required.
。 If you do not initialize before using the variable, you will get an error:[Plain]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
The reason for the error is that the variable is not initialized before using the stringvalue variable, which means that the variable does not have the memory at all.
This will make a mistake.
So we can use the optional type, followed by one? Just[Plain]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
It is mentioned in the document that the use of the optional value requires a specific operation, such as calling method, property, subscript index, etc. before adding one? , if it is a nil value (there is no value), that is, Optional.none, will skip the subsequent operation is not executed, if there is a value, that is, optional.some may be unpacking (unwrap), and then the value after the split to perform the following operation, to ensure that the operation of the security, For example Optional binding:[Plain]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
When writing a protocol (protocol), for an optional proxy method, you also need to follow the function name at the time of invocation. Such as:
Of course we can also use! To force the unpacking, which is what we would use if we were guaranteed to have a value:[Plain]View Plaincopyprint?
Implicit strong-Unpacking types:
Use! To declare a variable, it becomes an optional type of implicit strong unpacking, which means that the type never shows nil, but once it comes out,
It crashes when it is called.[Plain]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
Under what circumstances do you usually use the optional type?
(1) When we need to declare such a variable, when the variable is not initialized in the design initialization function, it is necessary to declare the variable to be the optional type. Because the variable must be used before
Declaration, and is initialized in the design initialization function. For example, the control (class member) that we initialize in the Viewdidload function needs to be declared as optional and must be a VAR declaration.
Because the constants that let declares can only be initialized in the initialization function.
(2) When we do not know whether there will be a value, this variable can be declared as optional, such as the agent, we do not require the agent to come over, then we need to declare as optional.
(3) As a function parameter, if the parameter can have no value, then use the optional type, such as the time of the proxy, is usually optional, can be set to nil
...... For the time being, anyone can continue to add!