来自'[PFObject]?' '[PFObject]'只展开选项

时间:2015-11-28 13:10:44

标签: ios swift parse-platform downcast

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

以下错误从'[PFObject]发生Downcast?' '[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)")
            }
        }

        
        
        
        
                }

1 个答案:

答案 0 :(得分:0)

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 }   
}