Question

I can't figure out how to simple observe change in one object property, that's not working as expected.

Here my code:

- (void)observeBasketValueChane:(MyObject*)theObject{
    [theObject addObserver: self
                  forKeyPath: @"value"
                     options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                     context: NULL];

// not working
    theObject.value = 5;

// will work
    [theObject willChangeValueForKey:@"value"];
    [theObject setValue: [NSNumber numberWithInt: 3] forKey:@"value"];
    [theObject didChangeValueForKey:@"value"];
}

The Apple doc said KVO on dot syntax is automatically done. My value property is synthesized on MyObject but that is not working as expected. Note that Myobject is a coreData object. But i don't see why it will change something since i have synthesized the method. Thank you for your help.

* edit

As Quellish said, my property was defined as

@property (nonatomic) int32_t value;

And that will not works.

The correct way is

@property (nonatomic) NSNumber* value;
Was it helpful?

Solution

This line:

theObject.value = 5;

Will not work, because you are setting a property that represents an NSNumber with an integer. Try this instead to see if it fixes your issue:

theObject.value = [NSNumber numberWithInteger:5];

You seem to have some other issues with how you are implementing KVO, but it looks like the above will fix the problem you are describing in this question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top