Several types of variables in Swift

Source: Internet
Author: User



Several types of variables in Swift









The variables in swift are divided into optional,non-optional and implicitly unwrapped optional these types


 var nullableProperty    : AnyObject? // optional
        var nonNullProperty     : AnyObject  // non-optional
        var unannotatedProperty : AnyObject! // implicitly unwrapped optional





Among them,optional (such as anyobject? ) values can contain nil values, and ( anyobject! ) is not a nil value, it will crash when forced to expand without a value; Anyobject) must be assigned in advance to be able to use it, and no value will crash after forced expansion











The following are the correspondence between several variables and OC variables












Attached source


 
import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        var nullableProperty    : AnyObject? // optional
        var nonNullProperty     : AnyObject  // non-optional
        var unannotatedProperty : AnyObject! // implicitly unwrapped optional
        
        nullableProperty    = UIColor.redColor()
        nonNullProperty     = UIColor.redColor()
        unannotatedProperty = UIColor.redColor()
        
        print(nullableProperty)
        print(nonNullProperty)
        print(unannotatedProperty)
    }
}





Several types of variables in Swift


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.