笔记信息的来源大部分来自课程随堂的Keynote,以及一些我个人的看法,因为没有软件开发的基础,难免容易混淆一些基础概念,还请各位看官斧正。 这节课主要介绍了在 iOS 开发中经常使用的结构类型,呈现了一些 Swift 语言的一些基础特性,更详细的语法和特性,还是需要仔细阅读苹果官方的The Swift Programming Language来了解的。
let animals = [“Giraffe”, “Cow”, “Doggie”, “Bird”]
animals.append(“Ostrich”) // won’t compile, animals is immutable (because of let)
let animal = animals[5] // crash (array index out of bounds)
// enumerating an Array
for animal in animals {
println(“(animal)”)
}
Dictionary
字典在Swift中和数组一样,属于结构体。
1
2
3
4
5
6
7
8
9
var pac10teamRankings = Dictionary<String, Int>()
… is the same as …
var pac10teamRankings = [String:Int]()
pac10teamRankings = [”Stanford”:1, ”Cal”:10]
let ranking = pac10teamRankings[“Ohio State”] // ranking is an Int? (would be nil)
// use a tuple with for-in to enumerate a Dictionary
for (key, value) in pac10teamRankings {
println(“(key) = (value)”)
}
属性—Properties
计算型属性-Computed Properties 在结构体,类,和枚举中都可以使用计算型属性,而存储型属性不能应用在枚举中。 某个属性在 set 和 get 的时候分别返回不同类型的参数的时候可以应用,或者在 set 或 get 的时候需要对参数进行加工时可以使用。 set 方法中包含一个隐藏参数newValue, 例如,displayValue = 5, 5就是 newValue