6

我只是在浏览AVFoundation.Framework-> AVSimpleEditoriOS& 的示例代码,我发现以下行我无法理解。

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

考虑以下

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

上面两行,我可以算出它们是 2 个带有一些花哨名称的静态 void/通用指针。

现在回到那两行,我再次将其粘贴在这里,

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

以上是否意味着,2个静态无效/通用指针存储它自己的引用以及为什么在什么意义上需要它?

我只需要很少的指南来学习这种编码模式。等待知识。

4

1 回答 1

7

引用指针

static void *foo = &foo;

只是一种在编译时创建唯一指针的方法

在那个“AVSimpleEditoriOS”示例项目中,这些指针稍后用作context参数

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

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

上下文参数的实际值根本不重要,它只是传递给的一些唯一值

- (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];
    }
}

(或者,可以检查 中的keyPath参数observeValueForKeyPath。) 请参阅下面的@Bavarious 评论,了解为什么通常首选唯一上下文指针而不是关键路径字符串。

于 2013-06-05T09:54:28.447 回答