0

我有一个像这样的对象 MyObject 设置


@interface MyObject : NSObject {
    int time;
}
@property (nonatomic, assign) int time;
@end

我正在尝试使用 KVC 设置值,我的代码如下所示:


MyObject* obj = [MyObject alloc] init];

int val = 2;
NSValue* nsvalue = [NSValue valueWithBytes:&val objCType:@encode(int)];
[obj setValue:nsvalue forKey:@"time"];   // here I get the exception

当我运行这段代码时,我得到一个这样的异常:


-[NSConcreteValue intValue]: unrecognized selector sent to instance 0x6e0f670

我尝试过使用其他数据类型(浮点数)并得到相同的异常。我也尝试过使用它来获得价值valueForKey,并且效果很好。

有任何想法吗?

4

1 回答 1

0

- setValue:forKey:'value不是NSValue这里的意思。您应该将标量int装入NSNumber但不装入NSValue. 请参阅https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/DataTypes.html#//apple_ref/doc/uid/20002171-BAJEAIEE

于 2012-02-17T09:12:18.783 回答