我試圖從後端獲取一些數據,然後將其分配給3個不同的數組。這些數組然後我想用來填充我的tableViewCells。問題是,當我在取塊的外面打印數組時,它們返回nil。當我在獲取塊中打印它們時,它們會返回我打算這樣做的對象的變量。如何填充這些數組以填充tableView?
我包含self.tableView.reloadData()行,希望數組被填充,然後填充tableViewCells,但它似乎並沒有工作。
歡迎任何有關如何正確填充這些數組的提示,以便在獲取請求之外打印它們時,它們會返回相應的數據。
var capArray = [String]()
var imageDic = [String: [PFFile]]()
var priceArray = [Int]()
override func viewDidAppear(animated: Bool) {
capArray.removeAll(keepCapacity: true)
imageDic.removeAll(keepCapacity: true)
priceArray.removeAll(keepCapacity: true)
let query = PFQuery(className: "SellerObject")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for o in objects {
if o.objectForKey("caption") != nil && o.objectForKey("imageFile") != nil && o.objectForKey("price") != nil {
let cap = o.objectForKey("caption") as? String
self.capArray.append(cap!)
let imdic = o.objectForKey("imageFile") as? [PFFile]
self.imageDic[cap!] = imdic
let price = o.objectForKey("price") as? String
let priceInt = Int(price!)
self.priceArray.append(priceInt!)
}
}
}
}
self.tableView.reloadData()
}
好的,這是有道理的。但現在,當我添加self.tableView.reloadData()應用程序崩潰時出現以下錯誤消息:致命錯誤:意外地發現零,同時解包可選值 –
您需要找出來自哪裏。在標題或問題中,字面意思是成千上萬的問題與「意外地發現零,同時解開可選值」。 – Gruntcakes
檢查這個線程:http://stackoverflow.com/questions/35683813/how-to-deal-with-fatal-error-unexpectedly-found-nil-while-unwrapping-an-option –