1.打印
print("")
2.数组contains
let arr = ["a","b","c","d","e"]
if arr.contains("a"){
}
3.获取字符串count
let str = "angelababy love"
let count = str.characters.count
4.do while改变
var count = 0
repeat{
print(count)
count++
}while count <= 10
5.guard
func todo(name:String?,age:Int?){
guard let myName = name else{
return
}
guard let myAge = age else{
return
}
print("MyName:\(myName),MyAge:\(myAge)")
}
用于简化如下swift1.2中过多的嵌套,gurad 后面表达式返回值是Bool类型
func todo(name:String?,age:Int?){
if let myName = name{
if let myAge = age{
print("MyName:\(myName),MyAge:\(myAge)")
}else{
return
}
}else{
return
}
}
6.异常处理
enum MyError: ErrorType {
case NotExist
}
func todo(name:String?) throws -> String{
guard let myName = name else{
throw MyError.NotExist
}
return myName
}
这里有两种处理
①
do{
let name = try todo("john")
print(name)
}catch{
print(error)
}
**这个结果是 **
"john"
②
let name = try? todo("john"))
print(name)
**这个结果是 **
Optional("john")