Mat*_*tia 7 exc-bad-access ocunit ios automatic-ref-counting
我有一个问题,我得到了错误的访问异常,但只有在运行测试版本时(在调试版本中调用相同的方法不会导致问题出现).该项目启用了ARC,我使用Xcode 4.3在iPad 5.1模拟器上运行它:
这就是问题出现的地方:
- (void)testChangeFoodNotification {
Player* p = [[Player alloc] init];
[p addObserver:self forKeyPath:@"food" options:0 context:0]; // <-EXC_BAD_ACCESS (code=2)
p.food += 1;
STAssertTrue(_wasNotifiedOfFoodChange, nil);
}
Run Code Online (Sandbox Code Playgroud)
在addObserver:调用该方法时,似乎不应该释放任何涉及的对象,那么可能导致异常的原因是什么?
编辑:
如果不清楚但上面的代码是作为测试用例的一部分执行(使用标准的Xcode OCUnit),请道歉.此外,如果它澄清了这里的任何内容,那么来自播放器类的相关代码(还有其他的ivars和方法,但它们与被测试的属性或方法没有任何关联):
// Public interface
@interface Player : NSObject
@property (nonatomic, assign) NSInteger food;
@end
// Private interface
@interface Player() {
NSInteger _food;
}
@end
@implementation Player
@synthesize food = _food;
#pragma mark - Getters/Setters
- (void)setFood:(NSInteger)food {
[self willChangeValueForKey:@"food"];
_food = food;
[self didChangeValueForKey:@"food"];
}
Run Code Online (Sandbox Code Playgroud)
小智 21
如果您的课程确实符合键值,请确保展示该问题的课程的实施不包含在您的测试产品中.这意味着.m文件的Identity检查器的Target Membership面板应仅检查您的应用程序(而不是YourAppTests).
我在Xcode 4.3.1中遇到了同样的问题,当两个产品都包含一个实现时,我在生产和测试代码中都注册了观察者.以下日志告诉我:
类YourClass在/ Users/yourUser/Library/Application Support/iPhone Simulator/5.1/Applications // YourApp.app/YourApp和/ Users/yourUser/Library/Developer/Xcode/DerivedData/YourApp-/Build/Products /中实现调试iphonesimulator/YourAppTests.octest/YourAppTests.将使用两者之一.哪一个未定义.
| 归档时间: |
|
| 查看次数: |
2021 次 |
| 最近记录: |