gpt4 book ai didi

swift - 类 'viewController' 没有初始化器并且 'CurrentState' 无法构造,因为它没有可访问的初始化器

转载 作者:可可西里 更新时间:2023-11-01 02:23:02 26 4
gpt4 key购买 nike

为什么我会收到这 2 个错误?

"Class 'viewController' has no initializers"

"'CurrentState' cannot be constructed because it has no accessible initializers".

import UIKit
import Foundation
import AVFoundation


let minFramesForFilterToSettle = 10

enum CurrentState {
case statePaused
case stateSampling
}

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {

let session = AVCaptureSession()
var camera : AVCaptureDevice?
var validFrameCounter: Int = 0
var pulseDetector: PulseDetector
var filter: Filter
var currentState = CurrentState() // init error

override func viewDidLoad() {
super.viewDidLoad()
self.pulseDetector = PulseDetector()
self.filter = Filter()
// TO DO startCameraCapture() // call to un-used function. TO DO create function
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



}

最佳答案

CurrentState 是一个枚举,所以

var currentState = CurrentState()       // init error

应该是(例如)

var currentState = CurrentState.statePaused

和属性

var pulseDetector: PulseDetector
var filter: Filter

没有初始值。要么你把它改成类似的东西(我不知道这些类型,因此我不能说哪个是正确的初始化):

var pulseDetector = PulseDetector()
var filter = Filter()

或者你覆盖每个指定的初始化方法来赋值初始值,或者您将它们声明为(可能隐式展开)选项:

var pulseDetector: PulseDetector!
var filter: Filter!

关于swift - 类 'viewController' 没有初始化器并且 'CurrentState' 无法构造,因为它没有可访问的初始化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29763497/

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