Ix. swift3.0 Optional type + type modifier and others

Source: Internet
Author: User

One, can be selected


All types in Swift do not receive nil values by default, and if a data type is required to receive a nil value, the type of data needs to be wrapped into an optional type.

1. Suffix: You must force parsing before you can get the value of the wrapper

2,! Suffix: Implicit parsing can get the value of the wrapper


Second, type conversion



III. privatization in Swift

Private: Modifies properties and functions by using them only in the current class or class extension.

Fileprivate: The file is private, as long as in the same file, then the properties and functions in this file can be accessed

@objc: Used to modify the optional method in Swift, which enables functions to be invoked at the runtime of OC, and is commonly used in #selector called methods


Iv.% transcoding using URLs in Swift

Note: in swift "\" is itself an escape character that needs to be not allowed in the URL through the "\" escape//' \ ' character, so they must be URL-coded let urlstring:string? = "/volumes/hd\\ extension/sourcetreecode/zmwebproduct/main.json" let escapedstring = urlstring? Addingpercentencoding (withallowedcharacters:. urlhostallowed) Let FileUrl = Nsurl.fileurl (withpath:escap
edstring!) Write an array to disk and save it in JSON format let array: [[string:any]] = [["Clsname": "Zmhomeviewcontroller", "title": "Home", "ima Gename ": Home", "Visitorinfo": ["imagename": "", "message": "Pay attention to some people, come back here to see what surprises"]]/// JSON, array ———— > JSON serialization let data = try!  Jsonserialization.data (Withjsonobject:array, Options: [. prettyprinted]) let urlstring:string? = "/users/czm/desktop/01-test/deom.json" (data as NSData). Write (tofile:urlstring!, atomically:true) number Groups are written to disk as plist//The configuration information is saved to the sandbox (array as Nsarray). Write (tofile: "/volumes/hd\\ extension/sourcetreecode/zmwebproduct/demo . plist ", AtomicaLly:true) 


Modifiers for type members

1, Static and class modifiers

Properties that are defined in enumerations, structures, and methods that use the static property can be transformed into type properties, type methods;

Enum Season:character {case
        Spring = ' S ' case
        Summer = ' U ' case
        Fall = ' F ' case
        Winter = ' W '
        
        static var desc:string?
        static Let name = "season"
        
        static var info:string {get
            {return
                ] represents the enumeration of seasons \ (desc?? "")
            }
            
            set {
                print (assign value to info attribute: \ (newvalue))}}
    

Season.desc = "season"
Season.info = "New Season"


Properties and methods defined in a class are decorated with static or class, and can be changed into type properties, type methods

The difference between a property or a method that is decorated in a class by class or static is:

Type attributes, type methods, which are decorated with static, cannot be overridden by quilts, while class adornments can be overridden by a quilt class.

Note: When a type stores a property, it can only be decorated with static, and can be decorated with the type calculation property class and static

Lass person {
    
    //Type calculation properties can be modified with static, class,
    class var name:int {get
        {
            3
        }
        
        set {
            print ( Assign value \ (NewValue))
        }
    
    
    //Type storage property can only use static to modify
    static var nation:string!
    
    
    class Func sum (X:int, y:int)-> Int {return
        x + y
    }
    
    
    static func agave (result:double)-> Double {return
        result/3.0
    }

}




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.