isKindOfClass和NSStringFromClass不同意UIApplicationDelegate

编程入门 行业动态 更新时间:2024-10-22 18:37:09
本文介绍了isKindOfClass和NSStringFromClass不同意UIApplicationDelegate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在玩一个简单的OCUnit测试用于iPhone应用程序,并且只是想断言应用程序委托是我期望它的类的实例。我没想到这个测试非常有用,但结果却揭示了我对Objective C的误解。

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.

我首先得到了对代表的引用。然后我记录回来的类名。在我的例子中,输出正确地说app delegate的类名是CalculatorAppDelegate。

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

什么情况可能导致NSStringFromClass()返回正确的类名,而isKindOfClass返回false?

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

推荐答案

您尚未正确配置测试目标。如果您按照本指南进行单元测试应用程序您应该有3个目标:计算器, CalculatorTests 和 CalculatorTesting 。 查看 CalculatorTests 中的构建阶段部分。在编译源中,只应列出 SenTestCase 源文件。我猜你在那里添加了 CalculatorAppDelegate.m 和其他文件 - 这会导致相同源文件的重复程序集,然后在构建 CalculatorTesting 目标。这就解释了为什么你的断言失败了。

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.

编辑:刚才意识到你不需要 CalculatorTesting 目标Xcode 4.只需转到Project> Edit Schemes ...并确保单元测试包列在 Test 部分中。然后你可以用Cmd-U运行单元测试。

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.

更多推荐

isKindOfClass和NSStringFromClass不同意UIApplicationDelegate

本文发布于:2023-11-27 11:27:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不同意   isKindOfClass   NSStringFromClass   UIApplicationDelegate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!