Swift错误:类RecordSoundsViewController没有初始化程序

时间:2015-02-25 20:14:59

标签: 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()

}

我不知道自己做错了什么。我的错误出现在我的班上。

它说

  

类RecordSoundsViewController在RecordSoundsViewController上没有初始值设定项:UIViewController,AVAudioRecorderDelegate {

1 个答案:

答案 0 :(得分:2)

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

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

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