關(guān)鍵字說明
@discardableResult
默認情況下編譯器就是會去檢查返回參數(shù)是否有被使用,沒有的話就會給出警告。如果你不想要這個警告,可以自己手動加上 @discardableResult
associatedtype
swift中protocol不能使用<T>這種泛型,但是提供了associatedtype
關(guān)鍵字來支持泛型
static與class的區(qū)別
被class修飾的類型方法疯兼,下標,允許被子類重寫
被static修飾的類型方法贫途,下標吧彪,不允許被子類重寫
for-區(qū)間運算符用在數(shù)組上
let names = ["aaa","bbb", "ccc", "ddd"]
for name in names[0...3] {
print(name)
}
- 單側(cè)區(qū)間:讓區(qū)間朝一個方向盡可能遠
for name in names[2...] {
print(name)
}
for name in names[...2] {
print(name)
}
for name in names[..<2] {
print(name)
}
let range1 = ...5
range1.contains(7)//false
range1.contains(4)//true
range1.contains(-3)//true
區(qū)間類型
let range1: ClosedRange<Int> = 1...3
let range2: Range<Int> = 1..<3
let range3: PartialRangeThrough<Int> = ...5
- 字符、字符串也能使用區(qū)間字符串潮饱,但默認不能用在for-in中
let stringRange1 = "cc"..."ff"
stringRange1.contains("cb")
stringRange1.contains("dz")
stringRange1.contains("fg")
let stringRange2 = "a"..."f"
stringRange2.contains("d")
stringRange2.contains("h")
從\0到~囊括了所有可能要用到的ASCII字符
let characterRange: ClosedRange<Character> = "\0"..."~"
characterRange.contains("G")
帶間隔的區(qū)間值
let hours = 11
let hourInterval = 2
// tickMark的取值: 從4開始来氧,累加2,不超過11
for tickMark in stride(from: 4, through: hours, by: hourInterval) {
print(tickMark)
} //4 6 8 10
fallthrough
- 使用fallthrough可以實現(xiàn)貫串效果
var number = 1
switch number {
case 1:
print("number is 1")
fallthrough
case 2:
print("number is 2")
default:
print("number is other")
}
//number is 1
//number is 2
標簽語句
你可以在循環(huán)語句或 switch 語句前面加上標簽香拉,它由標簽名和緊隨其后的冒號(:)組成啦扬。在 break 和 continue 后面跟上標簽名可以顯式地在循環(huán)語句或 switch 語句中改變相應(yīng)的控制流。
outer: for i in 1...4 {
for k in 1...4 {
if k == 3 {
continue outer
}
if i == 3 {
break outer
}
print("i == \(i), k == \(k)")
}
}
Swift自帶的Print打印函數(shù)
public func print(_ items: Any..., separator: String = " ", terminator: String = "\n")
print(1,2,3,4) // 1 2 3 4
print(1,2,3,4, separator: "_") // 1_2_3_4
typealias
用來給類型起別名
typealias Byte = Int8
typealias Short = Int16
typealias Long = Int64
typealias Date = (year: Int, month: Int, day: Int)
func test(_ date: Date) {
print(date.0)
print(date.year)
}
test((2020, 10, 9))
typealias IntFn = (Int, Int) -> Int
func difference(v1: Int, v2: Int) -> Int {
v1 - v2
}
let fn: IntFn = difference
fn(10, 20)
func setFn(_ fn: IntFn) {}
setFn(difference)
func getFn() -> IntFn {
difference
}
- 按照Swift的標準庫的定義凫碌,Void就是空元組()
public typealias Void = ()
延遲執(zhí)行dispatch_after
OC
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ {
__strong typeof(self) pThis = weakSelf;
});
swift
DispatchQueue.main.asyncAfter(deadline: .now()+0.5, execute: {
})
變量名與關(guān)鍵字沖突
可以使用('')包裹住變量名扑毡,這樣就可以用了
/// Shared singleton instance used by all `AF.request` APIs. Cannot be modified.
public static let `default` = Session()
檢測API可用性
if #available(iOS 10, macOS 10.12, *) {
// 在 iOS 使用 iOS 10 的 API, 在 macOS 使用 macOS 10.12 的 API
} else {
// 使用先前版本的 iOS 和 macOS 的 API
}
if #available(平臺名稱 版本號, ..., *) {
APIs 可用,語句將執(zhí)行
} else {
APIs 不可用盛险,語句將不執(zhí)行
}