我在 IOS 5.1 模拟器中运行以下代码:
Table* sparkFront =
[NSEntityDescription insertNewObjectForEntityForName:@"Table"
inManagedObjectContext:_context];
NSLog(@"%@", sparkFront);
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Table"
inManagedObjectContext:_context];
NSDictionary* dict = [entity propertiesByName];
for (NSObject* key in [dict allKeys])
{
NSLog(@"%@", key);
}
sparkFront.columnValuesAddress = 0x606;
此代码在最后一行出现 EXC_BAD_ACCESS 崩溃。Table 对象是一个核心数据对象,实现如下:
@interface Table : NSManagedObject
@property (nonatomic) int32_t columnValuesAddress;
@end
我知道 Core Data 本身并不做标量类型,但这是一个仅限 IOS 5+ 的应用程序,我的印象是取消/装箱是自动完成的。上述代码在崩溃前的输出表明我的上下文和实体是好的:
2012-07-20 22:17:52.714 otest[95147:7b03] <NSManagedObject: 0x9c87b20> (entity: Table; id: 0x9c86a80 <x-coredata:///Table/t2C0D90D5-E381-4BD0-B65D-8FC83C6D50DB2> ; data: {
columnValuesAddress = 0;
})
2012-07-20 22:17:52.716 otest[95147:7b03] columnValuesAddress
崩溃后,问题报告显示如下:
Application Specific Information:
objc_msgSend() selector name: isNSNumber__
Simulator libSystem was initialized out of order.
Thread 0 Crashed:
0 libobjc.A.dylib 0x00635098 objc_msgSend + 12
1 CoreData 0x05c2e833 _PFManagedObject_coerceValueForKeyWithDescription + 483
2 CoreData 0x05bfe3d1 _sharedIMPL_setvfk_core + 209
3 CoreData 0x05c16687 _svfk_0 + 39
4 ECMCalTests 0x0187f8f1 -[ECMCalTests testInsertRecords] + 849 (ECMCalTests.m:73)
5 CoreFoundation 0x003f74ed __invoking___ + 29
6 CoreFoundation 0x003f7407 -[NSInvocation invoke] + 167
7 SenTestingKit 0x201039c4 -[SenTestCase invokeTest] + 184
8 SenTestingKit 0x20103868 -[SenTestCase performTest:] + 183
9 SenTestingKit 0x201034a9 -[SenTest run] + 82
10 SenTestingKit 0x20106db2 -[SenTestSuite performTest:] + 106
11 SenTestingKit 0x201034a9 -[SenTest run] + 82
12 SenTestingKit 0x20106db2 -[SenTestSuite performTest:] + 106
13 SenTestingKit 0x201034a9 -[SenTest run] + 82
14 SenTestingKit 0x20105e97 +[SenTestProbe runTests:] + 174
15 CoreFoundation 0x00492d51 +[NSObject performSelector:withObject:] + 65
16 otest 0x0000231c 0x1000 + 4892
17 otest 0x000025be 0x1000 + 5566
18 otest 0x00002203 0x1000 + 4611
19 otest 0x00001f8d 0x1000 + 3981
20 otest 0x00001f31 0x1000 + 3889
我究竟做错了什么?
更新:我根据核心数据教程实现了属性的设置器/获取器。它仍然崩溃。它永远不会在设置器中遇到断点,因此它甚至在调用设置器之前就崩溃了。我是否遇到了 Apple 代码中的错误?
@interface Table : NSManagedObject
{
int32_t columnValuesAddress;
}
@property (nonatomic) int32_t columnValuesAddress;
@end
@implementation Table
- (int32_t)columnValuesAddress
{
[self willAccessValueForKey:@"columnValuesAddress"];
int32_t address = columnValuesAddress;
[self didAccessValueForKey:@"columnValuesAddress"];
return address;
}
- (void)setColumnValuesAddress:(int32_t)address
{
[self willChangeValueForKey:@"columnValuesAddress"];
columnValuesAddress = address;
[self didChangeValueForKey:@"columnValuesAddress"];
}
- (void)setNilValueForKey:(NSString *)key
{
if ([key isEqualToString:@"columnValuesAddress"])
{
self.columnValuesAddress = 0;
}
else
{
[super setNilValueForKey:key];
}
}
@end