这是"Cocoa Programming For Mac Os X This (HD)“一书中的一个示例,第7章”键值编码。键-值观察“
代码如下:
Person.h:
#import <Foundation/Foundation.h>
@interface Person : NSObject {
NSString *personName;
float expectedRaise;
}
@property (readwrite, copy) NSString *personName;
@property (readwrite) float expectedRaise;
@endPerson.mm:
#import "Person.h"
@implementation Person
@synthesize expectedRaise;
@synthesize personName;
- (id)init
{
[super init];
expectedRaise = 0.05;
personName = @"New Person";
return self;
}
- (void)dealloc
{
[personName release];
[super dealloc];
}
@endMyDocument.h:
#import <Cocoa/Cocoa.h>
@interface MyDocument : NSDocument
{
NSMutableArray *employees;
}
@property (retain) NSMutableArray *employees;
@endMyDocument.mm:
#import "MyDocument.h"
#import "Person.h"
@implementation MyDocument
@synthesize employees;
- (id)init
{
if (![super init])
return nil;
employees = [[NSMutableArray alloc] init];
return self;
}
- (void)dealloc
{
[employees release];
[super dealloc];
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
@end示例运行良好。(首先是一个空白表,您可以添加或删除记录)。
现在,我尝试向数组中添加一些记录,以便空表一开始会有一些内容。
下面是我尝试过的(在init方法中):
[self willChangeValueForKey:@"employees"];
Person *p1 = [[Person alloc] init];
[employees addObject: [NSData dataWithBytes: &p1 length: sizeof(p1)]];
[self didChangeValueForKey:@"employees"];但是当我构建并出错时,我得到了错误消息:
[<NSConcreteData 0x422bf0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key personName.
......有人能帮我离开这里吗?预先感谢^_^
发布于 2011-02-12 22:56:24
这似乎是一个非常合理的回应。您将NSData添加到名为employees的数组中;从名称和KVC猜测,您可能打算将p1添加到您的数组中。所以,试一试:
[employees addObject:p1];https://stackoverflow.com/questions/4977225
复制相似问题