简体   繁体   English

尝试执行方法调用时使用未声明的标识符错误

[英]Use of undeclared identifier error when trying to perform method call

I am using the AV Foundation framework. 我正在使用AV Foundation框架。 I am trying to perform a method call of "deviceInputWithDevice" for an "AVCaptureDeviceInput" object. 我正在尝试为“AVCaptureDeviceInput”对象执行“deviceInputWithDevice”的方法调用。

The problem is that the method call contains an "error" parameter that I have named "error" and I keep getting this warning in xcode: Use of undelcared identifier 'error' 问题是方法调用包含一个名为“error”的“error”参数,我在xcode中不断收到此警告:使用undelcared标识符“error”

All of my AV Foundation code is located in the View Controller's ViewDidLoad method implementation. 我的所有AV Foundation代码都位于View Controller的ViewDidLoad方法实现中。 Here it is: 这里是:

    AVCaptureSession *session = [[AVCaptureSession alloc]init];

    session.sessionPreset = AVCaptureSessionPresetHigh;

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:error];

    [session addInput:input];

I can't figure out why it keeps giving me the undeclared identifier warning for the "error" parameter. 我无法弄清楚为什么它一直给我“错误”参数的未声明的标识符警告。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

You have to declare the error variable that you are trying to use: 您必须声明您尝试使用的error变量:

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

And note that you need an & before error when you pass it to the method. 请注意,将其传递给方法时需要& before error And of course you should check it: 当然你应该检查一下:

if (input) {
    // it succeeded, do something
} else {
    NSLog(@"Error trying to call deviceInputWithDevice: %@", error);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM