xcode - swift 错误 : class RecordSoundsViewController has no initializers

标签 xcode multithreading swift exc-bad-instruction

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/

相关文章:

iOS 扩展 - 致命异常 : com. firebase.core 默认应用已配置

ios - 如何使用swift从字典中检索数据并存储在字符串类型中?

ios - 如何跟踪 Sprite Kit 中的动画

ios - 如何在 objective-c 中用边界线填充 ImageView 的颜色部分

c++ - 加入线程时无限循环

停止 Java 程序中间进程的 Java 紧急按钮

ios - 在主 ui 线程中加载 Realm 对象是否可以接受?

swift - 找不到框架错误

ios - NSBatchUpdateRequest 在 Swift 中引发错误

ios - 如何在工作区中使用子项目的标题? [图片]