4

I would like to observe the count property of an NSMutableArray object. I was able to directly observe the array for changes whenever an object was added or removed by utilizing the Indexed Array Accessors for a to-many relationship. However, I would like to simply observe the count property instead, so that when I use my observeValueForKeyPath method, the object being passed into the parameter is the array object itself, rather than the class holding the array.

My situation is as follows:

I have an NSMutableArray *cards declared in my AppDelegate class as a property (and ivar).

From my viewcontroller, I try observing the count property of this array:

[appDelegate.cards addObserver:self forKeyPath:@"count" options:0 context:NULL];

However, this crashes my program with the following error:

[<__NSArrayM 0x4e17fd0 addObserver:forKeyPath:options:context] is not supported. Key path: count'

I've tried implementing the accessors for a to-many relationship

- (void)addCardsObject:(Card *)anObject;
- (void)removeCardsObject:(Card *)anObject;

However, the program still crashes.

I have a few questions:

  1. How would I be able to observe the count property of this NSMutableArray?
  2. Is the count propety of the NSMutableArray defined as a to-one relationship, or is it the entire object that matters when determining whether it's a to-many or to-one relationship (NSMutableArray is a collection object, therefore it's a to-many relationship, EVEN though I'm merely looking to observe the count property, and not properties of objects in the collection).

Thanks in advance.

0

2 Answers 2

4

NSArray itself just doesn't support KVO, period. It's the controller in front of the array that you need to observe. For example, if you have an NSArrayController, you can set an observer for arrangedObjects.count.

Sign up to request clarification or add additional context in comments.

4 Comments

Is it possible to implement this programmatically, rather than using IB? If not, is there any alternative way to accomplish this? I'm trying to do everything programmatically in my app.
@Jim Hubbard: Yes. KVO is fully accessible through programmatic means: google.com/…
Not KVO, creating an NSArrayController object, and using that. I've searched for tutorials, but it seems like all tutorials I find are how to implement an NSArrayController through interface builder.
@Jim Hubbard: You don't strictly need an NSArrayController — any kind of controller that's KVO-compliant for an array property should work. It sounds like you already have a controller that implements the indexed accessor methods, so I'd just use that. (You can set up an NSArrayController in code, though. You just need to send bind:… messages rather than filling out the binding form in Interface Builder.)
1

Thats sad to know that NSarry, NSMutableArray do not support KVO. And I faced this when I wanted to use reactive cocoa to observe selection.

But thankfully, UIViewController is KVO compliant.

//create a readonly property selectionCount
@property (nonatomic, readonly)NSInteger selectionCount;
...

//Implement the getter method
-(NSInteger)selectionCount{
    return self.arrSelection.count;
}
...

RAC(self.btnConfirm, enabled) = [RACSignal combineLatest:@[RACAbleWithStart(self.selectionCount)] reduce:^(NSNumber *count){
        return @([count integerValue] > 0);
    }];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.