元组将多个值组合为单个值。元组内的值能够是任意 类型,各元素没必要是相同的类型。元组在做为函数返 回值时尤为有用。函数
1、定义方法1spa
let http404Error= (404,"Not Found").net
println("The status codeis \(http404Error.0)")code
// prints "The status codeis 404"orm
println("The statusmessage is \(http404Error.1)")server
// prints "The status message isNot Found"ip
2、定义方法2it
let http200Status = (statusCode: 200, description: "OK")io
println("The status code is变量
\(http200Status.statusCode)")
// prints "The status codeis 200"
println("The status message is
\(http200Status.description)")
// prints "The status message isOK"
可选(Optional)类型
使用可选类型
咱们在以下状况下使用可选类型:
• 它有值但不肯定
• 没有任何值
let possibleNumber = "123" //Hello
let convertedNumber : Int? = possibleNumber.toInt()"Int?"是可选类型
if convertedNumber {
println("\(possibleNumber) has an integer value of
\(convertedNumber!)")
} else {
println("\(possibleNumber) could not be convertedtoan integer")
}
convertedNumber!是从可选类型中取值。
使用 nil
咱们能够为可选类选的变量设置 nil 值,表示没有任何 值。
var serverResponseCode: Int? = 404
// serverResponseCode contains an actual Int value of
404
serverResponseCode = nil
// serverResponseCode now containswift元组
no value
Swift交流讨论论坛论坛:http://www.cocoagame.net欢迎加入Swift技术交流群:362298485