NSArrayController和键值编码 [英] NSArrayController and Key Value Coding
问题描述
我正在尝试填充基于 NSTableView
的文档,并使用 NSArrayController
对其进行控制.我想我了解键值编码的概念.但是,我担心 NSArrayController
不能兑现
I am trying to populate a document based NSTableView
and control it using the NSArrayController
. I guess I understood the concepts of Key Value coding. However, I fear the NSArrayController
is not honoring the Accessor Search Pattern for Ordered Collections. Let me explain
我定义了一个班级名称Student
I have a class Name Student as defined
#import <Cocoa/Cocoa.h>
@interface Student : NSObject {
NSString* studentName;
float marks;
}
//Accessor and mutators
@property (readwrite, copy) NSString* studentName;
@property (readwrite) float marks;
//Initializer - Init all resources
-(id) init;
//Dealloc - Release resources
-(void) dealloc;
@end
实现是
#import "Student.h"
@implementation Student
//Synthesize the accessors
@synthesize studentName;
@synthesize marks;
//Initializer - Init all resources
-(id) init
{
self = [super init];
if(self){
studentName = @"New Student";
marks = 0.0;
}
return self;
}
//Dealloc - Release resources
-(void) dealloc
{
[studentName release];
[super dealloc];
}
@end
MyDocument类的定义如下,其中包含一个 NSMutableArray
类型的即时变量
MyDocument class is defined as follows which contains an NSMutableArray
type instant variable
#import <Cocoa/Cocoa.h>
@class Student;
@interface MyDocument : NSDocument
{
NSMutableArray* students;
}
//Initializers
-(id) init;
//Deallocators
-(void) dealloc;
//Creating the proxy object
-(id) mutableArrayValueForKey:(NSString *)key;
//Array controller uses keyvalue
//coding to call this
-(void) insertObject:(Student*) s inStudentsAtIndex:(int) index;
@end
在IB中,将数组控制器的属性设置为Student对象,并将其实例变量添加到键中.在绑定部分,内容数组绑定到文件所有者,后者是MyDocument类的实例.模型键路径设置为数组名称 students
In IB, the Array Controller's attributes is set to Student object and its instance variables are added to the key. In the binding section, Content Array is bind to File Owner's which is an instance of MyDocument class. The model key path is set to the array name students
这是MyDocument的实现
Here is the implementation of MyDocument
#import "MyDocument.h"
#import "Student.h"
@implementation MyDocument
- (id)init
{
self = [super init];
if (self) {
students = [[NSMutableArray alloc] init];
}
return self;
}
-(void) dealloc
{
[students release];
[super dealloc];
}
//Array controller uses keyvalue
//coding to call this
-(void) insertObject:(Student*) s inStudentsAtIndex:(int) index
{
NSLog(@"Insert object is called");
}
//Creating the proxy object
-(id) mutableArrayValueForKey:(NSString *)key
{
NSLog(@"Checking if NSArrayController is trying to create a proxy %@",key);
return students;
}
我的问题-(void)insertObject:(Student *)的inStudentsAtIndex:(int)索引
从未被调用.但是,如果我实现一个函数名称-(void)setStudents:(Student *)s
,则会调用该函数.-(id)mutableArrayValueForKey:(NSString *)key
仅用于调试目的;我想查看键值编码的某些部分是否正常工作.无论是否使用-(id)mutableArrayValueForKey:(NSString *)key
My problem -(void) insertObject:(Student*) s inStudentsAtIndex:(int) index
is never called. However, if I implement a function name -(void) setStudents:(Student*)s
, that is called. -(id) mutableArrayValueForKey:(NSString *)key
is just for debugging purpose; I wanted to see some part of the Key Value coding is working. The behavior is same with or without -(id) mutableArrayValueForKey:(NSString *)key
我想念什么?我在使用XCode 3.2.5的Mac 10.6.6上
What am I missing ? I am on Mac 10.6.6 with XCode 3.2.5