Swift viewDidLoad中的变量集稍后将变为零

Swift viewDidLoad中的变量集稍后将变为零,swift,Swift,在viewDidLoad中设置变量后,我遇到了一个奇怪的问题,其中一个变量被设置为nil 我使用的是videoPreviewLayer AVPreview,有很多不同的会话队列,所以可能我没有正确理解某些内容。 我需要帮助诊断这个问题。 代码很长,但很简单。。任何帮助都将不胜感激。完整代码如下: 简短版本: var customRecordButton : RecordButton! var progressTimer : Timer! var progress : CGFloat! = 0

在viewDidLoad中设置变量后,我遇到了一个奇怪的问题,其中一个变量被设置为nil

我使用的是videoPreviewLayer AVPreview,有很多不同的会话队列,所以可能我没有正确理解某些内容。 我需要帮助诊断这个问题。 代码很长,但很简单。。任何帮助都将不胜感激。完整代码如下:

简短版本:

var customRecordButton : RecordButton!
var progressTimer : Timer!
var progress : CGFloat! = 0



override func viewDidLoad() {
    super.viewDidLoad()



    // custom recordButton https://github.com/samuelbeek/recordbutton
    let recordButtonRect = CGRect(x: 0, y: 0, width: 70, height: 70)
    let customRecordButton = RecordButton(frame: recordButtonRect)
    customRecordButton.center = self.view.center
    customRecordButton.progressColor = UIColor.red
    customRecordButton.closeWhenFinished = false
    customRecordButton.center.x = self.view.center.x
    customRecordButton.addTarget(self, action: #selector(self.record), for: .touchDown)
    customRecordButton.addTarget(self, action: #selector(self.stop), for: UIControlEvents.touchUpInside)
    self.view.addSubview(customRecordButton)
    }
func record() {
    self.progressTimer = Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
}

func updateProgress() {


    let maxDuration = CGFloat(5) // max duration of the recordButton

    progress = progress + (CGFloat(0.05) / maxDuration)
    customRecordButton.setProgress(progress) // (customRecordButton is always nil here)

    if progress >= 1 {
        progressTimer.invalidate()
    }

}
基本上,在updateProgress()中,customRecordButton始终为零

我试着浏览代码,在viewwill中,customRecordButton也是nil。我不确定我在这里使用的RecordButton类是否有问题: 我认为不应该有什么问题

编辑1:在viewDidLoad上方添加变量。我把它们放在那里只是忘了粘贴


编辑2:顺便说一句,record按钮出现在我的手机界面上,当我按下它时,它告诉我customRecordButton为零

尝试声明
让customRecordButton=RecordButton()
上面
viewDidLoad
然后在
viewDidLoad
中设置它
customRecordButton=RecordButton(框架:recordButtonRect)
很抱歉,我应该在原始答案中添加此选项。我确实在viewDidLoad上面设置了变量。我已经编辑了上面的代码
viewdidload
您有
var customRecordButton
并且您的内部有
let customRecordButton
,您的代码中是这样的吗?如果是,那你就有问题了。哇!我是一个白痴,谢谢你指出我需要声明
let customRecordButton=RecordButton()
在上面
viewDidLoad
然后在
viewDidLoad
中设置
customRecordButton=RecordButton(frame:recordButtonRect)
对不起,我应该在我的原始答案中添加这个。我确实在viewDidLoad上面设置了变量。我已经编辑了上面的代码
viewdidload
您有
var customRecordButton
并且您的内部有
let customRecordButton
,您的代码中是这样的吗?如果是,那你就有问题了。哇!我是个白痴,谢谢你指出我应该在我原来的答案中加上这个。我确实在viewDidLoad上面设置了变量是的,您可以在本地范围内的
viewDidLoad
中再次声明
customRecordButton
。删除
在那里。对不起,我应该在我的原始答案中添加这个。我确实在viewDidLoad上面设置了变量是的,您可以在本地范围内的
viewDidLoad
中再次声明
customRecordButton
。将该
移除,然后将其放在那里。
var customRecordButton: RecordButton?

override func viewDidLoad() {
    super.viewDidLoad()

    // custom recordButton https://github.com/samuelbeek/recordbutton
    let recordButtonRect = CGRect(x: 0, y: 0, width: 70, height: 70)
    customRecordButton = RecordButton(frame: recordButtonRect)

...

customRecordButton?.setProgress(progress)