Selector
如果方法名稱在方法所在域是唯一的,可以只是用方法的名字來作為#selector的內(nèi)容
let someMethod = #selector(callMe)
let anotherMethod = #selector(callMeWithParam)
let method = #selector(turn)
但如果再同一個(gè)作用域中存在同樣名字的兩個(gè)或多個(gè)方法 即使函數(shù)簽名不同,也無法編譯
@objc func commonFunc() {
}
@objc func commonFunc(input: Int) -> Int {
return input
}
let method = #selector(commonFunc) // 編譯錯(cuò)誤哨颂,`commonFunc` 有歧義
可以使用方法進(jìn)行強(qiáng)制轉(zhuǎn)換
let method1 = #selector(commonFunc as ()->())
let method2 = #selector(commonFunc as (Int)->Int)
實(shí)例方法的動(dòng)態(tài)調(diào)用
class MyClass {
func method(number: Int) -> Int {
return number + 1
}
}
let f = MyClass.method
let object = MyClass()
let result = f(object)(1)
只適用于實(shí)例方法 對于屬性的getter和setter是不能用類似的方法
class MyClass {
func method(number: Int) -> Int {
return number + 1
}
class func method(number: Int) -> Int {
return number
}
}
let f1 = MyClass.method
// class func method 的版本
let f2: (Int) -> Int = MyClass.method // 和 f1 相同
let f3: (MyClass) -> (Int) -> Int = MyClass.method // func method 的柯里化版
編譯標(biāo)記
// TODO: 未完成
// FIXME: 待修改
// MARK: 標(biāo)記
@UIApplicationMain
int main(int argc, char * argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil,
}
}
根據(jù)第三個(gè)參數(shù)初始化一個(gè)UIApplication或其子類的對象并開始接受事件(傳入nil,意味使用默認(rèn)的 UIApplication),最后一個(gè)參數(shù)指定了AppDelegate類作為應(yīng)用的委托,被用來接受類似didFinishLaunching或者 didEnterBackground 這樣的與應(yīng)用生命周期相關(guān)的委托方法
雖然這個(gè)方法標(biāo)明為返回一個(gè)int ,但是其實(shí)它并不會(huì)真正返回寝并。它會(huì)一直存在于內(nèi)存中,直到用戶或者系統(tǒng)將其強(qiáng)制終止
在swift中 使用的是@UIApplicationMain
如果想使用自己的UIApplication 可以刪除@UIApplicationMain 新建一個(gè)main.swift 特殊的文件
import UIKit
class MyApplication: UIApplication {
override func sendEvent(event: UIEvent!) {
super.sendEvent(event)
print("Event sent: \(event)"); }
}
UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(MyApplication), NSStringFromClass(AppDelegate))
每次發(fā)送事件 (比如點(diǎn)擊按鈕)時(shí),都可以監(jiān)聽到這個(gè)事件
@objc 和 dynamic
swift中繼承于NSObject的class會(huì)自動(dòng)為所有非private的類和成員加上@objc
可選協(xié)議和協(xié)議擴(kuò)展
OC中的protocol里存在optional關(guān)鍵字,被這個(gè)關(guān)鍵字修飾的方法并非必須要被實(shí)現(xiàn),但是,Swift中的protocol的所有方法必須都被實(shí)現(xiàn)
Swift中若想protocol中實(shí)現(xiàn)可選項(xiàng),那么就需要在協(xié)議和方法前面加上@objc,同時(shí)要在方法前加optional修飾表示可以選擇是否實(shí)現(xiàn)
對于protocol來說 如果想每個(gè)方法都是可選的,需要每個(gè)方法前面加optional
@objc protocol OptionalProtocol {
@objc optional func optionalMethod() // 可選
func necessaryMethod() // 必須
@objc optional func anotherOptionalMethod() // 可選
}
對于使用@objc修飾的protocol只能被class實(shí)現(xiàn),Struct和Enum無法實(shí)現(xiàn),無法令Struct和Enum所實(shí)現(xiàn)的協(xié)議中含有可選方法或者屬性,同時(shí),實(shí)現(xiàn)這個(gè)協(xié)議的class中的方法必須是@objc修飾 或者整個(gè)類繼承自NSObject
protocol OptionalProtocol {
func optionalMethod() // 可選
func necessaryMethod() // 必須
func anotherOptionalMethod() // 可選
}
extension OptionalProtocol {
func optionalMethod() {
print("Implemented in extension")
}
func anotherOptionalMethod() {
print("Implemented in extension")
}
}
class MyClass: OptionalProtocol {
func necessaryMethod() {
print("Implemented in Class3")
}
func optionalMethod() {
print("Implemented in Class3")
}
}
let obj = MyClass()
obj.necessaryMethod() // Implemented in Class3
obj.optionalMethod() // Implemented inClass3
obj.anotherOptionalMethod() // Implemented in extension
內(nèi)存管理,weak 和 unowned
unowned 設(shè)置以后即使它原來引用的內(nèi)容已經(jīng)被釋放了附迷,它仍然會(huì)保持對被已經(jīng)釋放了的對象的一個(gè) "無效的" 引用哎媚,它不能是 Optional 值,也不會(huì)被指向 nil 稻据。如果嘗試調(diào)用這個(gè)引用的方法或者訪問成員屬性的話买喧,程序就會(huì)崩潰。
因?yàn)閣eak引用的內(nèi)容被釋放之后,標(biāo)記為weak的成員將會(huì)自動(dòng)變?yōu)閚il,所以,被weak修飾的變量盡量是optional值
建議
當(dāng)能夠確定在訪問時(shí)不會(huì)被釋放的話 盡量使用unowned
如果存在被釋放的可能 就選擇用weak
值類型和引用類型
Swift的類型分為值類型和引用類型
- 值類型
值類型被復(fù)制的時(shí)機(jī)是值類型的內(nèi)容發(fā)生改變時(shí)
值類型在復(fù)制時(shí),會(huì)將存儲(chǔ)在其中的值類型一并進(jìn)行復(fù)制,而對于其中的引用類型的話 則只復(fù)制一份引用 - 引用類型
1.在處理大量數(shù)據(jù)并且頻繁操作(增刪)其中元素時(shí),選擇NSMutableArray和NSMutableDictionary會(huì)更好
2.對于容器條目小而容器本身數(shù)目多的情況 選擇Swift內(nèi)建的Array和Dictionary
String 還是 NSString
1.Cocoa提供了足夠多的操作String的方法
2.Swift中String是struct 通過配置常量賦值 在多線程編程時(shí)很重要.在不觸及 NSString 特有操作和動(dòng)態(tài)特性的時(shí)候今缚,使用 String 的方法姓言,在性能上也會(huì)有所提升
3.String實(shí)現(xiàn)了Collection協(xié)議 可以使用某些協(xié)議方法