ios – 示例代码中AVFoundation.Framework中的静态void指针

我正在浏览AVFoundation.Framework的示例代码 – > AVSimpleEditoriOS&我发现以下一行我无法理解.
static void *AVSEPlayerItemStatusContext = &AVSEPlayerItemStatusContext;
static void *AVSEPlayerLayerReadyFordisplay = &AVSEPlayerLayerReadyFordisplay;

考虑以下

static void *AVSEPlayerItemStatusContext = nil;
static void *AVSEPlayerLayerReadyFordisplay = nil;

在上面的两行中,我可以看出那些是2个静态的void / generic指针,带有一些奇特的名字.

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

static void *AVSEPlayerItemStatusContext = &AVSEPlayerItemStatusContext;
static void *AVSEPlayerLayerReadyFordisplay = &AVSEPlayerLayerReadyFordisplay;

上面的意思是,2个静态void / generic指针存储它自己的&为什么它需要在什么意义上?

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

解决方法

一个自引用指针
static void *foo = &foo;

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

在该“AVSimpleEditoriOS”示例项目中,这些指针稍后用作上下文参数

[self addobserver:self forKeyPath:@"player.currentItem.status" options:NSkeyvalueObservingOptionNew context:AVSEPlayerItemStatusContext];

[self addobserver:self forKeyPath:@"playerLayer.readyFordisplay" options:NSkeyvalueObservingOptionInitial | NSkeyvalueObservingOptionNew context:AVSEPlayerLayerReadyFordisplay];

context参数的实际值根本不重要,它只是一些独特的
传递给的值

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

(或者,可以检查observeValueForKeyPath中的keyPath参数.)请参阅下面的@BavarIoUs’评论,了解为什么唯一的上下文指针通常比键路径字符串更受欢迎.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...