如何排序 AVSpeechSynthesisVoice.speechVoices()
How to sort AVSpeechSynthesisVoice.speechVoices()
我是初学者,在处理此问题时,我注意到选择器视图未排序
很明显汉语没有排在土耳其语之后,那我该如何排序呢?几个小时以来,我一直在查看我的代码,但似乎无法弄清楚如何对其进行排序。这是我的完整代码。
谢谢
import AVFoundation
import UIKit
class SettingsView: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet var pickerView: UIPickerView!
var voicesArray: [String] = []
let speechVoices = AVSpeechSynthesisVoice.speechVoices()
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return speechVoices.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let language = Locale.current.localizedString(forLanguageCode: speechVoices[row].language)!
let languageCode = speechVoices[row].language
var regionCode:String = ""
for region in Locale.isoRegionCodes {
// Test if provided region codes has voices or not
if languageCode.contains(region) {
regionCode = region
}
}
let country = Locale.current.localizedString(forRegionCode: String(regionCode))
return "\(language) (\(country ?? ""))"
}
@IBAction func done(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}
中文 (zh) 肯定排在土耳其 (tr) 之后,因此这些似乎已按区域代码排序。如果您想按其他方式排序(我假设它们在语言环境中的显示名称),那么您可以在为选择器设置元素列表时对它们进行排序。例如:
let speechVoices = AVSpeechSynthesisVoice.speechVoices().sorted(by: {
Locale.current.localizedString(forLanguageCode: [=10=].language)! < Locale.current.localizedString(forLanguageCode: .language)!
})
我是初学者,在处理此问题时,我注意到选择器视图未排序
很明显汉语没有排在土耳其语之后,那我该如何排序呢?几个小时以来,我一直在查看我的代码,但似乎无法弄清楚如何对其进行排序。这是我的完整代码。
谢谢
import AVFoundation
import UIKit
class SettingsView: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet var pickerView: UIPickerView!
var voicesArray: [String] = []
let speechVoices = AVSpeechSynthesisVoice.speechVoices()
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return speechVoices.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let language = Locale.current.localizedString(forLanguageCode: speechVoices[row].language)!
let languageCode = speechVoices[row].language
var regionCode:String = ""
for region in Locale.isoRegionCodes {
// Test if provided region codes has voices or not
if languageCode.contains(region) {
regionCode = region
}
}
let country = Locale.current.localizedString(forRegionCode: String(regionCode))
return "\(language) (\(country ?? ""))"
}
@IBAction func done(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}
中文 (zh) 肯定排在土耳其 (tr) 之后,因此这些似乎已按区域代码排序。如果您想按其他方式排序(我假设它们在语言环境中的显示名称),那么您可以在为选择器设置元素列表时对它们进行排序。例如:
let speechVoices = AVSpeechSynthesisVoice.speechVoices().sorted(by: {
Locale.current.localizedString(forLanguageCode: [=10=].language)! < Locale.current.localizedString(forLanguageCode: .language)!
})