OGeek|极客世界-中国程序员成长平台

标题: ios - 是否可以创建一个继承自 AVCaptureDevice 的类? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:48
标题: ios - 是否可以创建一个继承自 AVCaptureDevice 的类?

我正在尝试像这样创建一个继承自 AVCaptureDevice 的类

在 MyClass.h 文件中

@interface MyClass : AVCaptureDevice

但是如何在 init 方法中将 AVCaptureDevice 实例分配给“self”?

这是我现在的方法(不起作用...)

在 MyClass.m 文件中

- (id) init
{
    self = [super init];
    if(self)
    {
       NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
       AVCaptureDevice *captureDevice = [devices firstObject];
       self = (MyClass*)captureDevice;
       [self myClassMethod];
    }
    return self;
}

- (void) myClassMethod
{
    NSLog(@"Doesn't work");
}

我得到了 NSInvalidArgumentException,原因:'-[AVCaptureFigVideoDevice test]: unrecognized selector sent to instance”,这是有道理的,因为 test 不是 AVCaptureDevice 中的方法。

我之前做的另一种方法是在 MyClass 类中包含 AVCaptureDevice,它有效。

但我仍然想知道我是否错过了一些实现这一想法的好方法。

谢谢。



Best Answer-推荐答案


AVCaptureDevice 不适用于子类化。
如果不使用其中一种类方法,就无法创建此类的具体实例:

+ devices
+ deviceWithUniqueID:
+ defaultDeviceWithMediaType:
+ devicesWithMediaType:

您的选项是包含实例并包装对它的访问,为它创建代理或一些包装器,甚至是类别。

关于ios - 是否可以创建一个继承自 AVCaptureDevice 的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24729974/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4