Parse.com in Swift - is it possible to get extracted PFObject as a subclass?

I created a subclass of PFObject, basically following the instructions of parse.com docs and bound the object locally. Analysis documents are not like getting a subclass of PFObject, and I'm wondering if it is possible to return the restored object as a subclass of PFObject. If so, how?

(I understand that if this is not possible, you may need to re-create a subclass based on the extracted PFObject properties.)

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") } } } } }) 
+5
source share
1 answer

Make sure you register the subclass in application:didFinishLaunchingWithOptions: In my case, it does not pass the extracted object as a subclass of PFObject.

 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 exit

 count local objects = Optional(10) PFObject object retrieved object is CatsObject subclass PFObject object cast as CatsObject 
+12
source

All Articles