gpt4 book ai didi

swift - View Controller 没有初始化器

转载 作者:搜寻专家 更新时间:2023-11-01 06:01:33 25 4
gpt4 key购买 nike

class ViewController3: UIViewController {

let session = AVCaptureSession()
var camera : AVCaptureDevice?
var cameraPreviewLayer : AVCaptureVideoPreviewLayer?
var cameraCaptureOutput : AVCapturePhotoOutput

我很困惑,因为我在不同的相机应用程序中使用了完全相同的代码,但无论出于何种原因,我收到一条错误消息,指出 viewcontroller 没有初始化程序。我该如何修复错误?

最佳答案

问题出在这里:

var cameraCaptureOutput : AVCapturePhotoOutput

因为不是可选的,所以必须是 initialized :

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition.

为了解决这个问题,你可以将这样的属性定义为可选的:

class ViewController3: UIViewController {
let session = AVCaptureSession()
var camera : AVCaptureDevice?
var cameraPreviewLayer : AVCaptureVideoPreviewLayer?
var cameraCaptureOutput : AVCapturePhotoOutput?
}

关于swift - View Controller 没有初始化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48592711/

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