2013-04-11 32 views
2

根据Apple的文档Key-Value Coding Programming Guide,您可以调用valueForKey:和setValue:forKey:on结构属性,并且它们应该自动包装在NSValue对象中。我发现,当我作出一个NSDecimal我收到以下错误此调用:Objective-C结构属性的键值编码

-[NSInvocation getArgument:atIndex:]: struct with unknown contents found while getting argument at index -1

任何人都可以阐明这是怎么认为光做?或者KVO破坏为这种情况...

+2

显示您拨打电话时的实际代码 – 2013-04-11 20:39:11

回答

3

似乎键值编码不能与包含位字段的结构一起使用。 因此,对于这个测试类

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 

由于NSDecimal包含位字段,这就解释了这个问题。