gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:01:55 25 4
gpt4 key购买 nike

我编写了代码(您可以在下面找到它),但它部分不起作用。我想创建一个应用程序,我可以在其中按下按钮 pushMicButton ,按下按钮时只需说出一些单词,SpeechKit 或类似的东西就会识别它并将系统已识别的单词添加到一个可识别的Label.text。我创建了这样的代码,但在第 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()
}

}

最佳答案

使用SFSpeechURLRecognitionRequest(url:)而不是SFSpeechRecognitionRequest(url:)

并更改guard let result else { print ("没有结果!");返回}to guard letresult= result else { print ("没有结果!");返回}

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

关于ios - Swift 4.2//参数传递给不带参数的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53259539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com