gpt4 book ai didi

ios - 示例代码中 AVFoundation.Framework 中的 static void 指针

转载 作者:可可西里 更新时间:2023-11-01 03:52:17 26 4
gpt4 key购买 nike

我正在查看 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 个静态空指针/通用指针存储它自己的引用以及为什么在什么意义上需要它?

我只需要很少的指导就可以学习这种编码模式。等待知识。

最佳答案

自引用指针

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

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

关于ios - 示例代码中 AVFoundation.Framework 中的 static void 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16936706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com