当使用Parsse SDK保存一个新对象时,如何获得createAt属性?

问题描述 投票:0回答:1

我正在创建一个新的对象,由于某些原因,我需要得到完整的保存对象,包括createdAt和updateAt属性。

在这段代码中,我正在保存新对象。

        let msg = PFObject(className: "ChatMessage")
        msg["User"] = user
        msg["Message"] = message
        msg["Topic"] = topic
        //messageList.append(message)
        msg.saveInBackground { (success, error) in
            if error == nil {
                //msg.createdAt is nil
            }
        }

我如何才能在不进行另一个查询的情况下获得完整的保存对象?

编辑

这是变量值

我可以看到 "createdAt "的值在"_pfinternal_state "对象中是可用的,但在"_estimatedData "中却没有。

enter image description here

有什么方法可以让我访问_pfinternal_state._createdAt?

有什么办法可以访问_pfinternal_state._createdAt?

swift parse-platform
1个回答
0
投票

我花了很多时间为这个小问题找到了一个解决方案。

你可以简单地通过调用 "value(forKey:... "函数来获得createAt和updateAt的值,并将返回的值投向NSDate,如下一段代码。

        let msg = PFObject(className: "ChatMessage")
        msg["User"] = user
        msg["Message"] = message
        msg["Topic"] = topic
        //messageList.append(message)
        msg.saveInBackground { (success, error) in
            if error == nil {
                //msg.createdAt is nil
                let createdAt = msg.value(forKey: "createdAt") as! NSDate 
                // this will return the NSDate object. 
                //   You must cast it to NSDate not Date

            }
        }

© www.soinside.com 2019 - 2024. All rights reserved.