isMemberOfClass doesn't work as expected with ocunit [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-11 11:38:57

问题


Possible Duplicate:
'isMemberOfClass' returning 'NO' when custom init

I've some trouble with the "isMemberOfClass"-Method.

I have a class, that generates and returns objects ("MyObject")

// ObjectFactory.h
...
-(MyObject*)generateMyObject;
...

// ObjectFactory.m
...
-(MyObject*)generateMyObject
{
    MyObject *obj = [[MyObject alloc]init];
    obj.name = @"Whatever";      // set properties of object
    return obj;
}
...

And there's a unittest-class, that calls the generateMyObject-selector and checks the class of the returned object:

...
ObjectFactory *factory = [[ObjectFactory alloc]init];
MyObject *obj = [factory generateMyObject];
if (![obj isMemeberOfclass:[MyObject class]])
    STFail(@"Upps, object of wrong class returned...");
else
...

I expect, that the else-part is processed...but the STFail(...) is called instead, but why?

Thx for any help! Regards, matrau

Ok, here is the original copy&pasted code:

//testcase
- (void)test001_setCostumeFirstCostume
{
    NSString *xmlString = @"<Bricks.SetCostumeBrick><costumeData reference=\"../../../../../costumeDataList/Common.CostumeData\"/><sprite reference=\"../../../../..\"/></Bricks.SetCostumeBrick>";
    NSError *error;
    NSData *xmlData = [xmlString dataUsingEncoding:NSASCIIStringEncoding];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData
                                                           options:0 error:&error];
    SetCostumeBrick *newBrick = [self.parser loadSetCostumeBrick:doc.rootElement];
    if (![newBrick isMemberOfClass:[SetCostumeBrick class]])
        STFail(@"Wrong class-member");
}

// "MyObject"
@implementation SetCostumeBrick
@synthesize indexOfCostumeInArray = _indexOfCostumeInArray;

- (void)performOnSprite:(Sprite *)sprite fromScript:(Script*)script
{
    NSLog(@"Performing: %@", self.description);

    [sprite performSelectorOnMainThread:@selector(changeCostume:) withObject:self.indexOfCostumeInArray waitUntilDone:true];
}

- (NSString*)description
{
    return [NSString stringWithFormat:@"SetCostumeBrick (CostumeIndex: %d)", self.indexOfCostumeInArray.intValue];
}

@end

// superclass of SetCostumeBrick
@implementation Brick
- (NSString*)description
{
    return @"Brick (NO SPECIFIC DESCRIPTION GIVEN! OVERRIDE THE DESCRIPTION METHOD!";
}

//abstract method (!!!)
- (void)performOnSprite:(Sprite *)sprite fromScript:(Script*)script
{
    @throw [NSException exceptionWithName:NSInternalInconsistencyException
                                   reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
                                 userInfo:nil];
}
@end

// the "factory" (a xml-parser)
- (SetCostumeBrick*)loadSetCostumeBrick:(GDataXMLElement*)gDataSetCostumeBrick
{
    SetCostumeBrick *ret = [[SetCostumeBrick alloc] init];

    NSArray *references = [gDataSetCostumeBrick elementsForName:@"costumeData"];
    GDataXMLNode *temp = [(GDataXMLElement*)[references objectAtIndex:0]attributeForName:@"reference"];
    NSString *referencePath = temp.stringValue;

    if ([referencePath length] > 2)
    {
        if([referencePath hasSuffix:@"]"]) //index found
        {
            NSString *indexString = [referencePath substringWithRange:NSMakeRange([referencePath length]-2, 1)];
            ret.indexOfCostumeInArray = [NSNumber numberWithInt:indexString.intValue-1];
        }
        else 
        {
            ret.indexOfCostumeInArray = [NSNumber numberWithInt:0];
        }
    }
    else 
    {
        ret.indexOfCostumeInArray = nil;
        @throw [NSException exceptionWithName:NSInternalInconsistencyException
                                       reason:[NSString stringWithFormat:@"Parser error! (#1)"]
                                     userInfo:nil];
    }

    NSLog(@"Index: %@, Reference: %@", ret.indexOfCostumeInArray, [references objectAtIndex:0]);

    return ret;
}

SOLUTION:

Eiko/jrturton gave me a link to the solution - thx: isMemberOfClass returns no when ViewController is instantiated from UIStoryboard

The problem was, that the classes were included in both targets (app and test bundle)

Thank you guys for your help :)


回答1:


Ok, with different class addresses per your comment, I think I can track this down to be a duplicate of this:

isMemberOfClass returns no when ViewController is instantiated from UIStoryboard

Basically, your class is included twice.




回答2:


You generally want isKindOfClass:, not isMemberOfClass. The isKindOfClass: will return YES if the receiver is a member of a subclass of the class in question, whereas isMemberOfClass: will return NO in the same case.

if ([obj isKindOfClass:[MyObject class]])

For example,

NSArray *array = [NSArray array];

Here [array isMemberOfClass:[NSArray class]] will return NO but [array isKindOfClass:[NSArray class]] will return YES.



来源:https://stackoverflow.com/questions/12712144/ismemberofclass-doesnt-work-as-expected-with-ocunit

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