0

在这行代码中,我在运行时抛出了一个错误:

[numberFormatter setCurrencySymbol:[theObject valueForKey:kFieldCurrency]];

kFieldCurrency 是一个常量,定义如下:

#define kValueCurrency          @"currency"

打印 theObject 的值会提供以下输出: 打印 theObject 的描述:

<NSManagedObject: 0x7836bf0> (entity: CustomValue; id: 0x78373d0 <x-coredata://84240925-2D7D-485E-AD9D-8DD48F602C00/CustomValue/p5> ; data: {
    contract = "0x78e4a20 <x-coredata://84240925-2D7D-485E-AD9D-8DD48F602C00/Contract/p3>";
    currency = "0x7839810 <x-coredata://84240925-2D7D-485E-AD9D-8DD48F602C00/Currency/p5>";
    dateTimeValue = nil;
    isChangeableByUser = 1;
    isListed = 1;
    listName = GELDWERT;
    numValue = 10;
    numberOfDigits = 0;
    stringValue = nil;
    tagName = GELDWERT;
    type = 5;
})

错误信息是:

-[NSManagedObject copyWithZone:]: unrecognized selector sent to instance 0x7c1c930

和:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject copyWithZone:]: unrecognized selector sent to instance 0x7c1c930'
*** First throw call stack:
(0x1959012 0x1690e7e 0x19e44bd 0x1948bbc 0x194894e 0xe77afd 0x28d4d 0x2922f 0x2c53a 0x2d14f 0x2d67a 0x27ecd 0x3a61c7 0x3a6232 0x3a64da 0x3bd8e5 0x3bd9cb 0x3bdc76 0x3bdd71 0x3be89b 0x3bee93 0xc8d83f7 0x3bea88 0x71ae63 0x70cb99 0x3a6dd2 0xc8d012c 0x10763 0x16a4705 0x2c82c0 0x504a64 0x16a4705 0x2c82c0 0x2c8258 0x389021 0x38957f 0x3886e8 0x2f7cef 0x2f7f02 0x2d5d4a 0x2c7698 0x23c5df9 0x23c5ad0 0x18cebf5 0x18ce962 0x18ffbb6 0x18fef44 0x18fee1b 0x23c47e3 0x23c4668 0x2c4ffc 0x289d 0x27c5)
libc++abi.dylib: terminate called throwing an exception

我注意到,NSObject(从中NSManagedObject继承)实现+copyWithZone:但没有-copyWithZone:。错误消息表明已发送 copyWithZone: 是类的一个实例,而不是类本身。

theObject是本地方法的参数。

- (NSString *) outputValue: (NSManagedObject *) theObject {...}

您在调试器输出中看到它确实属于那种类型。它确实有一个currency不同于的属性nil

有任何想法吗?我很高兴提供更多代码,但我还不知道您可能会对哪些代码片段感兴趣。

这是基于当前 xcode 和编译器版本的通用 SDK 6.1,不是当前循环的 beta 版本。

4

1 回答 1

3

从你的 NSLog 输出

<NSManagedObject: 0x7836bf0> (entity: CustomValue; id: 0x78373d0 <x-coredata://84240925-2D7D-485E-AD9D-8DD48F602C00/CustomValue/p5> ; data: {
    contract = "0x78e4a20 <x-coredata://84240925-2D7D-485E-AD9D-8DD48F602C00/Contract/p3>";
    currency = "0x7839810 <x-coredata://84240925-2D7D-485E-AD9D-8DD48F602C00/Currency/p5>";
    ...

似乎“货币”是与另一个实体的关系,所以

[theObject valueForKey:kFieldCurrency]

正如方法所期望的那样,将返回该实体的对象而不是字符串setCurrencySymbol:

也许你想要类似的东西

[numberFormatter setCurrencySymbol:[theObject valueForKeyPath:@"currency.symbol]];

假设“符号”是“货币”实体的字符串属性。

于 2013-08-12T21:19:20.730 回答