6

AVFoundation.Framework->のサンプル コードを調べているところですAVSimpleEditoriOSが、理解できない次の行が見つかりました。

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

以下を検討してください

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

2 行の上で、これらが 2 つの静的な void/ジェネリック ポインターであり、派手な名前が付いていることがわかります。

その2行に戻って、ここにもう一度貼り付けます。

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

上記は、それ自体の参照を格納する2つの静的void /ジェネリックポインターを意味しますか?なぜそれがどのような意味で必要なのですか?

そのようなコーディングパターンを学ぶためのガイドはほとんど必要ありません。知識お待ちしております。

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 に答える