Can't call function with argument list like '()'

I am new to IOS development. The AVAudioRecorder constructor AVAudioRecorder(URL: filePath, settings: nil, error: nil)

in the resource I am following, but I am coding in Swift 2 which has a different constructor.

Error Unable to call a function with a list of arguments of type "()" on the string " try audioRecorder = AVAudioRecorder(URL: filePath, settings: nil)

"

This is my sound file:

@IBAction func recordAudio(sender: UIButton) {
    stopButton.hidden = false
    recordButton.enabled = false
    recordingInProgress.hidden = false
    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)
    print(filePath)

    var session = AVAudioSession.sharedInstance()
    do {
        try session.setCategory(AVAudioSessionCategoryPlayAndRecord)

    } catch {}

    do {
        try audioRecorder = AVAudioRecorder(URL: filePath, settings: nil)
    } catch {}

    audioRecorder.meteringEnabled = true
    audioRecorder.prepareToRecord()
    audioRecorder.record()
}

      

+3


source to share


2 answers


The way to initialize AVAudioRecorder

using its initializer in Swift 2 is to leave the type parameter NSErrorPointer

and catch it:



do 
{
    audioRecorder = try AVAudioRecorder(URL:filePath, settings:[:])
} 
catch let error as NSError
{
    printf(error.description)
}

      

+1


source


You're missing part of the audio recording initializer error:



audioRecorder = AVAudioRecorder(URL: filePath, settings: nil, error: nil)

0


source







All Articles