Objective-C++ 无法使用 MyType *__strong 类型的左值初始化类型为 id<...> 的参数,即使它符合协议

Objective-C++ cannot initialize parameter of type id<...> with lvalue of type MyType *__strong even though it conforms to protocol

我正在尝试在 OSX Objective-C++ 应用程序中使用 AVKit,并且我有一个 VideoSource class 符合 AVCaptureAudioDataOutputSampleBufferDelegate , 但编译器不会接受它作为 id<AVCaptureVideoDataOutputSampleBufferDelegate> 类型的参数。我收到此错误:Capture.mm:30:48: Cannot initialize a parameter of type 'id<AVCaptureVideoDataOutputSampleBufferDelegate>' with an lvalue of type 'VideoSource *__strong'

这是我的代码:

Capture.h

#ifndef ThreesAI_Capture_h
#define ThreesAI_Capture_h

#ifdef __OBJC__

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface VideoSource : NSObject <AVCaptureAudioDataOutputSampleBufferDelegate>

@property AVCaptureSession *s;

- (id) init;

@end

#endif

void hello();

#endif

Capture.mm

#import "Capture.h"

@implementation VideoSource

- (id) init {
    if (self = [super init]) {
        self.s = [[AVCaptureSession alloc] init];
        AVCaptureDevice *camera = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][0];
        NSError *e;
        AVCaptureInput *cameraInput = [AVCaptureDeviceInput deviceInputWithDevice:camera error:&e];
        [self.s addInput:cameraInput];
        
        AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
        captureOutput.alwaysDiscardsLateVideoFrames = YES;
        dispatch_queue_t queue;
        queue = dispatch_queue_create("cameraQueue", NULL);
        [captureOutput setSampleBufferDelegate:self queue:queue];//Error on this line
        
        [self.s addOutput:captureOutput];
        [self.s startRunning];
    }
    return self;
}

- (void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    NSLog(@"asdasd");
}

@end

void hello() {
    VideoSource *v = [[VideoSource alloc] init];
}

我不确定这是怎么回事,因为 VideoSource 似乎应该是 id<AVCaptureAudioDataOutputSampleBufferDelegate> 类型。怎么回事?

您正在使用 AVCapture<b><i>Video</i></b>DataOutput,但您只声明符合AVCapture<b><i>Audio</i></b>DataOutputSampleBufferDelegate 协议。这些协议使用相同的方法,因此您可以简单地更改协议的名称(如果您愿意,也可以同时采用两者)。