Dynamically invoke a class method in Objective C

后端 未结 4 1851
感情败类
感情败类 2020-12-08 04:09

Suppose I have Objective C interface SomeClass which has a class method called someMethod:

@interface SomeClass : NSObject {
}

         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 04:45

    In Objective-C, classes are objects as well. The class objects are treated differently, however, as they can call the instance methods of their root class (NSObject or NSProxy in Cocoa).

    So it's possible to use all the instance methods defined in NSObject on class objects as well and the right way to dynamically invoke a class method is:

    [aClass performSelector:@selector(aSelector)];
    

    The apple docs are a bit more specific.

提交回复
热议问题