var ap = Array(count:3,repeatedValue: 16)
var ad = Array(count:3,repeatedValue : 15)
var ac = ap + ad
println( ac )
let emptyDic1 = [:]
let emptyDic2 = Dictionary<String,Float>()
println(airports)
/*
//add + delete
airports["china"] = "china international"
println(airports)
airports["usa"] = "ny"
println(airports)
//delete 1
airports["usa"] = nil
println(airports)
//delete 2
airports.removeValueForKey("china")
println(airports)
*/
//输出COUNT
println("airports count is "+String(airports.count))
println("airports count is \(airports.count)")
//bianli key
for key in airports.keys {
println(key)
}
//bianli value
for value in airports.values {
println(value)
}
// 把所有的key 和value 转换成数组再输出
let allkeys = Array(airports.keys)
let allvalues = Array(airports.values)
println(allkeys)
println(allvalues)
//元组
let (x,y) = (1,2)
//********
let http404error = (404,"not found")
println(http404error)
//1
let (statusCode,statusMessage) = http404error //采用类型dictionary方式 通过key拿这个value
@H_502_73@println("statusCode is \(statusCode) and statusMessage is \(statusMessage)")
//2
let (justthestatuscode,_) = http404error
println("justthestatuscode is \(justthestatuscode)")
//3
println("status code is \(http404error.0)")
println("message is \(http404error.1)")
let http200status = (statusCode: 200,desc:"ok") //左边是key 右边是value 赋给一个常量
println("the status code is\(http200status.statusCode) and the status message is \(http200status.desc)")
//可选类型 由于toint方法会失败 因此它会返回一个可选的int类型 而不同于int类型,一个可选的类型 返回的是一个int或者不存在
var serverResponseCode : Int? = 404
let possiblenumber = "123"
let convertednumber: Int? = possiblenumber.toInt()
var responsecode:Int? = 404 // 要么存在 且为404
responsecode = nil // 要么不存在 可以赋值为nil表示
//可选类型的nil和oc中的nil不一样 swift的nil不可用于非可选类型
//特定类型的值不存在 任何类型的可选类型都能赋值为nil 如果代码中的变量常量需要适配值不存在的情况,务必将他声明为可选类
//3.如果定义的【可选类型】的不提供默认值,该对象自动设为nil 但是常量变量定义需要默认值
// var strvalue = nir 不可以false错误
var strvalue:String? //default=nil
//let hashValue = strvalue?.hashValue //方法调用 ?是否响应后面的方法 存在就响应 不存在 就不响应 就不赋值给常量
println("hashvalue is \(strvalue?.hashValue)")
//解包!
let possiblestring:String? = "an optional string"
println(possiblestring!) //解包 我确定possiblestring的值确定存在
let stringValue = possiblestring!.hashValue //哈希值:hashvalue 必须解包 才能获取可选量的值
//or println(possiblestring!.hashValue)
println(stringValue)
//optional binding =上一行 好麻烦。。
if let svalue = possiblestring{
let stringvalue = svalue.hashValue
}
//隐式解包 后面用的时候不用加感叹号了!直接在定义的时候加感叹号
let assumestring : String! = "an lalalala"
println(assumestring)
println(assumestring.hashValue)
原文链接:https://www.f2er.com/swift/325824.html