我正在将解析查询保存到数组,但是如果让objects = objects as,我会出现以下错误? [PFObject]

并且发生以下错误:从[[PFObject]?到'[PFObject]'的唯一包装。

有人知道如何解决这个问题吗?



func getArray(funcstring: String){

        var userGeoPoint: PFGeoPoint
        PFGeoPoint.geoPointForCurrentLocationInBackground {
            (geoPoint: PFGeoPoint?, error: NSError?) -> Void in
            if error == nil {

            userGeoPoint = geoPoint!
            }
        }


        var searchQuery: [String] = [String]()

        var query = PFQuery(className:"User")
        query.whereKey("geoLocation", nearGeoPoint:userGeoPoint)
        query.limit = 100
        query.findObjectsInBackgroundWithBlock {
            (objects: [PFObject]?, error: NSError?) -> Void in
            if error == nil {
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        self.searchQuery.append(object.objectForKey("objectId") as! String)

                    }

                }
            } else {
                print("\(error?.userInfo)")
            }
        }





                }

最佳答案

objects被声明为[PFObject]?

您将把对象降级为编译器已经知道的东西。

只需检查nil

if let unwrappedObjects = objects {
  for object in unwrappedObjects {
     self.searchQuery.append(object.objectForKey("objectId") as! String)
  }
}


还是“笨拙”

if let unwrappedObjects = objects {
   self.searchQuery = unwrappedObjects.map{ $0["objectId"] as! String }
}

关于ios - 从“[[PFObject]]下调?到'[PFObject]'仅解开可选,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33971672/

10-14 11:32