这是我对数组的初始化:
var restaurantsArray = [String]()
这里是查询函数:
func loadRestaurants(){
let fetchRestaurantsQuery = PFQuery(className: "Restaurants")
fetchRestaurantsQuery.findObjectsInBackgroundWithBlock{ (objects: [PFObject]?, error: NSError?) -> Void in
if error == nil{
//after successfull fetch
print("b")
if let fetchedRestaurants = objects{
print("c")
for fetchedRestaurant in fetchedRestaurants{
print("a")
self.restaurantsArray.append(fetchedRestaurant.objectForKey("Name") as! String)
print(fetchedRestaurant.objectForKey("Name") as! String)
}
}
}else{
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
}
由于某种原因,for 循环中的代码从未被调用。有什么建议可以解决这个问题吗?
最佳答案
自 objects
被声明为可选 [PFObject]?
正确的可选绑定(bind)语法只是
if let fetchedRestaurants = objects {
和fetchedRestaurant in fetchedRestaurant
很奇怪。
for fetchedRestaurant in fetchedRestaurants {
关于ios - 将 PFObject 查询到 swift 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36554118/