isMemberOfClass不能与ocunit一起使用 [英] isMemberOfClass doesn't work as expected with ocunit

查看:137
本文介绍了isMemberOfClass不能与ocunit一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
'isMemberOfClass'在自定义初始化时返回'NO'

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

我在使用"isMemberOfClass"方法时遇到了麻烦.

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

我有一个生成并返回对象的类("MyObject")

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;
}
...

有一个unittest-class,它调用generateMyObject -selector并检查返回对象的类:

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
...

我希望对else部分进行处理...但是会调用STFail(...),但是为什么呢?

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;
}

解决方案:

Eiko/jrturton给了我解决方案的链接-thx:

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)

谢谢你们的帮助:)

推荐答案

好的,每个注释的类地址不同,我想我可以将其跟踪为以下内容的重复项:

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

从UIStoryboard实例化ViewController时,isMemberOfClass返回否

基本上,您的课程包含两次.

Basically, your class is included twice.

这篇关于isMemberOfClass不能与ocunit一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆