简体   繁体   中英

Cannot invoke function with an argument list of type '()'

I'm new to IOS development. The constructor of AVAudioRecorder is AVAudioRecorder(URL: filePath, settings: nil, error: nil) in the resource that I'm following but I'm coding in Swift 2 which has different constructor.

The error Cannot invoke function with an argument list of type '()' occurs on " try audioRecorder = AVAudioRecorder(URL: filePath, settings: nil) " line

This is my record audio 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()
}

The way of initializing AVAudioRecorder using its initializer in Swift 2 is to leave out the parameter of type NSErrorPointer and catch it:

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

您丢失了录音机初始化程序的错误部分:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM