Parth Adroja
Parth Adroja

Reputation: 13514

Using AnyObject Type from Parse and access object in swift 3

In parse first i used to get AnyObject from its method and i can access all the objects from there using objectForKey method. But now AnyObject is changed to Any.

 PFCloud.callFunction(inBackground: apiName.rawValue, withParameters: paramsDict) { (object, error) in
            completion(object, error as? NSError)
        }

Now here object on completion in of Type Any and i access it like

(object as AnyObject).object(forKey: "cleanPref") as? Bool

which don't seem to be proper and if i use Any Type then there i need to do it like

if let object = object as? [String: Any] {
 if let pref = object["cleanPref"] as? Bool {
   }
}

Is is correct way to do it? Any other solution to keep the AnyObject then Any because there is lot of code needs to be changed if i migrate AnyObject to Any.

Upvotes: 1

Views: 314

Answers (1)

Rashwan L
Rashwan L

Reputation: 38833

Yes it´s better to do as you have done but, but I would have changed thing:

Instead of:

if let pref = object["cleanPref"] as? Bool {
}

Use:

guard let pref = object["cleanPref"] as? Bool else { // Some error }

You could also add multiple conditions:

guard let pref = object["cleanPref"] as? Bool,
      let test = object["test"] as? String else { // Some error }

Upvotes: 1

Related Questions