6

With swift 1.2, I can no longer retrieve an array of poiter with parse subclass and downcasting it with another parse subclass.

I always found the error:

fatal error: NSArray element failed to match the Swift Array Element type

Do you have an idea or it may come?

The code:

import Foundation

class ShotModel : PFObject, PFSubclassing {

    /**
    * MARK: Properties
    */
    @NSManaged var name: String

    @NSManaged var pics: [PicModel]


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

    class func parseClassName() -> String! {
        return "Shot"
    }

}

import Foundation

class PicModel : PFObject, PFSubclassing {

    /**
    * MARK: Properties
    */
    @NSManaged var name: String


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

    class func parseClassName() -> String! {
        return "Pic"
    }

}

// this cause error

var shot: ShotModel = // a shot model get with fetchInBackgroundWithBlock

shot.pics // fatal error: NSArray element failed to match the Swift Array Element type

Thanks for your time

Antoine Lenoir
  • 524
  • 1
  • 3
  • 17
  • Cast `shot` to `PFObject` instead of `ShotModel` then you can get `shot.valueForKey("pics")`. Try once it worked for me. – Rahul Mane Mar 13 '15 at 10:08
  • Yes i have access to `shot.valueForKey("pics")` with cast `ShotModel` but i can't downcast `shot.valueForKey("pics")` to `[PicModel]` – Antoine Lenoir Mar 13 '15 at 10:34
  • Try to change `@NSManaged var pics: [PicModel]` to ` @NSManaged var pics: [**PFObject**]` I had same issue solved it by casting to `PFObject` – Rahul Mane Mar 13 '15 at 10:42
  • ok, but how can get an array of PicModel objects ? I tried but i have en other error when i make this : `pis as! PicModel` => `Could not cast value of type 'PFObject' to 'PicModel'` ... – Antoine Lenoir Mar 13 '15 at 10:51
  • Found similar post on Github - https://github.com/burczyk/ParseCollectionsSwiftBug. May be that could help you. – Rahul Mane Mar 13 '15 at 10:56

4 Answers4

15

The problem come from this part of code :

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

registerSubclass() for ShotModel is called before registerSubclass() for PicModel.

I've resolved with this in AppDelegate :

PicModel.registerSubclass()
ShotModel.registerSubclass()
Antoine Lenoir
  • 524
  • 1
  • 3
  • 17
  • This wasn't working for me until I put the .registerSubclass() calls before Parse Initialization. Now it works great. Frustrating bug though. Thanks! – Rick Roberts Apr 29 '15 at 04:58
  • I solved it by adding PicModel.initialize() on the first line of the initialisation of ShotModel – zirinisp May 06 '15 at 16:01
  • 1
    I actually filed a bug (https://developers.facebook.com/bugs/1452388645073907) against parse for this very same reason and they ended up updating their documentation with the following: Please note that the initialize method is not called until the class receives its first message, meaning that you need to call any instance or class method on your subclass before it will be registered with Parse SDK.` – Julian B. May 28 '15 at 14:38
  • Thank you. this definitely helped. – RyanPliske Sep 27 '15 at 19:32
4

The problem lies to the fact that ShotModel is registered as a subclass before PicModel. To invert that we can call PicModel initialisation the initialisation of ShotModel.

This way we keep the suggested solution by parse and make sure that classes are registered in the correct order.

class ShotModel : PFObject, PFSubclassing {

    /**
    * MARK: Properties
    */
    @NSManaged var name: String

    @NSManaged var pics: [PicModel]


    override class func initialize() {
        var onceToken : dispatch_once_t = 0;
        dispatch_once(&onceToken) {
            PicModel.initialize()
            self.registerSubclass()
        }
    }
zirinisp
  • 9,971
  • 5
  • 32
  • 38
1

Somehow I had to also init the object after registering in AppDelegate:

PicModel.registerSubclass()
PicModel()
ShotModel.registerSubclass()
ShotModel()
Aviel Gross
  • 9,770
  • 3
  • 52
  • 62
0

I actually filed a bug against parse for this very same reason and they ended up updating their subclassing documentation with the following:

Please note that the initialize method is not called until the class receives its first message, meaning that you need to call any instance or class method on your subclass before it will be registered with Parse SDK.

So you NEED to call the registerSubclass() method, or any other method for the class to be registered properly with Parse.

Julian B.
  • 3,605
  • 2
  • 29
  • 38