Void static pointer in AVFoundation.Framework in code sample

I am just looking at the sample code AVFoundation.Framework AVSimpleEditoriOS , and I found the following line that I could not understand.

 static void *AVSEPlayerItemStatusContext = &AVSEPlayerItemStatusContext; static void *AVSEPlayerLayerReadyForDisplay = &AVSEPlayerLayerReadyForDisplay; 

consider the following

 static void *AVSEPlayerItemStatusContext = nil; static void *AVSEPlayerLayerReadyForDisplay = nil; 

Above the two lines, I can understand that these are 2 static void / generic pointers with some bizarre name.

Now back to these two lines, I insert them here again,

 static void *AVSEPlayerItemStatusContext = &AVSEPlayerItemStatusContext; static void *AVSEPlayerLayerReadyForDisplay = &AVSEPlayerLayerReadyForDisplay; 

Does this mean 2 static void / generic pointers that keep a reference to it and why is it necessary in this sense?

I just need a little tutorial to learn such a coding pattern. Waiting for knowledge.

+6
source share
1 answer

Self-regulation pointer

 static void *foo = &foo; 

is just a method of creating a unique pointer at compile time .

In this AVSimpleEditoriOS example , these pointers are later used as the context parameter for

 [self addObserver:self forKeyPath:@"player.currentItem.status" options:NSKeyValueObservingOptionNew context:AVSEPlayerItemStatusContext]; 

and

 [self addObserver:self forKeyPath:@"playerLayer.readyForDisplay" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:AVSEPlayerLayerReadyForDisplay]; 

The actual value of the context parameter does not matter at all, it's just some unique value that is passed to

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (context == AVSEPlayerItemStatusContext) { // Notification for @"player.currentItem.status" // ... } else if (context == AVSEPlayerLayerReadyForDisplay) { // Notification for @"playerLayer.readyForDisplay" // ... } else { // Something else, pass to superclass: [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } 

(Alternatively, you can check the keyPath parameter in observeValueForKeyPath .) See @Bavarious's comment below why unique context pointers are generally preferred for key strings.

+6
source

Source: https://habr.com/ru/post/946577/


All Articles