1

Possible Duplicate:
KVO and NSMutableArray

Like the title says, I need to be able to detect a change in a mutable array. The reason for this would be to automatically refresh my mapview for an app I'm making as soon as the user adds a new location.

I imagine it would be something like asking if the array is equal to the array's count plus one or the array's count minus one but I don't know how to implement that.

If you need more information before answering please don't hesitate to ask in the comments.

Community
  • 1
  • 1
crenfrow
  • 159
  • 1
  • 7

4 Answers4

2

You can use Key-Value Observing for this. For more details check Introduction to Key-Value Observing Programming Guide.

Key-value observing provides a mechanism that allows objects to be notified of changes to specific properties of other objects. It is particularly useful for communication between model and controller layers in an application. (In OS X, the controller layer binding technology relies heavily on key-value observing.) A controller object typically observes properties of model objects, and a view object observes properties of model objects through a controller. In addition, however, a model object may observe other model objects (usually to determine when a dependent value changes) or even itself (again to determine when a dependent value changes).

You can observe properties including simple attributes, to-one relationships, and to-many relationships. Observers of to-many relationships are informed of the type of change made—as well as which objects are involved in the change.

Check this blog about KVO on how to use it. Also check this tutorial. Check this question as well, Key-Value-Observing a to-many relationship in Cocoa

Community
  • 1
  • 1
iDev
  • 23,310
  • 7
  • 60
  • 85
1

There are many answers to this question.

A popular way would be to use NSNotficationCenter to post notifications everytime your model changes.

You can also use delegation to invoke a method when an object is inserted or Key-Value Observing.

It much depends on your design. If you specify, maybe you can get a more concrete answer.

Mario
  • 4,530
  • 1
  • 21
  • 32
0

you could use KVO for that. Personally I don't like NotificationCenter but I guess that personal taste.

see KVO and NSMutableArray

Community
  • 1
  • 1
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
0

Yet another solution would be Don't Do That. If you the one making the change to the mutable array, then you know you are making a change to the mutable array. If you aren't, then don't vend a mutable array; wrap the array in a class so that any changes must come in through a method that you control.

matt
  • 515,959
  • 87
  • 875
  • 1,141