gpt4 book ai didi

xcode - swift 错误 : class RecordSoundsViewController has no initializers

转载 作者:行者123 更新时间:2023-11-28 05:30:34 26 4
gpt4 key购买 nike

class RecordSoundsViewController: UIViewController, AVAudioRecorderDelegate{

@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var recodinginProgress: UILabel!
@IBOutlet weak var stopButton: UIButton!

var audioPlayer: AVAudioPlayer!
var recordAudio: RecordedAudio!
var audioRecorder:AVAudioRecorder

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
//hide the stop button
stopButton.hidden = true
recordButton.enabled = true
}

@IBAction func recordAudio(sender: UIButton) {
recordButton.enabled = false
stopButton.hidden = false
recodinginProgress.hidden = false
//TODO: Record the user"s voice
let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
.UserDomainMask, true)[0] as String
let currentDateTime = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "ddMMyyyy-HHmmss"
let recordingName = formatter.stringFromDate(currentDateTime)+".wav"
let pathArray = [dirPath, recordingName]
let filePath = NSURL.fileURLWithPathComponents(pathArray)
println(filePath)

var session = AVAudioSession.sharedInstance()
session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
audioRecorder = AVAudioRecorder(URL: filePath, settings: nil, error: nil)
audioRecorder.delegate = self
audioRecorder.meteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()

}

我不知道我做错了什么。我的错误出现在我的类里面。

它说

class RecordSoundsViewController has no initializers on RecordSoundsViewController: UIViewController, AVAudioRecorderDelegate{

最佳答案

编译器的错误信息有点糟糕。您看到错误的原因是因为您有一个没有默认值的属性。

在 Swift 中,所有值都需要有一个默认值,除非它是可选的。在你的情况下,它是这个属性:var audioRecorder:AVAudioRecorder

在您的情况下,我会将此属性设为可选:var audioRecorder:AVAudioRecorder? 并确保在使用时检查是否为 nil。或者让它成为一个隐式解包的可选(如果你知道总会有一个值):var audioRecorder:AVAudioRecorder!

关于xcode - swift 错误 : class RecordSoundsViewController has no initializers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28728747/

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