这是我对阵列的初始化:
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循环中的代码。有什么建议可以解决这个问题吗?
答案 0 :(得分:0)
由于objects
被声明为可选[PFObject]?
,因此正确的可选绑定语法只是
if let fetchedRestaurants = objects {
和fetchedRestaurant in fetchedRestaurant
非常奇怪。
for fetchedRestaurant in fetchedRestaurants {