isKindOfClass and NSStringFromClass disagree about UIApplicationDelegate

笑着哭i 提交于 2020-01-09 19:26:08

问题


I was playing with a simple OCUnit test for an iPhone app, and just wanted to assert that the app delegate was an instance of the class that I expected it to be. I didn't expect this test to be very useful, but it turned out to reveal a misunderstanding that I have regarding Objective C.

I first get a reference to the delegate. Then I log the class name of what comes back. In my case, the output correctly says "app delegate's class name is CalculatorAppDelegate".

However, the assertion on the next line fails, and I don't understand why.

- (void)testAppDelegate 
{
    id appDelegate = [[UIApplication sharedApplication] delegate];
    NSLog(@"app delegate's class name is %@", NSStringFromClass([appDelegate class]));
    NSLog(@"is it kind? %i", [appDelegate isKindOfClass:[CalculatorAppDelegate class]]);
    NSLog(@"is it member? %i", [appDelegate isMemberOfClass:[CalculatorAppDelegate class]]);
    NSLog(@"class == class %i", [appDelegate class] == [CalculatorAppDelegate class]);
    STAssertTrue([appDelegate isKindOfClass:[CalculatorAppDelegate class]], @"wtf");
}

What circumstances could cause NSStringFromClass() to return the correct class name, while isKindOfClass returns false?

2011-03-19 15:51:13.864 Calculator[40092:207] app delegate's class name is CalculatorAppDelegate
2011-03-19 15:51:13.864 Calculator[40092:207] is it kind? 0
2011-03-19 15:51:13.865 Calculator[40092:207] is it member? 0
2011-03-19 15:51:13.865 Calculator[40092:207] class == class 0
/Users/pohl/Developer/FoundationCalculator/CalculatorTests/CalculatorBrainTests.m:37: error: -[CalculatorBrainTests testAppDelegate] : "[appDelegate isKindOfClass:[CalculatorAppDelegate class]]" should be true. wtf
Test Case '-[CalculatorBrainTests testAppDelegate]' failed (0.002 seconds).

回答1:


You haven't configured your testing target correctly. If you followed this guide for unit testing applications you should have 3 targets: Calculator, CalculatorTests and CalculatorTesting. Check the 'build phases' section in CalculatorTests. In 'Compile Sources' only the SenTestCase source files should be listed there. I guess you added the CalculatorAppDelegate.m and other files there - this would lead to duplicate assemblies of the same source files which are then linked to the same application when you build the CalculatorTesting target. That explains why your assertions fail.

EDIT: Just realized that you don't need the CalculatorTesting target in Xcode 4. Just go to Project > Edit Schemes... and make sure the unit test bundle is listed in the Test section. Then you can run unit tests with Cmd-U.




回答2:


try log out whats the return value of the operation

NSLog(@"is it kind? %i", [appDelegate isKindOfClass:[CalculatorAppDelegate class]]);

and you can even test if its a member:

NSLog(@"is it member? %i", [appDelegate isMemberOfClass:[CalculatorAppDelegate class]]);

not sure if it will help but its a starting point



来源:https://stackoverflow.com/questions/5364074/iskindofclass-and-nsstringfromclass-disagree-about-uiapplicationdelegate

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