日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS Sharing #02 | 2019-03-30

發(fā)布時間:2025/3/18 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS Sharing #02 | 2019-03-30 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

1、類方法去哪里找?

2、isa指針有幾種類型么?

3、分類的方法具體是在什么時候添加到類的方法列表中?

4、class_addMethod()都需要什么參數(shù)?

5、iOS消息轉(zhuǎn)發(fā)流程


1、類方法去哪里找?

答: 見上一期《iOS Sharing #01 | 2019-03-23》第5問

5、實例方法去哪里找?


2、isa指針有幾種類型么?

答: isa指針分,指針類型和非指針類型,32位只做地址保存,非嵌入式64位架構(gòu)下,包含除類地址外的其他信息。


3、分類的方法具體是在什么時候添加到類的方法列表中?

答: 類在編譯后會以 class_ro_t 的結(jié)構(gòu)把類的信息存儲在 bits 里,運行時的 realizeClass 之后,會把 ro 中的所有信息拷貝到 bits 的 data 內(nèi),即以 class_rw_t 的形式存在,分類里的方法即在這個時候添加到類的方法表里,并在方法表數(shù)組的最前面

4、class_addMethod()都需要什么參數(shù)?

答: /** * Adds a new method to a class with a given name and implementation. * * @param cls The class to which to add a method. * @param name A selector that specifies the name of the method being added. * @param imp A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd. * @param types An array of characters that describe the types of the arguments to the method. * * @return YES if the method was added successfully, otherwise NO * (for example, the class already contains a method implementation with that name). * * @note class_addMethod will add an override of a superclass's implementation, * but will not replace an existing implementation in this class. * To change an existing implementation, use method_setImplementation. */ OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);復(fù)制代碼
  • 給類添加一個新的方法和該方法的具體實現(xiàn)
  • BOOL: 返回值,YES -------方法添加成功 NO --------方法添加失敗
  • Class cls: 將要給添加方法的類,傳的類型 [類名 class]
  • SEL name: 將要添加的方法名,傳的類型 @selector(方法名)
  • IMP imp:實現(xiàn)這個方法的函數(shù) ,傳的類型
    • 1、C語言寫法:(IMP)方法名
    • 2、OC的寫法:class_getMethodImplementation(self,@selector(方法名:))
  • const char *types:表示我們要添加的方法的返回值和參數(shù)
  • "v@:@":
    • 'v'是添加方法無返回值
    • '@'表示是id(也就是要添加的類)
    • ':'表示添加的方法類型
    • '@'表示參數(shù)類型

const char *types含義表:

CodeMeaning
cA char
iAn int
sA short
lA long l is treated as a 32-bit quantity on 64-bit programs.
qA long long
CAn unsigned char
IAn unsigned int
SAn unsigned short
LAn unsigned long
QAn unsigned long long
fA float
dA double
BA C++ bool or a C99 _Bool
vA void
*A character string (char *)
@An object (whether statically typed or typed id)
#A class object (Class)
:A method selector (SEL)
[array type]An array
{name=type...}A structure
(name=type...)A union
bnumA bit field of num bits
^typeA pointer to type
?An unknown type (among other things, this code is used for function pointers)

注意:
用這個方法添加的方法是無法直接調(diào)用的,必須用performSelector:調(diào)用。 因為performSelector是運行時系統(tǒng)負責(zé)去找方法的,在編譯時候不做任何校驗;如果直接調(diào)用編譯是會自動校驗。 添加方法是在運行時添加的,你在編譯的時候還沒有這個本類方法,所以當(dāng)然不行。


5、iOS消息轉(zhuǎn)發(fā)流程

答:
消息轉(zhuǎn)發(fā)機制基本分為三個步驟:
  • 1、動態(tài)方法解析
  • 2、備用接受者
  • 3、完整轉(zhuǎn)發(fā)

代碼

類方法:

實例方法:

詳細流程:

感謝大佬提供的圖片。


倉庫

本篇相關(guān)代碼


聯(lián)系方式

郵箱: adrenine@163.com

  • 掘金 - Adrenine
  • 簡書 - Adrenine
  • Blog - Adrenine
  • Github - Adrenine

郵箱: holaux@gmail.com

  • 掘金 - oneofai
  • Blog - oneofai
  • Github - oneofai

郵箱: ledahapple@icloud.com

  • Github - ledah217
  • Notion - 217

總結(jié)

以上是生活随笔為你收集整理的iOS Sharing #02 | 2019-03-30的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。