Swift2.0学习笔记1
Swift基本语法 元组:把多个值组合成一个复合值。元组内的值可以是任意类型,并不要求是相同类型。 可选类型:(optionals)来处理值可能缺失的情况。可选类型表示: var userDefinedColorName: String? //可选类型
隐式解析可选类型当可选类型被第一次赋值之后就可以确定之后一直有值的时候; var myButton: UIButton!
错误处理
do {
//代码块
} catch Error.OutOfCleanDishes{
//错误处理代码块
}
空合运算符(a ?? b)将对可选类型a进行空判断,如果a包含一个值就进行解封,否则就返回一个默认值b 简短表达: a != nil ? a! : b
例:
let defaultColorName = "red"
var userDefinedColorName: String? //默认值为nil
var colorNameToUser = userDefinedColorName ?? defaultColorName
区间运算符:
for index in 1...5{ // 包含1和5
print("(index)*5 = (index*5)")
}
使用场景:半开区间使用在一个从0开始的列表或者数组
let names = ["Anna","Alex","Brian","Jack"]
let count = names.count
for i in 0..<count{
print("第(i+1)个人叫(names[i])")
}
字符串
for index in greeting.characters.indices{
print("(greeting[index])",terminator:"")
}
集合类型1)数组(Arrays) 控制语句
while condition{
statements
}
repeat {
statements
} while condition
switch condition{
case value1:
staements
case value2,value3:
statements
default:
otherwise
}
switch condition{
case value1:
staements
fallthrough //关键字fallthrough,c语言的贯穿特性(Java语言的switch特性)
case value2,value3:
statements
default:
otherwise
}
label name: while condition{ statements }
func greet (person: [String: String]){
guard let name = person["name"] else{
return
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |