Swift学习(一)
2015-10-02 20:52
399 查看
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>()
var airports = ["china":"shandong","jinan":"qingdao"]
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)
println("x is \(x) and y is
\(y)")
//********
let http404error = (404,"not found")
println(http404error)
//1
let (statusCode,statusMessage) = http404error
//采用类型dictionary方式
通过key拿这个value
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)
16)
var ad = Array(count:3 , repeatedValue :
15)
var ac = ap + ad
println( ac )
let emptyDic1 = [:]
let emptyDic2 = Dictionary<String,Float>()
var airports = ["china":"shandong","jinan":"qingdao"]
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)
println("x is \(x) and y is
\(y)")
//********
let http404error = (404,"not found")
println(http404error)
//1
let (statusCode,statusMessage) = http404error
//采用类型dictionary方式
通过key拿这个value
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)
相关文章推荐
- Swift中的数据类型
- 7.Swift翻译教程系列——控制循环的流程
- swift基础学习篇发布时间调整
- swift 中switch 的新用法
- swift实现ios类似微信输入框跟随键盘弹出的效果
- swift的类和对象
- swift的类与对象讲解
- swift的函数与闭包
- swift语法讲解及简单入门教程
- swift闭包
- swift函数
- Swift学习笔记7——闭包(Closures)
- swift元组
- Swift学习笔记6——函数(Function)
- Swift 语言的相关数据
- Swift语法特点
- swift学习
- swift详解之二十九------谈谈debug的一些高级用法lldb
- 我的IOS成长之路——by Swift
- 初步swift语言学习笔记9(OC与Swift杂)