gpt4 book ai didi

json - 我无法从数据库加载数据来显示 UIPickerView 的结果

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

我无法从数据库加载数据来显示 UIPickerView 的结果。我通过修复数组值进行测试 UIPicker 可以将数据加载到 pickerView 但当我使用数组加载 json UIPickerViewDataSource 无法加载到选取器 View

可以加载

var team = ["Apple", "Banana", "Tomato", "Corn", "Bean", "Orange", "Mango", "Mangoteen"]

无法加载

var team: [AnyObject] = []


import UIKit

class getAllTeamViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate, UITextFieldDelegate {

var selectValue: String?

@IBOutlet weak var showSelectValuePickerView: UITextField!

// Array
//var team = ["Apple", "Banana", "Tomato", "Corn", "Bean", "Orange", "Mango", "Mangoteen"]
var team: [AnyObject] = []

//Function getValue from database
func getvalue() -> Void {
//get the values from sql/Json
let url = NSURL(string: "http://www.test.com/myWebService/getteamPickView.php")

let data = NSData(contentsOf: url! as URL)
var jsonResponse = try! JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
jsonResponse = jsonResponse.reversed() as NSArray
reloadInputViews()
print("jsonResponse ==> \(jsonResponse)")

for candidate in jsonResponse {
if let cdict = candidate as? NSDictionary {
//fullName is the column name in sql/json
let names = cdict["NameTeam"]
self.selectValue?.append(((names! as AnyObject) as! String))
print("Names ==> \(String(describing: names))")
}//if
}//for
}//getvalue

// Picker View Function
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.team.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let titleRow = (team[row] )
return titleRow as? String
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

if team.count > 0 && team.count >= row{
self.showSelectValuePickerView.text = self.team[row] as? String
self.showSelectValuePickerView.isHidden = true
}
}

}

最佳答案

抱歉,这是非常糟糕的代码。有很多不好的做法。

  • 如果数组明显是 [String],请勿将其声明为 [AnyObject]
    这避免了像名称这样可怕的类型转换!作为 AnyObject)作为!字符串
  • 不要在 Swift 中使用 NSDataNSURLNSArrayNSDictionary。使用原生类型。
  • .mutableContainers 在 Swift 中没有意义,省略整个参数。
  • 从不使用 Data(contentsOf) 从远程 URL 同步加载数据。
  • 始终在主线程上重新加载 UI。
  • 处理所有错误!至少打印它们。

发生错误的原因是您在填充数据源数组之前调用了 reloadInputViews()(我假设它会重新加载选取器 View )。

并且不是填充数据源数组team,而是将字符串附加到selectValue,这是没有意义的。

这是代码的稍微优化版本

@IBOutlet weak var showSelectValuePickerView: UITextField!

var team = [String]()

//Function getValue from database
func getvalue() {
//get the values from sql/Json
let url = URL(string: "http://www.test.com/myWebService/getteamPickView.php")!
let task = URLSession.shared.dataTask(with: url) { (data, _ , error) in
if let error = error { print(error); return }
do {
if let jsonResponse = try JSONSerialization.jsonObject(with: data!) as? [[String:Any]] {
self.team = jsonResponse.compactMap{ $0["NameTeam"] as? String }
DispatchQueue.main.async {
self.reloadInputViews()
}
} else { print("JSON is not an array") }
} catch { print(error) }
}
task.resume()
}

// Picker View Function
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.team.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return team[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.showSelectValuePickerView.text = self.team[row]
self.showSelectValuePickerView.isHidden = true // Are you serious ??? This hides the text field
}

关于json - 我无法从数据库加载数据来显示 UIPickerView 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56339354/

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