版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.07.26 |
前言
OC是運行時的語言竿秆,底層就是運行時,可以說runtime是OC的底層稿壁,很多事情也都可以用運行時解決幽钢,下面就講述一下運行時runtime的知識以及它的妙用。
runtime 基本
下面就說一下runtime
的基本知識常摧。Runtime
是一套底層的C
語言API
(包含強大的C語言數(shù)據(jù)類型和函數(shù))搅吁,OC
代碼都是基于Runtime
實現(xiàn)的,即編寫的OC代碼最終都會轉(zhuǎn)成Runtime
的代碼落午。
下面就是蘋果的runtime開發(fā)者文檔谎懦。
1. runtime作用
- 獲取類的私有變量。
- 動態(tài)產(chǎn)生類溃斋,成員變量和方法界拦。
- 動態(tài)修改類,成員變量和方法梗劫。
- 對換兩個方法的實現(xiàn)(
swizzle
)
runtime基本架構(gòu)
runtime
底層很多C
語言函數(shù)享甸,下面我們就介紹下。
1. 職能任務(wù)
使用類
- class_getName
- class_getSuperclass
- class_isMetaClass
- class_getInstanceSize
- class_getInstanceVariable
- class_getClassVariable
- class_addIvar
- class_copyIvarList
- class_getIvarLayout
- class_setIvarLayout
- class_getWeakIvarLayout
- class_setWeakIvarLayout
- class_getProperty
- class_copyPropertyList
- class_addMethod
- class_getInstanceMethod
- class_getClassMethod
- class_copyMethodList
- class_replaceMethod
- class_getMethodImplementation
- class_getMethodImplementation_stret
- class_respondsToSelector
- class_addProtocol
- class_addProperty
- class_replaceProperty
- class_conformsToProtocol
- class_copyProtocolList
- class_getVersion
- class_setVersion
- objc_getFutureClass
- objc_setFutureClass
- class_setSuperclass已過時的OS X V10.5
添加類
類的實例化
用工作實例
- object_copy
- object_dispose
- object_setInstanceVariable
- object_getInstanceVariable
- object_getIndexedIvars
- object_getIvar
- object_setIvar
- object_getClassName
- object_getClass
- object_setClass
獲取類定義
- objc_getClassList
- objc_copyClassList
- objc_lookUpClass
- objc_getClass
- objc_getRequiredClass
- objc_getMetaClass
使用實例變量工作
聯(lián)想?yún)⒖?/strong>
發(fā)送消息
當(dāng)它遇到一個方法調(diào)用時梳侨,編譯器可能會生成調(diào)用任意幾個函數(shù)來執(zhí)行實際的消息調(diào)度蛉威,根據(jù)不同的接收器,返回值和參數(shù)走哺。您可以使用這些功能來動態(tài)地從你自己的純C代碼中調(diào)用方法蚯嫌,或者使用不NSObject
的公司允許說法的形式進行。
-
objc_msgSend
發(fā)送一個消息丙躏,一個簡單的返回值的類的實例择示。 -
objc_msgSend_stret
發(fā)送與數(shù)據(jù)結(jié)構(gòu)返回值的消息,一個類的實例晒旅。 -
objc_msgSendSuper
發(fā)送一個消息栅盲,一個簡單的返回值的類的實例的超類。 -
objc_msgSendSuper_stret
發(fā)送消息與數(shù)據(jù)結(jié)構(gòu)返回值的類的實例的超類废恋。 - objc_msgSend
- objc_msgSend_fpret
- objc_msgSend_stret
- objc_msgSendSuper
- objc_msgSendSuper_stret
與工作方法
- method_invoke
- method_invoke_stret
- method_getName
- method_getImplementation
- method_getTypeEncoding
- method_copyReturnType
- method_copyArgumentType
- method_getReturnType
- method_getNumberOfArguments
- method_getArgumentType
- method_getDescription
- method_setImplementation
- method_exchangeImplementations
使用庫
與選擇器的工作
與合作協(xié)議
- objc_getProtocol
- objc_copyProtocolList
- objc_allocateProtocol
- objc_registerProtocol
- protocol_addMethodDescription
- protocol_addProtocol
- protocol_addProperty
- protocol_getName
- protocol_isEqual
- protocol_copyMethodDescriptionList
- protocol_getMethodDescription
- protocol_copyPropertyList
- protocol_getProperty
- protocol_copyProtocolList
- protocol_conformsToProtocol
與工作性質(zhì)
使用Objective-C語言特征
- objc_enumerationMutation
- objc_setEnumerationMutationHandler
- imp_implementationWithBlock
- imp_getBlock
- imp_removeBlock
- objc_loadWeak
- objc_storeWeak
參考資料
后記
未完谈秫,待續(xù)~~~
美景