jeudi 14 janvier 2016

Why can certain Class objects not be passed as return values in Objective-C?

Consider the following code which tries to work with the non-NSObject class object __NSMessageBuilder:

- (Class)getTestClass {
    Class class = objc_getClass("__NSMessageBuilder");
    return class;
}

- (void)testNonNSObjectClass {

    // case 1
    Class class1 = objc_getClass("__NSMessageBuilder");
    const char *name1 = class_getName(class1);

    // case 2
    Class class2 = [self getTestClass];
    const char *name2 = class_getName(class2);

}

The first case works as expected and name1 contains the correct name of the class. However, the execution crashes on [self getTestClass] in case 2 with the following seemingly unrelated log output:

NSForwarding: warning: object 0x7fff76f18dc0 of class '__NSMessageBuilder' does not 
implement doesNotRecognizeSelector: -- abort
Message from debugger: Terminated due to signal 9

This does not make any sense to me. What is going on here, and how can I fix it? The problem is not limited to __NSMessageBuilder, this also happens with other special classes like __ARCLite__ etc.





Aucun commentaire:

Enregistrer un commentaire