2015-05-17 10 views
5

Ho creato una sottoclasse di PFObject, fondamentalmente seguendo le istruzioni su , e ho bloccato l'oggetto localmente. I documenti di analisi non sembrano andare nel recupero di una sottoclasse PFObject, e mi chiedo - è possibile lanciare l'oggetto recuperato come sottoclasse PFObject. Se é cosi, come?parse.com in Swift - è possibile eseguire il cast di PFObject recuperato come sottoclasse?

(ho capito se non è possibile, potrebbe essere necessario ri-creare un'istanza della sottoclasse, in base alle proprietà recuperate del 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") 
        } 
       } 
      } 
     } 
    }) 
+0

Hai provato a fare 'se l'oggetto è Armour {...}' ' – sbarow

+0

(oggetto è Armour)' è uguale a false. –

+0

Che versione di libreria di Parse stai usando? – siegy22

risposta

12

assicurati di aver sottoclasse registrato application:didFinishLaunchingWithOptions:. Nel mio caso, non lancia l'oggetto recuperato come sottoclasse 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") 
        } 
       } 
      } 
     } 
    }) 
} 

uscita Console

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

Sto seguendo le istruzioni e il codice di esempio nella sezione sottoclassi dei documenti parse.com, che chiama registerSubclass() nel metodo di inizializzazione. Vado a dare un suggerimento, invece di registrare la sottoclasse in AppDelegate. Quando recuperi il tuo oggetto, puoi lanciarlo come sottoclasse piuttosto che come PFObject? –

+2

Ho testato la tua teoria e hai ragione. La registrazione della sottoclasse nel delegato dell'applicazione sembra funzionare, mentre la registrazione nel metodo di inizializzazione come per il codice di esempio nei documenti non lo fa! –

+0

Cool, sono nel processo di modifica del mio codice –

Problemi correlati