NSKeyValueChangeNewKey returning NSNull

  • Hi,

    I have a case in which a KVO change dictionary always contains NSNull
    under NSKeyValueChangeNewKey and no idea why.

    Observation is established like this:

    [objectToObserve addObserver:self forKeyPath:@"visiblePage"
      options:NSKeyValueObservingOptionNew
      context:whatever];

    objectToObserve has this pair of accessors:

    - (NSNumber*) visiblePage {
        return [NSNumber numberWithInt:_visiblePage];
    }

    - (void) setVisiblePage:(NSNumber*)value {
        _visiblePage = [value intValue];
    }

    which are declared in the interface, too:

    - (NSNumber*) visiblePage;
    - (void) setVisiblePage:(NSNumber*)value;

    _visiblePage is an iVar of type int.

    Any idea why the change dictionary would contain NSNull for
    NSKeyValueChangeNewKey in this case?
    Note: - (NSNumber*) visiblePage is not called during the whole change
    process, but observeValueForKeyPath:ofObject:change:context: is
    called on the observing object, just with the defective change
    dictionary.

    In a very similar case (identical observed class, slightly different
    observer) everything works well.

    The workaround is obvious (using valueForKey: instead of depending on
    the change dictionary) and works, but I'd would prefer to understand
    this issue.

    This is all with Xcode 3 under 10.5.1 with GC off.

    Best,
    Kai
  • NSNull is used by KVO to represent a nil value.

    Mike.

    On 21 Dec 2007, at 18:50, Kai Brüning wrote:

    > Hi,
    >
    > I have a case in which a KVO change dictionary always contains
    > NSNull under NSKeyValueChangeNewKey and no idea why.
    >
    > Observation is established like this:
    >
    > [objectToObserve addObserver:self forKeyPath:@"visiblePage"
    > options:NSKeyValueObservingOptionNew
    > context:whatever];
    >
    > objectToObserve has this pair of accessors:
    >
    > - (NSNumber*) visiblePage {
    > return [NSNumber numberWithInt:_visiblePage];
    > }
    >
    > - (void) setVisiblePage:(NSNumber*)value {
    > _visiblePage = [value intValue];
    > }
    >
    > which are declared in the interface, too:
    >
    > - (NSNumber*) visiblePage;
    > - (void) setVisiblePage:(NSNumber*)value;
    >
    > _visiblePage is an iVar of type int.
    >
    > Any idea why the change dictionary would contain NSNull for
    > NSKeyValueChangeNewKey in this case?
    > Note: - (NSNumber*) visiblePage is not called during the whole
    > change process, but observeValueForKeyPath:ofObject:change:context:
    > is called on the observing object, just with the defective change
    > dictionary.
    >
    > In a very similar case (identical observed class, slightly different
    > observer) everything works well.
    >
    > The workaround is obvious (using valueForKey: instead of depending
    > on the change dictionary) and works, but I'd would prefer to
    > understand this issue.
    >
    > This is all with Xcode 3 under 10.5.1 with GC off.
    >
    > Best,
    > Kai
  • Mike:

    I am aware of this, but have no idea why nil should be returned in my case.

    Best,
    Kai

    > NSNull is used by KVO to represent a nil value.
    >
    > Mike.
    >
    > On 21 Dec 2007, at 18:50, Kai Brüning wrote:
    >
    >> Hi,
    >>
    >> I have a case in which a KVO change dictionary always contains NSNull under NSKeyValueChangeNewKey and no idea why.
    >>
    >> Observation is established like this:
    >>
    >> [objectToObserve addObserver:self forKeyPath:@"visiblePage"
    >> options:NSKeyValueObservingOptionNew
    >> context:whatever];
    >>
    >> objectToObserve has this pair of accessors:
    >>
    >> - (NSNumber*) visiblePage {
    >> return [NSNumber numberWithInt:_visiblePage];
    >> }
    >>
    >> - (void) setVisiblePage:(NSNumber*)value {
    >> _visiblePage = [value intValue];
    >> }
    >>
    >> which are declared in the interface, too:
    >>
    >> - (NSNumber*) visiblePage;
    >> - (void) setVisiblePage:(NSNumber*)value;
    >>
    >> _visiblePage is an iVar of type int.
    >>
    >> Any idea why the change dictionary would contain NSNull for NSKeyValueChangeNewKey in this case?
    >> Note: - (NSNumber*) visiblePage is not called during the whole change process, but observeValueForKeyPath:ofObject:change:context: is called on the observing object, just with the defective change dictionary.
    >>
    >> In a very similar case (identical observed class, slightly different observer) everything works well.
    >>
    >> The workaround is obvious (using valueForKey: instead of depending on the change dictionary) and works, but I'd would prefer to understand this issue.
    >>
    >> This is all with Xcode 3 under 10.5.1 with GC off.
    >>
    >> Best,
    >> Kai
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today