NSArrayController 和键值编码

发布于 2024-10-21 09:26:59 字数 2943 浏览 1 评论 0原文

我正在尝试填充基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与酒说心事 2024-10-28 09:26:59

请阅读 此处。引用这句话

搜索接收者的类别
名称匹配的方法对
模式 -insertObject:inAtIndex:
和 -removeObjectFromAtIndex:
(对应NSMutableArray
原始方法
insertObject:atIndex:
分别删除ObjectAtIndex:),或
匹配模式的方法
-insert:atIndexes:-removeAtIndexes: (对应于
NSMutableArrayinsertObjects:atIndexes:
removeObjectsAtIndexes: 方法)。

所以你必须实现PAIR。您的类应该同时实现 -removeObjectFromAtIndex:insertObject:inAtIndex:

Please read the Accessor Search Pattern for Ordered Collections section in here. Quoting the sentence

The receiver's class is searched for a
pair of methods whose names match the
patterns -insertObject:in<Key>AtIndex:
and -removeObjectFrom<Key>AtIndex:
(corresponding to the NSMutableArray
primitive methods
insertObject:atIndex: and
removeObjectAtIndex: respectively), or
methods matching the pattern
-insert<Key>:atIndexes: and -remove<Key>AtIndexes: (corresponding to the
NSMutableArrayinsertObjects:atIndexes:
and removeObjectsAtIndexes: methods).

So you have to implement the PAIR. Your class should implement both -removeObjectFrom<Key>AtIndex: and insertObject:in<Key>AtIndex:

黄昏下泛黄的笔记 2024-10-28 09:26:59

根据您所说的,我不确定何时会调用 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 your NSMutableArray, so it doesn't know anything about your MyDocument 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文