Objective-C variable… pointing to itself?

前端 未结 2 471
半阙折子戏
半阙折子戏 2021-02-04 05:12

I spotted this construct in some of Apple\'s example code for dealing with key-value observing. When adding an observer, you can add a context (in the form of a void* variable)

相关标签:
2条回答
  • 2021-02-04 06:00

    I think this is overly complicated and confusing. When you want to have a unique context for KVO just declare it and use a pointer to it:

    static int kMyObjectPropertyObservationContext;
    
    ...
    
    [object addObserver:self
             forKeyPath:@"myProperty"
                options:0
                context:&kMyObjectPropertyObservationContext];
    
    0 讨论(0)
  • 2021-02-04 06:05

    I think that the most accurate description would be "a self-referential pointer".

    0 讨论(0)
提交回复
热议问题