3

根据 Apple 的文档Key-Value Coding Programming Guide,您可以在结构属性上调用 valueForKey: 和 setValue:forKey:,它们应该自动包装在 NSValue 对象中。我发现当我在 NSDecimal 上进行此调用时,出现以下错误:

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

任何人都可以阐明这应该如何完成?或者这种情况下KVO是否损坏......

4

1 回答 1

4

似乎键值编码不适用于包含位字段的结构。所以对于这个测试类

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.

于 2013-04-11T21:36:14.843 回答