Swift Learning 21:? And! Detailed

Source: Internet
Author: User

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" >
    1. var stringvalue:string
    2. Error:variable ' stringvalue ' used before being initialized
    3. Let HashValue = Stringvalue.hashvalue
    4. //                            ^
    5. Let HashValue = Stringvalue.hashvalue


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" >
    1. This is optional, strvalue automatically gets the default value: Nil
    2. This nil is different from the nil in objective-c, not a pointer, but a value that does not exist.
    3. var strvalue:string?
    4. Determine if the optional has a value
    5. If strvalue {
    6. Do-what-need to does here
    7. }

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" >
    1. Optional binding
    2. if strvalue = = Nil, then the result is nil, does not call the string HasValue
    3. If strvalue! = nil, it returns strvalue corresponding HashValue value and assigns a value to the constant HashValue
    4. If let HashValue = strvalue?. HashValue {
    5. Do something if neccessary
    6. }


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:

[Plain]View Plaincopyprint?
  1. @objc is used to process the conversion between Swift and OC, since @optional is a keyword in OC,
  2. So you need to add the @objc before protocol.
  3. @objc protocol Httprequestdelegate {
  4. @optional shows that this proxy method is optional,
  5. So when calling, you need to call this: delegate?. Requestfinished? (Self, downloaddata)
  6. Where delegate? is because delegate is also optional.
  7. @optional func requestfinished (request:httprequest!, downloaddata:nsmutabledata!)
  8. Other Funcs ...
  9. }
  10. var delegate:httprequestdelegate?
  11. var downloaddata = Nsmutabledata ()
  12. Delegate.requestfinished (self, downloaddata)

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?
    1. var strvalue:string?
    2. strvalue = "1234"
    3. Let integer = strvalue!. ToInt ()
    4. The more secure notation is
    5. If strvalue {
    6. Let integer = strvalue!. ToInt ()


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" >
    1. Implicitly unwrapped Optionals
    2. If you declare it this way, you don't need to use it when you call it! To illustrate.
    3. var mylabel:uilabel!
    4. MyLabel = UILabel (Frame:cgrectmake (10, 100, 300, 10))
    5. MyLabel.Text = "Label"



Summarize:

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.