对Objective-C / Cocoa键值编码和数组 [英] On Objective-C/Cocoa Key-Value coding and arrays

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

问题描述

我试图找出正确的方式来处理填充一个数组键值编码的iPhone应用程序。我提出了一些有用的东西,但它是相当黑的。基本上我将XML文档解析为一组代码生成的模型。让我们假设XML是这样的格式:

I'm trying to work out the "correct" way to handle populating an array with key-value coding for an iPhone app. I've come up with something that works, but it's fairly hackish. Basically I'm parsing an XML document into a set of code-generated models. Let's assume the XML is of this format:

<foo>
    <bar>
        <item name="baz" />
        <item name="bog" />
    </bar>
</foo>

在我生成的代表Bar元素的对象上,我有一个为子节点定义的NSMutableArray:

On my generated object that represents the Bar element, I have an NSMutableArray defined for the sub-node:

@interface Bar : NSObject {
    NSMutableArray *item;
}
@end


$ b

所以默认情况下,当我调用setValue:forKey:在Bar的实例上,它最终用Item对象的单个实例覆盖NSMutableArray的实例。我目前做了这件工作是在哪里得到hacky。我将数组实例变量重命名为别的东西,让我们说复数形式的名字:

So by default when I call setValue:forKey: on an instance of Bar, it ends up overwriting the instance of NSMutableArray with a single instance of the Item object. What I've currently done to get this working is where it gets hacky. I renamed the array instance variable to be something else, let's say the plural form of the name:

@interface Bar : NSObject {
    NSMutableArray *items;
}
@end

这将导致setValue的默认存取器:forKey:错过。然后我将这个方法添加到Bar实现:

This causes the default accessor for setValue:forKey: to miss. I then added this method to the Bar implementation:

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    if([key isEqualToString:@"item"]) {
        if(items == nil) {
            items = [[NSMutableArray alloc] init];
            [items retain];         
        }
        [items addObject:value];
    }
}

我肯定有一个更好的方法来做到这一点!我已经阅读了键值编码编程指南,但我必须缺少一些东西,因为我不清楚应该如何工作的数组访问器。我试过实现countOf:和objectInAtIndex:因为他们的KVC编程指南似乎表明,但仍然导致与我的NSMutableArray被项目类型的实例覆盖。

And everything works! I'm sure there must be a better way to do this though! I've read through the Key-Value Coding Programming Guide, but I must be missing something, because I'm unclear as to how things are supposed to work for array accessors. I've tried implementing countOf: and objectInAtIndex: as they KVC Programming guide seems to indicate, but that still results with my NSMutableArray being overwritten with an instance of the Item type.

推荐答案

如果你想使用KVC属性的数组,你应该查看 mutableArrayValueForKey:

If you want to use KVC properties for an array you should look into the documentation for mutableArrayValueForKey:

基本上用你的类:

@interface Bar : NSObject {
    NSMutableArray *items;
}
@end

您将这些方法定义为最小

You would define these methods as a minimum, assuming you initialized your array elsewhere:

- (void)insertObject:(id)object inItemsAtIndex:(NSUInteger)index {
    [items insertObject:object atIndex:index];
}

- (void)removeObjectFromItemsAtIndex:(NSUInteger)index {
    [items removeObjectAtIndex:index];
}

- (NSArray *)items {
    /* depending on how pedantic you want to be
       possibly return [NSArray arrayWithArray:items] */
    return items;
}

一旦你实现了这些方法,你可以调用 [bar mutableArrayValueForKey:@items] 。它将返回一个 NSMutableArray 代理对象,您可以从中添加和删除对象。该对象将反过来生成适当的KVO消息,并使用刚才定义的方法与实际数组进行交互。

Once you've implemented those methods you can call [bar mutableArrayValueForKey:@"items"]. It will return an NSMutableArray proxy object that you can add and remove objects from. That object will in turn generate the appropriate KVO messages and uses the methods you just defined to interact with your actual array.

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

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