'isMemberOfClass' returning 'NO' when custom init [duplicate]

廉价感情. 提交于 2019-12-02 18:48:10

问题


Possible Duplicate:
isMemberOfClass returns no when ViewController is instantiated from UIStoryboard

I recently stumbled over a weird problem:

I was implementing simple Testcases and using the NSObject isMemberOfClass method to check for class equality.

Additionally I implemented a custom init:

-(id)initWithMessage:(NSString *)message

If I replace id with the right class name the isMemberOfClass will return 'YES'. Otherwise it will fail.

The interesting part is: The class Method will return the right Class every time.

Is this a bug? Or is it supposed to work that way?

Thanks..

EDIT:

Ok this did not solve the problem.. Here is what I do.. isMemberOfClass will always return NO

Testcase:

- (void)test010_broadcastWait
{
    ...
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData
                                                           options:0 error:&error];
    Brick *newBrick = [self.parser loadBroadcastWaitBrick:doc.rootElement];
    if (![newBrick isMemberOfClass:[BroadcastWaitBrick class]])
        STFail(@"Wrong class-member");
    ....
}

BroadCastWait Class:

import "BroadcastWaitBrick.h"

@implementation BroadcastWaitBrick

-(id)initWithMessage:(NSString *)message
{
    self = [super init];
    if (self)
    {
        self.message = message;
    }
    return self;
}

...

loadMethod:

-(BroadcastWaitBrick*)loadBroadcastWaitBrick:(GDataXMLElement*)gDataXMLElement
{


    NSArray *messages = [gDataXMLElement elementsForName:@"broadcastMessage"];
    GDataXMLElement *message = (GDataXMLElement*)[messages objectAtIndex:0];

    BroadcastWaitBrick* brick = [[BroadcastWaitBrick alloc]initWithMessage:message.stringValue];

    return brick;
}

回答1:


isMemberOfClass in your test case returns NO because you declare newBrick var as a member of class Brick.

Better choice in this case is to use isKindOfClass method or declare newBrick as id.



来源:https://stackoverflow.com/questions/12692854/ismemberofclass-returning-no-when-custom-init

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!