NSArrayController 和键值编码
我正在尝试填充基于 NSTableView
的文档并使用 NSArrayController
控制它。我想我理解了 键值编码的概念。但是,我担心 NSArrayController
不遵守 有序集合的访问器搜索模式。让我解释一下,
我定义了一个名为 Student 的类
#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
类型即时变量
#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
这是 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*) s inStudentsAtIndex:(int) index
从未被调用过。但是,如果我实现一个函数名称 -(void) setStudents:(Student*)s
,则会调用该函数。 -(id) mutableArrayValueForKey:(NSString *)key
仅用于调试目的;我想看到键值编码的某些部分正在工作。无论有没有 -(id) mutableArrayValueForKey:(NSString *)key
,行为都是相同的
我错过了什么?我使用的是 Mac 10.6.6 和 XCode 3.2.5
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
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
The implementation is
#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 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
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
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;
}
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
What am I missing ? I am on Mac 10.6.6 with XCode 3.2.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请阅读 此处。引用这句话
所以你必须实现PAIR。您的类应该同时实现 -
removeObjectFromAtIndex:
和insertObject:inAtIndex:
Please read the Accessor Search Pattern for Ordered Collections section in here. Quoting the sentence
So you have to implement the PAIR. Your class should implement both -
removeObjectFrom<Key>AtIndex:
andinsertObject:in<Key>AtIndex:
根据您所说的,我不确定何时会调用
insertObject:
。在 IB 中,您将数组控制器绑定到NSMutableArray
,因此它不知道有关您的MyDocument
类的任何信息。如果您希望在新学生添加到您的数组时收到某种通知,您需要自己连接。执行此操作的一种方法是自己处理 UI 中的操作(例如,如果您有“新建”按钮),并在该处理程序中将对象添加到数组中,并执行所需的任何其他逻辑。
Based on what you're said, I'm not sure when
insertObject:
would be called. In IB, you bound the array controller to yourNSMutableArray
, so it doesn't know anything about yourMyDocument
class.If you want some sort of notification of when a new student is added to your array, you would need to hook that up yourself. One way to do this is to handle the action from the UI yourself (such as if you have a New button), and in that handler add the object to your array, and do whatever other logic is required.