-1
我有一個dataTask在一個單獨的迅速類文件檢索某些數據到一個數組工作:解析JSON並不首次
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSArray {
let resourceIDs = NSMutableArray()
var result = NSArray()
for i in 0 ..< jsonResult.count {
let jsonElement : NSDictionary = jsonResult[i] as! NSDictionary
let resourceID = jsonElement["ResourceID"]!
resourceIDs.add(resourceID)
}
result = resourceIDs
print(result) // data is stored
VC.resourceIDs = result //pass the object's NSArray to the ViewController's NSArray
}
else {
print("Could not parse JSON!")
}
當我嘗試打電話給我的按鈕功能首次,我的ViewController的NSArray resourceIDs
沒有收到任何東西。當我再次調用我的按鈕功能時,數組將被填充。
@IBAction func btnCheckAvailability(_ sender: UIButton) {
showOverlayOnTask(message: "Please wait...")
let dateFormatter = DateFormatter()
let timeFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
timeFormatter.dateFormat = "HH:mm:ss"
let date = dateFormatter.string(from: startDate)
let time = timeFormatter.string(from: startDate)
CheckAvailableModel().checkAvailable(resourceType: resourceType!, startDate: date, startTime: time, VC: self)
if (resourceIDs.count > 0) {
DispatchQueue.main.async {
self.dismiss(animated: false, completion: { action in
print(self.resourceIDs)
})
}
}
else {
DispatchQueue.main.async {
self.dismiss(animated: false, completion: { action in
print("No available resources")
})
}
}
}
當我第一次調用按鈕函數時,如何從我的視圖控制器中獲取數組?
爲什麼在Swift中使用NS [Mutable] Array和NS [Mutable] Dictionary來代替Swift本地數組和字典? – rmaddy