简体   繁体   English

在 swift 3 中使用 Parse 中的 AnyObject 类型和访问对象

[英]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.首先在解析中,我曾经从它的方法中获取AnyObject并且我可以使用 objectForKey 方法从那里访问所有对象。 But now AnyObject is changed to Any .但是现在AnyObject更改为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现在这里对象在类型 Any 中完成,我像访问它一样

(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这似乎不合适,如果我使用 Any Type 那么我需要这样做

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.保留 AnyObject 然后 Any 的任何其他解决方案,因为如果我将 AnyObject 迁移到 Any,则需要更改大量代码。

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 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM