似乎键值编码不适用于包含位字段的结构。所以对于这个测试类
typedef struct { int a; int b; } mystruct1;
typedef struct { int a:4; int b:4; } mystruct2;
@interface MyClass : NSObject
@property (nonatomic) mystruct1 s1;
@property (nonatomic) mystruct2 s2; // struct with bit fields
@end
以下工作,并返回一个NSValue
对象:
MyClass *o = [[MyClass alloc] init];
mystruct1 s1 = { 4, 5 };
o.s1 = s1;
NSValue *v1 = [o valueForKey:@"s1"];
但是与包含位字段的结构相同的代码会崩溃,并显示与您的问题完全相同的消息:
mystruct2 s2 = { 4, 5 };
o.s2 = s2;
NSValue *v2 = [o valueForKey:@"s2"]; // --> NSInvalidArgumentException
Since NSDecimal
contains bit fields, this explains the problem.