var name: String? = "CSDN"
name = nil
var age: Int? //默认就是nil
age = 30
age = nil
var array = [2, 4, 6, 8]
func getNumber(_ index: Int) -> Int? {
if index < 0 || index >= array.count {
return nil
}
return array[index]
}
print(getNumber(1)) //Optional(4)
print(getNumber(-1)) //nil
print(getNumber(4)) //nil
var age: Int? //默认就是nil
age = 10
age = nil
var age: Int? = 20
let ageInt: Int = age!
print(ageInt) //20
var age: Int?
age!
Fatal error: Unexpectedly found nil while unwrapping an Optional value
let number = Int("123")
if number != nil {
print("字符串转成数字成功为:\(number!)")
}else{
print("字符串转成数字失败")
}
// 字符串转成数字成功为:123
if let number = Int("123") {
print("字符串转成数字成功为:\(number)")
// number 是强制解包之后的Int值
// number 作用域仅限于这个大括号
}else{
print("字符串转成数字失败")
}
// 字符串转成数字成功为:123
enum Season : Int {
case spring = 1, summer, autumn, winter
}
if let season = Season(rawValue: 8){
switch season {
case .spring:
print("the season is spring")
default:
print("the season is other")
}
} else {
print("no such season")
}
// no such season
if let first = Int("5") {
if let second = Int("45") {
if first < second && second < 50 {
print("\(first) < \(second) < 50")
}
}
}
// 5 < 45 < 50
if let first = Int("5"),
let second = Int("45"),
first < second && second < 50 {
print("\(first) < \(second) < 50")
}
// 5 < 45 < 50
//遍历数组,将遇到 的正整数都加起来,如果遇到负数或者非数字,则停止遍历
var array = ["10", "20", "30", "ab", "-20", "40"]
var index = 0
var sum = 0
while let num = Int(array[index]), num > 0 {
sum += num
index += 1
}
print(sum)
public func ?? <T>(optional: T?, defaultValue: @autoclosure () throws -> T?) rethrows -> T?
public func ?? <T>(optional: T?, defaultValue: @autoclosure () throws -> T) rethrows -> T
let a: Int? = 1
let b: Int? = 2
let c = a ?? b // c是Int? , Optional(1)
let a: Int? = nil
let b: Int? = 2
let c = a ?? b // c是Int? , Optional(2)
let a: Int? = nil
let b: Int? = nil
let c = a ?? b // c是Int? , nil
let a: Int? = 1
let b: Int = 2
let c = a ?? b // c是Int , 1
let a: Int? = nil
let b: Int = 2
let c = a ?? b // c是Int , 2
let a: Int? = nil
let b: Int = 2
//如果不使用 ?? 运算符
let c: Int
if let tmp = a {
c = tmp
} else {
c = b
}
let a: Int? = 1
let b: Int? = 2
let c = a ?? b ?? 3 // c是Int , 1
let a: Int? = nil
let b: Int? = 2
let c = a ?? b ?? 3 // c是Int , 2
let a: Int? = nil
let b: Int? = nil
let c = a ?? b ?? 3 // c是Int , 3
let a: Int? = nil
let b: Int? = 2
if let c = a ?? b {
print(c)
}
//类似于 if a != nil || b != nil
let a: Int? = nil
let b: Int? = 2
if let c = a , let d = b {
print(c)
print(d)
}
// 类似于 if a != nil && b != nil
func login(_ info: [String : String]) {
let username: String
if let tmp = info["username"] {
username = tmp
} else {
print("请输入用户名")
return
}
let password: String
if let tmp = info["password"] {
password = tmp
} else {
print("请输入密码")
return
}
// if username ....
// if password ....
print("用户名:\(username)", "密码:\(password)", "登陆ing")
}
login(["username" : "jack", "password" : "123456"]) // 用户名:jack 密码:123456 登陆ing
login(["password" : "123456"]) // 请输入密码
login(["username" : "jack"]) // 请输入用户名
guard 条件 else {
// do something....
退出当前作用域
//return、break、continue、throw error
}
func login(_ info: [String : String]){
guard let username = info["username"] else {
print("请输入用户名")
return
}
guard let password = info["password"] else {
print("请输入密码")
return
}
// if username ...
// if password ...
print("用户名:\(username)","密码:\(password)","登录ing")
}
login(["username" : "jack", "password" : "123456"]) // 用户名:jack 密码:123456 登陆ing
login(["password" : "123456"]) // 请输入密码
login(["username" : "jack"]) // 请输入用户名
let num1: Int! = 10
let num2: Int = num1
if num1 != nil {
print(num1 + 6) // 16
}
if let num3 = num1 {
print(num3) // 10
}
let num1: Int! = nil
//Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
let num2: Int = num1
var age: Int? = 10
print("My age is \(age)")
print("My age is \(age!)")
//My age is 10
print("My age is \(String(describing: age))")
//My age is Optional(10)
print("My age is \(age ?? 0)")
//My age is 10
var num1: Int? = 10
var num2: Int?? = num1
var num3: Int?? = 10
print(num2 == num3) // true
var num1: Int? = nil
var num2: Int?? = num1
var num3: Int?? = nil
print(num2 == num3) // false
(num2 ?? 1) ?? 2 // 2
(num3 ?? 1) ?? 2 // 1