isMemberOfClass无法正常工作与ocunit

可能重复:
'isMemberOfClass'在自定义init时返回'NO'

我在“isMemberOfClass”方法上遇到了一些麻烦。

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

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

还有一个unittest类,它调用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(…)被调用,但为什么呢?

Thx任何帮助! 问候,matrau

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

 //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给了我一个解决scheme的链接 – thx: 当ViewController从UIStoryboard实例化时,isMemberOfClass返回no

问题是,这两个类都包含在两个目标(应用程序和testing包)

谢谢你们的帮助:)

好吧,根据你的评论,不同的课程地址,我想我可以跟踪这是一个重复的:

当ViewController从UIStoryboard实例化时,isMemberOfClass返回no

基本上,你的class级包括两次。

您通常需要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。