Search code examples
swiftavaudiorecorderswift2initializer

Invoking initializer for AVAudioRecorder - Swift 2.0


This seems to be an issue that I am having with the transition to Swift 2.0:

I am trying to initialize my AVAudioRecorder and the settings parameter (which I used to give nil) will not accept this anymore. Thoughts?

*var session = AVAudioSession.sharedInstance()
        do{
            try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
        } catch{
        }
        audioRecorder = AVAudioRecorder(URL: filePath, settings: nil)
        audioRecorder.meteringEnabled = true
        audioRecorder.prepareToRecord()
        audioRecorder.record()*

It gives me the error: **

"Cannot invoke initializer for type 'AVAudioRecorder' with an argument list type '(URL: NSURL, settings:nil)'."

**

filePath is of type NSURL by the way. Thank you everyone for your help!


Solution

  • When you have a problem like this, look at the declaration in the header:

    init(URL url: NSURL, settings: [String : AnyObject]) throws
    

    As you can see clearly from that declaration, just about everything about this line is wrong:

    audioRecorder = AVAudioRecorder(URL: filePath, settings: nil)
    
    • You cannot supply nil settings. Pass an empty dictionary if you have no settings.

    • This is a throws initializer; you must say try and enclose it in a do/catch structure.