Objective C中動態識別的方法

2021-06-07 04:20:52 字數 1621 閱讀 7360

objective-c中動態識別的方法

- (bool) iskindofclass: classobj //是否是其子孫或一員

- (bool) ismemberofclass: classobj //是否是其一員

- (bool) respondstoselector: selector //是否有這種方法

+ (bool) instancesrespondtoselector: selector //類的物件是否有這種方法

- (id) performselector: selector // 執行物件的方法

#import "square.h"

#import "rectangle.h"

#import < stdio.h >

int main(int argc, const char * argv)

// false

if ([sq ismemberofclass: [rectangle class]] == yes)

// false

if ([sq ismemberofclass: [nsobject class]] == yes)

// iskindofclass

// true

if ([sq iskindofclass: [square class]] == yes)

// true

if ([sq iskindofclass: [rectangle class]] == yes)

// true

if ([sq iskindofclass: [nsobject class]] == yes)

// respondstoselector

// true

if ([sq respondstoselector: @selector(setsize: )] == yes)

// false

if ([sq respondstoselector: @selector(nonexistant)] == yes)

// true

if ([square respondstoselector: @selector(alloc)] == yes)

// instancesrespondtoselector

// false

if ([rectangle instancesrespondtoselector: @selector(setsize: )] == yes)

// true

if ([square instancesrespondtoselector: @selector(setsize: )] == yes)

// free memory

[rec release];[sq release];

return 0;

}

Objective C動態獲取例項屬性

本文主要圍繞乙個主題,如何動態獲取例項屬性的值?objective c動行時庫已經有這樣的功能。使用這些方法需要加標頭檔案 import 要用到的方法是 objc property t class copypropertylist class cls,unsigned int outcount 從方...

深入Objective C的動態特性

目錄 動態語言基礎 深入執行時 objective c有相當多的動態特性,基本上也是最常用的有動態型別 dynamic typing 動態繫結 dynamic binding 和動態載入 dynamic loading 這些都是在cocoa程式開發中非常常用的語言特性,在此之後oc底層也提供了相當豐...

Objective C,關於動態引數方法

ios開發中有時需要給方法傳入不定個數的引數,即動態引數繫結,編寫動態引數方法之前,有幾個c語言函式需要了解一下 va list 宣告乙個指向動態引數列表的指標 va start param,test 獲得引數位址,即讓 param 指向test va arg param,id 指向動態引數列表的下...