[英]How can I sort this Array in Swift 3 ? ( Picker View ) ( Core Data )
無需每次在titleForRow
中對數組進行排序, titleForRow
需要在初始化stores
數組后對其進行一次排序。
stores.sort { $0.name < $1.name }
現在stores
數組是按name屬性排序的,現在只需從titleForRow
方法返回名稱titleForRow
。
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return stores.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return stores[row].name
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(stores[row].name)
}
Swift 3.1:
var arr = ["aaa","zz","ddd","mnm","zzzz"]
var newArr = arr.sorted()
print(newArr)
//log: ["aaa", "ddd", "mnm", "zz", "zzzz"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.