isKindOfClass无法按预期工作

时间:2022-02-22 13:05:29

i'm working on a iOS5+ project (xcode 4.4.1 SDK 5.1)

我正在开发一个iOS5 +项目(xcode 4.4.1 SDK 5.1)

i have this code inside a unit test:

我在单元测试中有这个代码:

[_appDelegate application:nil didFinishLaunchingWithOptions:nil];

UITabBarController *tabBarController = (UITabBarController*)_appDelegate.window.rootViewController;

NSArray *viewControllers = [tabBarController viewControllers];

UINavigationController *nc_1 = [viewControllers objectAtIndex:0];
UIViewController *vc_1 = nc_1.topViewController;

STAssertTrue([vc_1 isKindOfClass:[ScheduleViewController class]]==YES, @"UITabBarController first tab should be a ScheduleViewController class");

If i run the test, the test fail.

如果我运行测试,测试失败。

So i check with the debugger:

所以我检查调试器:

(lldb) po [ScheduleViewController class]
(id) $1 = 0x00142b04 ScheduleViewController
(lldb) po vc_1
(UIViewController *) $2 = 0x11a32dc0 <ScheduleViewController: 0x11a32dc0>
(lldb) print (BOOL) [vc_1 isKindOfClass:(Class)[ScheduleViewController class]]
(BOOL) $4 = YES
(lldb) po [vc_1 class]
(id) $5 = 0x00142b04 ScheduleViewController
(lldb) 

In application:didFinishLaunchingWithOptions: i create a ScheduleViewController and i use it as rootController of the navigation controller. The debugger say it's correct. I don't understand what is wrong with the assert above.

在应用程序中:didFinishLaunchingWithOptions:我创建一个ScheduleViewController,我将它用作导航控制器的rootController。调试器说它是正确的。我不明白上面的断言有什么问题。

Someone have idea about this?

有人对此有所了解吗?

Update

The first implementation of thE assert was:

断言的第一个实现是:

STAssertTrue([vc_1 isKindOfClass:[ScheduleViewController class]], @"UITabBarController first tab should be a ScheduleViewController class");

The assert failed at the same way.

断言以同样的方式失败。

Update 2

As suggested in the comment i try to add this piece of code before the assert:

正如评论中所建议的那样,我尝试在断言之前添加这段代码:

BOOL vcBool = [vc_1 isKindOfClass:[ScheduleViewController class]];

With the debugger i see:

使用调试器,我看到:

(lldb) print (BOOL) [vc_1 isKindOfClass:(Class)[ScheduleViewController class]]
(BOOL) $1 = YES
(lldb) print (BOOL) vcBool
(BOOL) $2 = NO
(lldb) 

Update 3

I added this line, as suggested in the comments, before the assert:

在断言之前,我按照评论中的建议添加了这一行:

NSLog(@"vc_1=%@ class=%@", vc_1, NSStringFromClass([vc_1 class]));

From the debug console:

从调试控制台:

vc_1=<ScheduleViewController: 0x993bdb0> class=ScheduleViewController

2 个解决方案

#1


21  

I found the solution.

我找到了解决方案。

It's the inverse of the solution presented in the post linked by @vacawama in the comments. I had all *.m source of the app target in the test target too. While i was searching for a solution to the isKindOfClass problem i noticed a lot of warning on the console at the begin of the test session. The warnings was like this:

这是由@vacawama在评论中链接的帖子中提供的解决方案的反转。我也在测试目标中拥有了应用程序目标的所有* .m源代码。当我在寻找isKindOfClass问题的解决方案时,我注意到在测试会话开始时控制台上发出了很多警告。警告是这样的:

Class AClass is implemented in both /Application Support/iPhone Simulator/5.0/Applications/7FC68A9C-4F2C-4A30-85AD-87D8ABA7A275/App.app/App and /Developer/Xcode/DerivedData/App-fvbgaqbdupuoodgquxhlwbudpsin/Build/Products/Debug-iphonesimulator/App.octest/AppTests. One of the two will be used. Which one is undefined.

I removed all .m files of the application from test target.

我从测试目标中删除了应用程序的所有.m文件。

Now isKindOfClass works as expected.

现在isKindOfClass按预期工作。

Thank to all for the support.

感谢大家的支持。

#2


3  

You shouldn't directly compare BOOL values to YES. It's possible this is causing the issue with your assert. Here's a reference with background on the issue: http://mobiledevelopertips.com/objective-c/of-bool-and-yes.html

您不应该直接将BOOL值与YES进行比较。这可能会导致您的断言出现问题。以下是该问题的背景参考:http://mobiledevelopertips.com/objective-c/of-bool-and-yes.html

#1


21  

I found the solution.

我找到了解决方案。

It's the inverse of the solution presented in the post linked by @vacawama in the comments. I had all *.m source of the app target in the test target too. While i was searching for a solution to the isKindOfClass problem i noticed a lot of warning on the console at the begin of the test session. The warnings was like this:

这是由@vacawama在评论中链接的帖子中提供的解决方案的反转。我也在测试目标中拥有了应用程序目标的所有* .m源代码。当我在寻找isKindOfClass问题的解决方案时,我注意到在测试会话开始时控制台上发出了很多警告。警告是这样的:

Class AClass is implemented in both /Application Support/iPhone Simulator/5.0/Applications/7FC68A9C-4F2C-4A30-85AD-87D8ABA7A275/App.app/App and /Developer/Xcode/DerivedData/App-fvbgaqbdupuoodgquxhlwbudpsin/Build/Products/Debug-iphonesimulator/App.octest/AppTests. One of the two will be used. Which one is undefined.

I removed all .m files of the application from test target.

我从测试目标中删除了应用程序的所有.m文件。

Now isKindOfClass works as expected.

现在isKindOfClass按预期工作。

Thank to all for the support.

感谢大家的支持。

#2


3  

You shouldn't directly compare BOOL values to YES. It's possible this is causing the issue with your assert. Here's a reference with background on the issue: http://mobiledevelopertips.com/objective-c/of-bool-and-yes.html

您不应该直接将BOOL值与YES进行比较。这可能会导致您的断言出现问题。以下是该问题的背景参考:http://mobiledevelopertips.com/objective-c/of-bool-and-yes.html