This:
[array.arrayData arrayByAddingObjectsFromArray:newItems];
actually has no effect. The -arrayByAddingObjectsFromArray:
creates and returns a new array. It does not affect the receiver (array.arrayData
). Since you're not doing anything with the returned value, it is just thrown away. So, a new array is created and then thrown away. Nothing else is affected.
The wiki article to which you linked, showed a slightly different statement:
self.myArray = [self.myArray arrayByAddingObject:myNewObject];
In that case, it is the assignment to the property which generates the KVO change notification.
An important concept to understand is that KVO observes the object which has the property for changes in that property. In your case, KVO is observing the object referenced by self.array
at the time you added the observer. It is observing it for changes in its arrayData
property.
The reason this is important is that KVO is not observing the array itself. That's a point of confusion for many folks when they're new to KVO. Modifying the array (for example, if it were a mutable array) would not generate any KVO change notifications.
All KVO-compliant changes must occur through messages to the observed object (self.array
in your case). Here are some of the messages to the self.array
that would generate change notifications for its arrayData
property:
-setArrayData:
, the property setter. KVO recognizes the property setter using naming conventions, so the setter name must be of the form -set<Key>:
.
- Since it's apparently an indexed to-many relationship property, any of the mutable indexed accessors, such as
-insertObject:inArrayDataAtIndex:
or -removeArrayDataAtIndexes:
. You would need to implement such accessors on the class which has the arrayData
property. If the property is backed by an NSMutableArray
, those methods can be simple pass-throughs to the similar methods on NSMutableArray
.
- Explicit invocation of the
-willChange...
and -didChange...
methods of the NSKeyValueObserving
informal protocol, specifying @"arrayData"
as the key. This is generally discouraged.
- Using KVC methods such as
-setValue:forKey:
, specifying @"arrayData"
as the key. This will work even in the absence of proper accessors so long as the instance variable is named either _arrayData
or arrayData
and the class method +accessInstanceVariablesDirectly
hasn't been overridden to disable it. Relying on direct instance variable access is really discouraged because it violates encapsulation.
- Obtaining a KVC mutable array proxy for the property using
-mutableArrayValueForKey:
and then sending NSMutableArray
mutation methods to it. This will actually use one of the above mechanisms to modify the property, so it's actually redundant but it's good to know about.