Swift Tour随笔总结(2)

Type Aliases

typealias AudioSample = UInt16

Booleans

非boolean值不会被替代为bool,例如:

let i = 1
if i {
  // this example will not compile, and will report an error
}

Tuples

例如:HTTPStatus Code ("404", "Not Found")

let http404Error = (404, "Not Found")
// http404Error is of type (Int, String)

Access Tuple:

let (statusCode, statusMessage) = heep404Error
println("This status code is \(statusCode)")
// prints "The status code is 404"
println("The statuis message is \(statusMessage)")
// prints "The status message is Not Found"

简写,使用 _ 代替不需要的变量,例如:

let (justTheStatusCode, _) = http404Error
println("The status code is \(justTheStatusCode)")
// prints "The status code is 404"

另一种access tuple的方法:

println("The status code is \(http404Error.0)")
// prints "The status code is 404"
println("The status message is \(http404Error.1)")
// prints "The status message is Not Found"

Tuple的完整define

let http200Status = (statusCode: 200, description: "OK")

对应的access

println("The status code is \(http200status.statusCode)")
println("The status code message is \(http200status.description)")

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。