小知識點(diǎn)

關(guān)鍵字說明

@discardableResult
默認(rèn)情況下編譯器就是會去檢查返回參數(shù)是否有被使用,沒有的話就會給出警告。如果你不想要這個警告,可以自己手動加上 @discardableResult

associatedtype
swift中protocol不能使用<T>這種泛型,但是提供了associatedtype關(guān)鍵字來支持泛型


static與class的區(qū)別

被class修飾的類型方法,下標(biāo),允許被子類重寫
被static修飾的類型方法,下標(biāo),不允許被子類重寫


for-區(qū)間運(yùn)算符用在數(shù)組上

let names = ["aaa","bbb", "ccc", "ddd"]
for name in names[0...3] {
    print(name)
}
  • 單側(cè)區(qū)間:讓區(qū)間朝一個方向盡可能遠(yuǎn)
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ū)間字符串,但默認(rèn)不能用在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可以實(shí)現(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

標(biāo)簽語句

你可以在循環(huán)語句或 switch 語句前面加上標(biāo)簽,它由標(biāo)簽名和緊隨其后的冒號(:)組成。在 break 和 continue 后面跟上標(biāo)簽名可以顯式地在循環(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的標(biāo)準(zhǔn)庫的定義,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í)行
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容