将PFObjects查询到swift数组中

时间:2016-04-11 16:20:09

标签: ios swift parse-platform pfquery pfobject

这是我对阵列的初始化:

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循环中的代码。有什么建议可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

由于objects被声明为可选[PFObject]?,因此正确的可选绑定语法只是

if let fetchedRestaurants = objects {

fetchedRestaurant in fetchedRestaurant非常奇怪。

for fetchedRestaurant in fetchedRestaurants {