观察NSMutableArray用于插入/删除 [英] Observing an NSMutableArray for insertion/removal

查看:135
本文介绍了观察NSMutableArray用于插入/删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类具有类型为NSMutableArray的属性(和实例var),带有合成访问器(通过 @property )。如果你观察这个数组使用:

  [myObj addObserver:self forKeyPath:@theArrayoptions:0 context:NULL]; 

然后在数组中插入一个对象,如下:

  [myObj.theArray addObject:NSString.string]; 

发送了一个observeValueForKeyPath ...通知。但是,以下确实发送了适当的通知:

  [[myObj mutableArrayValueForKey:@theArray] addObject:NSString.string] ; 

这是因为 mutableArrayValueForKey



但合成的访问器不应该自动返回这样的代理对象吗?我应该写一个自定义的访问器,只是调用 [super mutableArrayValueForKey ...]


$ b
$ b

否。


如何解决这个问题?只是调用 [super mutableArrayValueForKey ...]


实施数组访问器。当您调用这些时,KVO将自动发布相应的通知。所以你需要做的是:

  [myObject insertObject:newObject inTheArrayAtIndex:[myObject countOfTheArray]]; 

正确的事情会自动发生。



为方便起见,你可以写一个 addTheArrayObject:存取器。这个访问器将调用上述的一个真正的数组访问器:

   - (void)addTheArrayObject:(NSObject *)newObject {
[self insertObject:newObject inTheArrayAtIndex:[self countOfTheArray]];
}

(您可以并且应该为数组中的对象填写适当的类,代替 NSObject 。)



然后,而不是 [myObject insertObject:... ] ,您写 [myObject addTheArrayObject:newObject]



c $ c> add< Key> Object:及其对应物 remove< Key> Object:设置(如在NSSet)属性,而不是数组属性,所以你不会得到免费的KVO通知与他们,除非你实现它们的访问器,它确认识别。我提出了一个错误:x-radar:// problem / 6407437



我有我的博客上的所有访问器选择器格式的列表。 >

A class has a property (and instance var) of type NSMutableArray with synthesized accessors (via @property). If you observe this array using:

[myObj addObserver:self forKeyPath:@"theArray" options:0 context:NULL];

And then insert an object in the array like this:

[myObj.theArray addObject:NSString.string];

An observeValueForKeyPath... notification is not sent. However, the following does send the proper notification:

[[myObj mutableArrayValueForKey:@"theArray"] addObject:NSString.string];

This is because mutableArrayValueForKey returns a proxy object that takes care of notifying observers.

But shouldn't the synthesized accessors automatically return such a proxy object? What's the proper way to work around this--should I write a custom accessor that just invokes [super mutableArrayValueForKey...]?

解决方案

But shouldn't the synthesized accessors automatically return such a proxy object?

No.

What's the proper way to work around this--should I write a custom accessor that just invokes [super mutableArrayValueForKey...]?

No. Implement the array accessors. When you call these, KVO will post the appropriate notifications automatically. So all you have to do is:

[myObject insertObject:newObject inTheArrayAtIndex:[myObject countOfTheArray]];

and the Right Thing will happen automatically.

For convenience, you can write an addTheArrayObject: accessor. This accessor would call one of the real array accessors described above:

- (void) addTheArrayObject:(NSObject *) newObject {
    [self insertObject:newObject inTheArrayAtIndex:[self countOfTheArray]];
}

(You can and should fill in the proper class for the objects in the array, in place of NSObject.)

Then, instead of [myObject insertObject:…], you write [myObject addTheArrayObject:newObject].

Sadly, add<Key>Object: and its counterpart remove<Key>Object: are, last I checked, only recognized by KVO for set (as in NSSet) properties, not array properties, so you don't get free KVO notifications with them unless you implement them on top of accessors it does recognize. I filed a bug about this: x-radar://problem/6407437

I have a list of all the accessor selector formats on my blog.

这篇关于观察NSMutableArray用于插入/删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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