gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:39:53 25 4
gpt4 key购买 nike

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

这是我的代码:

捕获.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

捕捉.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协议(protocol)。这些协议(protocol)使用相同的方法,因此您可以简单地更改协议(protocol)的名称(如果您愿意,也可以同时采用两者)。

关于c++ - Objective-C++ 无法使用 MyType *__strong 类型的左值初始化类型为 id<...> 的参数,即使它符合协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32039797/

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