NSArrayController和键值编码 [英] NSArrayController and Key Value Coding

查看:38
本文介绍了NSArrayController和键值编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试填充基于 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

推荐答案

请阅读

搜索接收者的班级一对名称匹配的方法模式 -insertObject:inAtIndex:和- removeObjectFrom< Key> AtIndex:(对应于NSMutableArray原始方法 insertObject:atIndex:和removeObjectAtIndex:分别),或匹配模式的方法 -insert< Key>:atIndexes: -remove< Key> AtIndexes:(对应于 NSMutableArrayinsertObjects:atIndexes: removeObjectsAtIndexes:方法).

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).

因此,您必须实施 PAIR .您的类应同时实现- removeObjectFrom< Key> AtIndex: insertObject:in< Key> AtIndex:

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

这篇关于NSArrayController和键值编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆