【发布时间】:2019-03-28 06:35:29
【问题描述】:
我在下面定义了 Dictionary in an Array 对象:
var dataList : [[String:Any]]?
然后我将一些数据加载到其中。这是我在下面运行此代码时的输出:
print("DATA LIST:\n \(myRecordList)")
输出:
Optional([["itemCode": 0, "itemText": please select city], ["itemCode": 1, "itemText": City A], ["itemCode": 2, "itemText": City B], ["itemCode": 3, "itemText": City C], ["itemCode": 4, "itemText": City D], ["itemCode": 5, "itemText": City E], ["itemCode": 6, "itemText": City F]])
我想从数组中的第一项获取字典的值:
如您所知,这里是数组中的第一项:
["itemCode": 0, "itemText": 请选择城市]
这是上面字典中的值:
"itemText": 请选择城市
如果dataList计数不为零,我想将上面的文本(“请选择城市”)设置为按钮标题:
if dataList?.count != 0
{
**//need help in here!!!**
{
self.btnSelectMarkaOutlet.setTitle(tempBtnTitle, for: .normal)
}
}
我已经尝试过以下范围。但它粉碎了:
if let newTry = dataList![0]["itemText"] as? [String]
{
print("here is the output:: \(newTry[0])")
}
这是错误:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
让 tempBtnTitle = 必须是“请选择城市” 我想这样做。
让我分享整个视图控制器文件。因为 dataList 是 nil。
import UIKit
class SinyalSeviyesineGoreAramaYapViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var btnSelectMarkaOutlet: UIButton!
@IBOutlet weak var pickerViewOutlet: UIPickerView!
var dataList : [[String:Any]]?
override func viewDidLoad() {
print("viewDidLoad")
super.viewDidLoad()
self.pickerViewOutlet.delegate = self
self.pickerViewOutlet.dataSource = self
self.loadSayacRecordsForTheUIPickerView()
print("Is the data list nil? : \(self.dataList)")
if let newTry = self.dataList![0]["itemText"] as? String
{
print("here is the output:: \(newTry)")
}
//I WANTED TO SET BUTTON TITLE !!
let tempButtonTitle = ""
self.btnSelectMarkaOutlet.setTitle(tempButtonTitle, for: .normal)
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let recordName = self.dataList![row]
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//return self.cityList![row]
let info = self.dataList![row]
if let name = info["itemCode"] as? String,
let code = info["itemText"] as? String
{
let text = "\(name) - \(code)"
//print("am i here? \(text)")
return text
}
return nil
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.dataList == nil ? 0 : self.dataList!.count
}
func loadSayacRecordsForTheUIPickerView()
{
print("loadSayacRecordsForTheUIPickerView method is called")
ServiceManager.oServiceManager.loadSayacRecords()
{
(result) in
if let sayacRecords = try? JSONSerialization.jsonObject(with: result!, options: .mutableContainers) as? [String:Any]
{
if let resultCodeFromWebApi = sayacRecords?["resultCode"] as? String
{
DispatchQueue.main.async
{
print("loadSayacRecordsForTheUIPickerView resultCode: \(resultCodeFromWebApi)")
if resultCodeFromWebApi == "999"
{
if let myRecordList = sayacRecords?["recordList"] as? [[String:Any]]
{
//DispatchQueue.main.async
//{
self.dataList = myRecordList
print("DATA LIST:\n \(self.dataList)")
self.pickerViewOutlet.reloadAllComponents()
//}
}
} // resultCodeFromWebApi == 999 ENDS
else
{
if let resultMessageFromWebApi = sayacRecords?["resultMessage"] as? String
{
print("resultMessage: \(resultMessageFromWebApi)")
}
}
} // DispatchQueue.main.async ENDS
}
else
{
print("cant parse it")
} // if let resultCodeFromWebApi = loginResult?["resultCode"] as? Int ENDS
}
}
} // loadSayacRecordsForTheUIPickerView ENDS
} // class SinyalSeviyesineGoreAramaYapViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource ENDS
【问题讨论】:
-
dataList显然是零,你需要as? String而不是as? [String]。 -
亲爱的@Sweeper dataList 是零。这就是我共享整个 viewController 文件的原因。你能看看为什么dataList是nil吗?我在 'print("Is the data list nil? : (self.dataList)")'之前调用 loadSayacRecordsForTheUIPickerView()
-
为什么这个问题用 swift5 标记?在我看来,您在异步调用完成之前正在访问
dataList。 -
Swift 5 是个错误。谢谢。我在 loadSayacRecordsForTheUIPickerView() 函数中使用了 DispatchQueue.main.async。 DispatchQueue.main.async 对于异步问题不是很好吗?我该如何解决这个问题?
-
您应该使用完成处理程序(闭包)来更新您的按钮,例如 this question