Swift Tour随笔总结(2)_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Swift Tour随笔总结(2)

Swift Tour随笔总结(2)

 2015/1/14 9:55:29  极地渔翁.NTLD  程序员俱乐部  我要评论(0)
  • 摘要:TypeAliasestypealiasAudioSample=UInt16Booleans非boolean值不会被替代为bool,例如:leti=1ifi{//thisexamplewillnotcompile,andwillreportanerror}Tuples例如:HTTPStatusCode("404","NotFound")lethttp404Error=(404,"NotFound")//http404Errorisoftype(Int,String)AccessTuple
  • 标签:总结 随笔

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)")
上一篇: SharpMap V1.1 For Web教程系列之——前言 下一篇: 没有下一篇了!
发表评论
用户名: 匿名