JobPlus知识库 互联网 互联网+ 文章
Swift基础知识

Swift是类型安全的语言;

Swift introduces optional types, which handle the absence of a value. Optional say either "there is a value, and it equals x" or "there isn't a value at all".


类型注解

var welcomeMessage: Stringvar red, green, double: Double

类型别名(Type Alias)

typealias AudioSample = UInt16var maxAmplitudeFound = AudioSample.min

元组(Tuples):元组内的值可以是不同类型的任何值。

let http404Error = (404, "Not Found")//http404Error的类型是(Int, String)

分解(decompose)元组:可以使用下划线来忽略一些值

let (statusCode, _) = http404Error

print("The status code is (status)")

也可以使用下标来取出单个值:

 print("The status code is (http404Error.0)") // Prints "The status code is 404"

 print("The status message is (http404Error.1)") // Prints "The status message is Not Found"

可以在定义元组的时候,给每个元素命名:

let http200Status = (statusCode: 200, des cription: "OK")

这个时候,可以这样获取每个元素的值:

print("The status code is (http200Status.statusCode)")

Optional type

let possibleNumber = "123"let convertedNumber = Int(possibleNumber)//convertedNumber的类型是 Int?, 不是Int

Swift的nil跟oc中的nil不同。In Objective-C, nil is a pointer to a nonexistent object. In Swift, nil is not a pointer -- it's the absence of a value of certain type. Optionals of any type can be set to nil, not just object types.

forced unwrapping

optional binding

implicity unwrapped optionals


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
439人赞 举报
分享到
用户评价(0)

暂无评价,你也可以发布评价哦:)

扫码APP

扫描使用APP

扫码使用

扫描使用小程序