Objective-C结构属性的键值编码 [英] Key value coding of Objective-C struct properties

查看:114
本文介绍了Objective-C结构属性的键值编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Apple的文档键值《编码编程指南》 中,您可以在结构属性上调用valueForKey:和setValue:forKey:,它们应自动包装在NSValue对象中.我发现当我在NSDecimal上进行此调用时,出现以下错误:

According to Apple's documentation Key-Value Coding Programming Guide you can call valueForKey: and setValue:forKey: on struct properties and they should be automatically wrapped in NSValue objects. I'm finding that when I make this call on an NSDecimal I get the following error:

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

任何人都可以阐明应该怎么做吗?还是在这种情况下KVO损坏了?

Can anyone shed light on how this is supposed to be done? Or is KVO broken for this case...

推荐答案

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

It seems that Key-Value Coding does not work with structs containing bit fields. So for this test class

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对象:

the following works, and returns a NSValue object:

MyClass *o = [[MyClass alloc] init];
mystruct1 s1 = { 4, 5 };
o.s1 = s1;
NSValue *v1 = [o valueForKey:@"s1"];

,但是具有包含位字段的结构的相同代码会以完全相同的方式崩溃 消息与您的问题类似:

but the same code with the struct containing bit fields crashes with exactly the same message as in your question:

mystruct2 s2 = { 4, 5 };
o.s2 = s2;
NSValue *v2 = [o valueForKey:@"s2"]; // --> NSInvalidArgumentException

由于NSDecimal包含位字段,因此可以说明问题.

Since NSDecimal contains bit fields, this explains the problem.

这篇关于Objective-C结构属性的键值编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆