带有 weak 的变量可能会变成 nil,所以使用 weak 变量之前有义务使用 optional type 进行检查。
optional:
```swift
funccompute(x: Int?) -> String { // This function uses optional binding to deconstruct optionals iflet y = x { return"The value is: \(y)" } else { return"No value" } }
print(compute(42)) // The value is: 42 print(compute(nil)) // No value
unowned 会假设在它的生命周期期间永远不会变成 nil,它需要在初始化时就设置好它的值。这意味着使用它的变量时不需要进行 optional type 检测。如果某个对象以某种方式将它释放了,那么 unowned reference 再次被使用时会应用程序会崩溃。
Apple docs: Use a weak reference whenever it is valid for that reference to become nil at some point during its lifetime. Conversely, use an unowned reference when you know that the reference will never be nil once it has been set during initialization.
Swift 数据类型
定义变量
var str = "Hello "
定义特定类型
1 2 3 4 5 6
var s:String = "World"
var i:Int = 100
varWords:String = "World"
说明:一般来说不需要指定类型,Swift 会自动分析类型。
字符串连接
字符串相加
1 2 3 4
var str = "Hello " str = str + "World"
println(str)
这种方式不能与其它类型直接连接
不同类型连接
1 2 3 4
var i = 200 var str = "Hello " str = "\(str),Other Str,\(100),\(i)"
数组
不同类型可以放入同一个数组
var array = ["Hello","World",100,2.3]
声明空数组
var empty = []
只能存放特定类型的数组
var arrStr =[String]()
字典
类似 C++ 的 Map
创建一个字典
var dict = ["name":"jikexueyuan" , "age" : "18"]
动态添加字段
dict["sex"] = "Female"
取字段值
println(dict["name"])
循环语句
基本循环
1 2 3 4 5 6 7 8 9 10 11
var arr =[String]()
for index in0...100{ arr.append("Item \(index)") }
println(arr)
forvar index = 0; index < 3; ++index { println("index is \(index)") }
for in
1 2 3 4 5 6
//array 是一个数组
for value in array{ println(value) }
while
1 2 3 4 5 6 7 8 9
var i = 0
//array 是一个数组
while i<arr.count { println(arr[i]) i++ }
遍历字典
1 2 3 4 5 6
var dict = ["name" : "Hello","age" : "18"]
for(key,value) in dict{ println("\(key),\(value)") }
流程控制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for index in0...100{ if index%2==0 { println(index) } }
//定义一个可选变量 var myName:String ?= "jike"
//如果可选变量为空,条件判断不执行。 iflet name = myName { println("Hello \(name)") }
This approach supports lazy initialization because Swift lazily initializes class constants (and variables), and is thread safe by the definition of let.
Class constants were introduced in Swift 1.2. If you need to support an earlier version of Swift, use the nested struct approach below or a global constant.
类中有了 static let sharedInstance = Singleton() 如果类还有实例变量,那么还需要重载 init 方法了。
Here we are using the static constant of a nested struct as a class constant. This is a workaround for the lack of static class constants in Swift 1.1 and earlier, and still works as a workaround for the lack of static constants and variables in functions.
dispatch_once
1 2 3 4 5 6 7 8 9 10 11 12 13
class Singleton { class var sharedInstance: Singleton { struct Static { static var onceToken: dispatch_once_t = 0 static var instance: Singleton? = nil } dispatch_once(&Static.onceToken) { Static.instance = Singleton() } return Static.instance! } }
The traditional Objective-C approach ported to Swift. I’m fairly certain there’s no advantage over the nested struct approach but I’m putting it here anyway as I find the differences in syntax interesting.