从查询中访问PFObject子类属性

时间:2015-10-24 18:05:31

标签: parse-platform properties

我正在使用Parse并创建了PFObject的子类。创建对象时,它会使事情变得更容易。创建对象后,我正在尝试查询数据库并访问我创建的自定义属性。我发现的是,当我处理从查询返回的PFObject时,我不能使用点表示法来访问属性。这是正常的吗?

这是我创建的子类。

import Foundation
import UIKit
import Parse

class MessagePFObject: PFObject
{
    @NSManaged var messageSender : String
    @NSManaged var messageReceiver : String
    @NSManaged var messageMessage : String
    @NSManaged var messageSeen : Bool
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    // Custom Query method.
    override class func query() -> PFQuery?
    {
        let query = PFQuery(className: MessagePFObject.parseClassName())
        query.includeKey("user")
        query.orderByDescending("createdAt")

        return query
    }

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


    init(messageSenderInput: String?, messageReceiverInput: String?, messageMessageInput: String?)
    {

        super.init()

        self.messageSender = messageSenderInput!
        self.messageReceiver = messageReceiverInput!
        self.messageMessage = messageMessageInput!
        self.messageSeen = false     
    }

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    override init()
    {
        super.init()
    }
}

//++++++++++++++++++++++++++++++++++++ EXTENSION +++++++++++++++++++++++++++++++++++++++++++++++++++

extension MessagePFObject : PFSubclassing
{
    class func parseClassName() -> String
    {
        return "MessagePFObject"
    }

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    override class func initialize()
    {
        var onceToken: dispatch_once_t = 0
        dispatch_once(&onceToken) {
            self.registerSubclass()
        }
    }
}

以下是我的查询以及访问这些属性需要执行的操作。 createdAt,updatedAt等都有点表示法,但我的自定义属性都没有。你可以看到我使用element.objectForKey(“messageSeen”)访问messageSeen。

let predicate = NSPredicate(format: "messageSender == %@ OR messageReceiver == %@", self.currentUser!.username!, self.currentUser!.username!)
        let query = messagePFObject.queryWithPredicate(predicate)
        query!.findObjectsInBackgroundWithBlock ({ (objects, error) -> Void in

            if error == nil && objects!.count > 0
            {
                for element in objects!
                {
                    print(element)
                    print(element.parseClassName)
                    print(element.objectId)
                    print(element.createdAt)
                    print(element.updatedAt)
                    print(element.objectForKey("messageSeen"))
                }
            }
            else if error != nil
            {
                print(error)
            }
        })

如果这是正常的那么这很好。我只是想确保我没有错过任何东西。

小心,

乔恩

1 个答案:

答案 0 :(得分:0)

您的对象子类必须实现PFSubclassing协议,您需要在应用代理中调用MessagePFObject.registerSubclass()

解析文档非常好:https://parse.com/docs/ios/guide#objects-subclasses