绑定问题与包含NSPoint的NSValue

时间:2011-08-11 12:22:21

标签: cocoa core-data binding cocoa-bindings appkit

我有这个managedObject子类,位置rappresent一个NSPoint。在XIB中我想要两个像这样的NSTextField

http://s1.postimage.org/rkd74ebo/Schermata_08_2455785_alle_14_13_57.png

其中一个显示position.x值,另一个显示position.y。我试图将NSTextField绑定到管理帧的NSArrayController的“selection.position.x”和“selection.position.y”keyPath

我收到此错误::

  

[setValue:forUndefinedKey:]:这个类   不是密钥x的密钥值编码兼容。

所以我检查了KVC:

Frame.h

@interface Frame : NSManagedObject {

@private

}

@property (nonatomic, retain) NSNumber  * order;
@property (nonatomic, retain) NSNumber  *visible;
@property (nonatomic, retain) NSValue   *position;
@property (nonatomic, retain) NSNumber  *scale;
@property (nonatomic, retain) Sprite    *sprite;
@property (nonatomic, retain) Resource  *resource;

//test
@property (nonatomic, retain) NSValue   *frame;

@end

-(void)awakeFromInsert我试试这段代码:

-(void)awakeFromInsert
{
    [super awakeFromInsert];

    self.position = [NSValue valueWithPoint:NSPointFromCGPoint(CGPointZero)];

    //[self.position setValue:[NSNumber numberWithFloat:50.0f] forKey:@"x"];

    [self setValue:[NSNumber numberWithFloat:1.0f] forKeyPath:@"scale"];
    [self setValue:[NSNumber numberWithFloat:10.0f] forKeyPath:@"frame.origin.x"];
    [self setValue:[NSNumber numberWithFloat:10.0f] forKeyPath:@"position.x"];
}

第一个和第二个KVC setValue工作,第三个崩溃与上面的相同消息

[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key x.

我不明白为什么,任何帮助都是值得欣赏的。

1 个答案:

答案 0 :(得分:0)

NSValue对象不可变。要更改NSValue属性,必须创建新的NSValue对象,然后将其分配给属性。

相关问题