gpt4 book ai didi

cocoa - 如何绑定(bind)到NSArrayController的排列对象

转载 作者:行者123 更新时间:2023-12-03 16:20:31 25 4
gpt4 key购买 nike

我想以编程方式将自定义类 (MyClass) 数组绑定(bind)到数组 Controller (NSArrayController),并将其内容绑定(bind)到另一个数组 (modelArray)。 MyClass 显示数组的内容,就像 NSTableView 一样。

我的问题是:如何以调用可变数组的方法(即方法)的方式创建此绑定(bind)

-(void) insertObject:(id)object inContentAtIndex:(NSUInteger)index
-(void) removeObjectFromContent:(id) object

(1) 如果我以这种方式绑定(bind),则会调用上述方法,但 Controller 的内容不再绑定(bind)到 modelArray (显然)

[myArrayController bind:@"contentArray" toObject:myClassInstance withKeyPath:@"content" options:nil];

(2) 如果我以这些方式绑定(bind),则仅调用 setContent:content 方法,而不调用可变方法。另外,我尝试删除这些方法(setContent:content),但它只会引发异常 setValue:forUndefinedKey:

[myClassInstance bind:@"content" toObject:myArrayController withKeyPath:@"arrangedObjects" options:nil];

[myClassInstance bind:@"content" toObject:myArrayController withKeyPath:@"content" options:nil];

我不相信在绑定(bind)到数组 Controller 时每次添加一行时都会重新设置整个表的数组,并且我希望有相同类型的绑定(bind)。

最佳答案

您遇到的问题与键值编码如何处理数组值有关。 KVC 没有特定类型的概念,因此当您通过 KVC 访问数组值时,它无法知道返回的数组是可变的。它必须假设最坏的情况(即数组是不可变的)。它通常处理这个问题的方式是使用一个代理对象,它的作用类似于 NSMutableArray,但在幕后它会获取假定的不可变数组,创建一个可变副本,改变副本,然后推送整个副本回到使用 setter 的事情。 (这是您所看到的行为 - 整个数组被替换而不是就地突变。)

控制此功能的方法是 - (NSMutableArray *)mutableArrayValueForKey:(NSString *)key 。该方法中可能会发生很多事情,我将在下面粘贴该方法的标题注释以给出完整的故事,但长话短说,如果您希望 NSArrayController 就地改变可变数组,最简单的方法是将此覆盖添加到提供 modelArray 的类中属性:

- (NSMutableArray *)mutableArrayValueForKey:(NSString *)key
{
if ([@"modelArray" isEqual: key])
{
// We know this is mutable, even if KVC doesn't!
return self.modelArray;
}
return [super mutableArrayValueForKey:key];
}

更长的故事是,KVC 在尝试弄清楚如何处理集合突变时会寻找一系列的事情。 NSKeyValueCoding.h 中详细解释了它们。 。这些是 mutableArrayValueForKey: 的评论.

Given a key that identifies an ordered to-many relationship, return a mutable array that provides read-write access to the related objects. Objects added to the mutable array will become related to the receiver, and objects removed from the mutable array will become unrelated.

The default implementation of this method recognizes the same simple accessor methods and array accessor methods as -valueForKey:'s, and follows the same direct instance variable access policies, but always returns a mutable collection proxy object instead of the immutable collection that -valueForKey: would return. It also:

  1. Searches the class of the receiver for methods whose names match the patterns -insertObject:in<Key>AtIndex: and -removeObjectFrom<Key>AtIndex: (corresponding to the two most primitive methods defined by the NSMutableArray class), and (introduced in Mac OS 10.4) also -insert<Key>:atIndexes: and -remove<Key>AtIndexes: (corresponding to -[NSMutableArray insertObjects:atIndexes:] and -[NSMutableArray
    removeObjectsAtIndexes:]
    ). If at least one insertion method and at least one removal method are found each NSMutableArray message sent to the collection proxy object will result in some combination of -insertObject:in<Key>AtIndex:, -removeObjectFrom<Key>AtIndex:, -insert<Key>:atIndexes:, and -remove<Key>AtIndexes: messages being sent to the original receiver of -mutableArrayValueForKey:. If the class of the receiver also implements an optional method whose name matches the pattern -replaceObjectIn<Key>AtIndex:withObject: or (introduced in Mac OS 10.4) -replace<Key>AtIndexes:with<Key>: that method will be used when appropriate for best performance.
  2. Otherwise (no set of array mutation methods is found), searches the class of the receiver for an accessor method whose name matches the pattern -set<Key>:. If such a method is found each NSMutableArray message sent to the collection proxy object will result in a -set<Key>: message being sent to the original receiver of -mutableArrayValueForKey:.
  3. Otherwise (no set of array mutation methods or simple accessor method is found), if the receiver's class' +accessInstanceVariablesDirectly method returns YES, searches the class of the receiver for an instance variable whose name matches the pattern _<key> or <key>, in that order. If such an instance variable is found, each NSMutableArray message sent to the collection proxy object will be forwarded to the instance variable's value, which therefore must typically be an instance of NSMutableArray or a subclass of NSMutableArray.
  4. Otherwise (no set of array mutation methods, simple accessor method, or instance variable is found), returns a mutable collection proxy object anyway. Each NSMutableArray message sent to the collection proxy object will result in a -setValue:forUndefinedKey: message being sent to the original receiver of -mutableArrayValueForKey:. The default implementation of -setValue:forUndefinedKey: raises an NSUndefinedKeyException, but you can override it in your application.

关于cocoa - 如何绑定(bind)到NSArrayController的排列对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12010360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com