Swift 4.2 //传递给不带参数的调用的参数

时间:2018-11-12 09:50:07

标签: ios swift xcode swift4

我写了代码(您可以在下面找到它),但是部分无效。我想创建一个应用程序,在这里我可以按下按钮pushMicButton,当按下按钮时,只需说出一些单词和SpeechKit即可,否则类似的东西将识别出该单词并将系统已识别的单词添加到一个认可的标签中。文本。我创建了这样的代码,但是在第43行中失败了。

您能帮我解决我的代码吗?

谢谢!

代码示例:

import UIKit
import Speech

class VoiceController: UIViewController {

    private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "se-SE"))

    @IBOutlet weak var recognizedLabel: UILabel!

    @IBAction func pushMicButton(_ sender: Any) {
        print("Voice recognition button")
        voiceRecognition()
    }

    @IBAction func AddToListButton(_ sender: UIButton) {
        print("Add something to list")
        getAddedData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()


    }

    func voiceRecognition() {

        SFSpeechRecognizer.requestAuthorization { _ in
            DispatchQueue.main.async {
                switch SFSpeechRecognizer.authorizationStatus() {
                case .authorized:

                    let audioURL = Bundle.main.url(forResource: "test", withExtension: "wav")!
                    let recognizer = SFSpeechRecognizer()
                    let request = SFSpeechRecognitionRequest(url: audioURL)
                    recognizer?.recognitionTask(with: request) { result, error in
                        guard error == nil else { print("Error: \(error)"); return }
                        guard let result else {print ("No result!"); return}
                        self.recognizedLabel.text = result.bestTranscription.formattedString
                    }
                    break
                default:
                    break
                }
            }
        }

        //ADD VOICE RECOGNITION HERE

    }

    func getAddedData() {
        let originalString = recognizedLabel.text
        let escapedString = originalString!.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
        let dataLink = "http://yabahi.st/items/find?name="
        let lookingURL = dataLink + escapedString!

        guard let url = URL(string: lookingURL) else {return}
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            guard let dataResponse = data,
                error == nil else {
                    print(error?.localizedDescription ?? "Response Error")
                    return }
            do{
                let jsonResponse = try JSONSerialization.jsonObject(with:
                    dataResponse, options: [])
                print(jsonResponse)

                //SEND "jsonResponse.name" TO MAIN UITABLEVIEW

            } catch let parsingError {
                print("Error", parsingError)
            }
        }
        task.resume()
    }

}

2 个答案:

答案 0 :(得分:0)

使用SFSpeechURLRecognitionRequest(url:)代替SFSpeechRecognitionRequest(url:)

并更改guard let result else { print ("No result!"); return }guard let结果= result else { print ("No result!"); return }

它应该解决此代码中的所有错误。

答案 1 :(得分:0)

voiceRecognition()方法应实现为:

func voiceRecognition() {
    SFSpeechRecognizer.requestAuthorization { _ in
        DispatchQueue.main.async {
            switch SFSpeechRecognizer.authorizationStatus() {
            case .authorized:

                guard let audioURL = Bundle.main.url(forResource: "test", withExtension: "wav") else {
                    return
                }
                let recognizer = SFSpeechRecognizer()
                let request = SFSpeechURLRecognitionRequest(url: audioURL)

                recognizer?.recognitionTask(with: request) { result, error in
                    if error != nil {
                        print("Error: \(error!)")
                        return
                    }

                    guard let result = result else { print ("No result!"); return }
                    self.recognizedLabel.text = result.bestTranscription.formattedString
                }

                break
            default:
                break
            }
        }
    }

    //ADD VOICE RECOGNITION HERE
}

现在这是区别:

1-为了安全起见,应避免用力audioURL展开,guard声明是个不错的选择。

2-如果您要创建语音识别请求(使用指定的URL初始化),则应使用SFSpeechURLRecognitionRequest而不是SFSpeechRecognitionRequest

3-似乎guard有误用...