Swift 如何修复Xcode错误:类型为';AVAudioRecorder?&x27;没有成员';URL';

Swift 如何修复Xcode错误:类型为';AVAudioRecorder?&x27;没有成员';URL';,swift,xcode,Swift,Xcode,我是一名Udacity iOS开发者学生,正在开发一款可以录制和播放音频的应用程序PitchPerfect。我正在使用Xcode 10.2.1以及模拟器和Swift 4。我一直在密切关注课堂教学,但我的RecordSoundsViewController.swift中的代码出现错误。我的身材一直不好。我收到以下错误: “AVAudioRecorder”类型的值没有成员“URL” 我会喜欢你关于如何前进的建议,并让我的构建进行编译 我尝试更新我的Xcode版本(到我正在运行的当前版本,10.2.1

我是一名Udacity iOS开发者学生,正在开发一款可以录制和播放音频的应用程序PitchPerfect。我正在使用Xcode 10.2.1以及模拟器和Swift 4。我一直在密切关注课堂教学,但我的RecordSoundsViewController.swift中的代码出现错误。我的身材一直不好。我收到以下错误:

“AVAudioRecorder”类型的值没有成员“URL”

我会喜欢你关于如何前进的建议,并让我的构建进行编译

我尝试更新我的Xcode版本(到我正在运行的当前版本,10.2.1)并切换到Swift 4.2,但似乎没有帮助

//
//  RecordSoundsViewController.swift
//  PitchPerfect
//
//  Created by Shara Karasic on 6/1/19.
//  Copyright © 2019 Shara Karasic. All rights reserved.
//

import UIKit
import AVFoundation

class RecordSoundsViewController: UIViewController,       AVAudioRecorderDelegate {

    var audioRecorder: AVAudioRecorder!
    var recordedAudioURL:URL!

    @IBOutlet weak var recordingLabel: UILabel!
    @IBOutlet weak var recordButton: UIButton!
    @IBOutlet weak var stopRecordingButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        stopRecordingButton.isEnabled = false
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print ("viewWillAppear called")
    }

    @IBAction func recordAudio(_ sender: AnyObject) {
        recordingLabel.text = "Recording in progress"
        stopRecordingButton.isEnabled = true
        recordButton.isEnabled = false

        let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0] as String
        let recordingName = "recordedVoice.wav"
        let pathArray = [dirPath, recordingName]
        let filePath = URL(string: pathArray.joined(separator: "/"))

        let session = AVAudioSession.sharedInstance()
        try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeDefault, options: AVAudioSession.CategoryOptions.defaultToSpeaker)

        try! audioRecorder = AVAudioRecorder(url: filePath!, settings: [:])
        audioRecorder.isMeteringEnabled = true
        audioRecorder.prepareToRecord()
        audioRecorder.record()
    }
    @IBAction func stopRecording(_ sender: Any) {
        recordButton.isEnabled = true
        stopRecordingButton.isEnabled = false
        recordingLabel.text = "Tap to Record"
        audioRecorder.stop()
        let audioSession = AVAudioSession.sharedInstance()
        try! audioSession.setActive(false)
    }
    func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
        if flag {
            performSegue(withIdentifier: "stopRecording", sender: audioRecorder.URL)
        } else {
            print ("recording was not successful")
        }
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "stopRecording" {
            let playSoundsVC = segue.destination as! PlaySoundsViewController
            let recordedAudioURL  = audioRecorder.url
            playSoundsVC.recordedAudioURL = recordedAudioURL
        }
    }
}
我希望这段代码能够顺利运行,尤其是我直接从Udacity课程中获得的代码,但我的构建失败了,我得到了错误:


“AVAudioRecorder”类型的值没有成员“URL”

您的代码似乎已过时,
URL
是小写的,请使用传递的
recorder
参数

performSegue(withIdentifier: "stopRecording", sender: recorder.url)
读这本书总是值得的


这四行根本不起作用(
URL(string
是不合适的),这是一种连接路径的可怕方式

let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)[0] as String
let recordingName = "recordedVoice.wav"
let pathArray = [dirPath, recordingName]
let filePath = URL(string: pathArray.joined(separator: "/"))
换成

let dirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let fileURL = dirURL.appendingPathComponent("recordedVoice.wav")

...

try! audioRecorder = AVAudioRecorder(url: fileURL, settings: [:])

旁注:不鼓励
尝试!
除非代码无法崩溃(如
文件管理器中的
行)

提示:案例很重要。我尝试了上述方法,但仍然得到相同的错误:-(你的意思是删除原来的四行,只替换为你的两行,然后用你的新行替换try!audioRecorder行吗?如果你使用
recorder.url
错误必须消失。是的,用my 2替换原来的四行,并在
audioRecorder
行中使用
fileURL
。哦,我通过更改来修复它audioRecorder.URL中的“URL”在两个地方小写。然后构建成功。谢谢!