objective-c - isMemberOfClass 不能按预期使用 ocunit

标签 objective-c ios ocunit

<分区>

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

“isMemberOfClass”方法有些问题。

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

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

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

还有一个单元测试类,它调用 generateMyObject-selector 并检查返回对象的类:

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

我预计,else 部分已处理...但调用了 STFail(...),但为什么呢?

感谢您的帮助! 问候, 马特劳

好的,这是原始的复制粘贴代码:

//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 给了我解决方案的链接 - 谢谢:isMemberOfClass returns no when ViewController is instantiated from UIStoryboard

问题是,这些类包含在两个目标(应用程序和测试包)中

谢谢大家的帮助:)

最佳答案

您通常需要 isKindOfClass:,而不是 isMemberOfClass。如果接收者是相关类的子类的成员,isKindOfClass: 将返回 YES,而 isMemberOfClass: 在相同情况下将返回 NO。

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

例如,

NSArray *array = [NSArray array];

此处 [array isMemberOfClass:[NSArray class]] 将返回 NO 但 [array isKindOfClass:[NSArray class]] 将返回 YES。

关于objective-c - isMemberOfClass 不能按预期使用 ocunit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12712144/

相关文章:

ios - 使用过渡将图像和视频合并为一个视频

javascript - MultiMarkdown 到 Javascript 中的 HTML 解析器

ios - OCUnit的测试用例类中是否需要在public接口(interface)中定义测试方法

ios - 更改 Xcode 项目的单一语言

objective-c - XCode 4.2中应用测试和逻辑测试代码模板的区别

xcode - OSX Lion 上未生成代码覆盖率文件

iOS GMaps 委托(delegate) mapView : markerInfoWindow: not implemented

objective-c - Xcode 4.4 无法重命名类/变量

ios - 当您同时触摸两个按钮时会发生什么

ios - 如何在 Swift 项目中使用 iOS SDK 插入 firebase 服务器时间戳