import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//demo44444(name: "老王", age: 10)
demo4(num: "9")
demo5()
}
func demo() {
let name: String? = "老王"
// ?? 如果有数值使用默认数值 如果没有值 使用 ?? 后面的 + 为拼接字符串
print((name ?? "")+"你好")
//??优先级低
print(name ?? "" + "你好")
}
func demo2() {
let name: String? = "老王"
// ?? 如果有数值使用默认数值 如果没有值 使用 ?? 后面的 + 为拼接字符串
print((name ?? "")+"你好")
//??优先级低
print(name ?? "" + "你好")
}
// 判断非空值
func demo44444(name:String? , age:Int?) {
// let oName: String? = "老王"
// let oAge: Int? = 10
if let name = name ,
let age = age {
print(name+String(age))
}
print("----------")
guard let name = name
, let age = age else {
print("姓名 或者年龄 为 nil")
return;
}
print(name+String(age))
}
// 同一条件满足用,隔开
func demo4(num : String) {
switch num {
case "10" ,"9":
print("优秀")
default:
print("一般")
}
}
func demo5() {
// [0,5)
for i in 0..<5 {
print(i)
}
print("------")
// [0,5]
for i in 0...5 {
print(i)
}
print("------")
for i in 1..<5.revesed {
print(i)
}
}
}
网友评论