Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse.com in Swift - is it possible to cast retrieved PFObject as subclass?

I've created a subclass of PFObject, basically following instructions on parse.com docs, and pinned the object locally. The parse docs don't seem to go into retrieving a PFObject subclass, and I'm wondering - is it possible to cast the retrieved object as the PFObject subclass. If so, how?

(I understand if it's not possible, it could be necessary to re-instantiate the subclass, based on the retrieved properties of the PFObject.)

   let query = PFQuery(className:Armor.parseClassName())
    query.fromLocalDatastore()
    query.findObjectsInBackgroundWithBlock({
        (objects:[AnyObject]?, error: NSError?) in
        if let error = error {
            // There was an error
        } else {
            if let objects = objects as? [PFObject] {
                for object in objects {
                    //This println is outputting to the console:
                    println("PFObject object retrieved")
                    if let object = object as? Armor {
                          //This println is NOT outputting to the console:
                          println("PFObject object cast as Armor")
                    }
                }
            }
        }
    })
like image 541
Craig Grummitt Avatar asked May 17 '15 18:05

Craig Grummitt


1 Answers

Be sure you've registered subclass in application:didFinishLaunchingWithOptions:. In my case, it doesn't cast the retrieved object as the PFObject subclass.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    Armor.registerSubclass()

    Parse.enableLocalDatastore()
    Parse.setApplicationId(..., clientKey: ...)

    return true
}

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    CatsObject.registerSubclass()

    Parse.enableLocalDatastore()
    Parse.setApplicationId("...", clientKey: "...")

    return true
}

CatsObject.swift

import Foundation

class CatsObject: PFObject, PFSubclassing {
    static func parseClassName() -> String {
        return "Cat"
    }
}

CatViewController.swift

override func viewDidLoad() {
    queryData()
}

func queryData() {
    let query = PFQuery(className: CatsObject.parseClassName())
    query.fromLocalDatastore()
    query.findObjectsInBackgroundWithBlock({
        (objects:[AnyObject]?, error: NSError?) in
        if let error = error {
            // There was an error
        } else {
            println("count local objects = \(objects?.count)")
            if let objects = objects as? [PFObject] {
                for object in objects {
                    println("PFObject object retrieved")
                    if object is CatsObject {
                        println("object is CatsObject subclass")
                    }

                    if let object = object as? CatsObject {
                        println("PFObject object cast as CatsObject")
                    }
                }
            }
        }
    })
}

Console output

count local objects = Optional(10)
PFObject object retrieved
object is CatsObject subclass
PFObject object cast as CatsObject
like image 79
top.dev Avatar answered Nov 13 '22 12:11

top.dev