remy boys remy boys - 4 months ago 85
iOS Question

reason of NSArray element failed to match the Swift Array Element type

i'm stuck here from past 2 hours still have no clue what the heck is wrong here :

in my backend i have values say have tables named

Dogs
and
Cats


my dogs table's one column is related to cats table (one to many relation)

now i'm trying to get the values of my dogs table but i'm keep getting thins error :

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


my Code:

//object classes
class Dogs : NSObject {
var id : String?
var cats : [Cats]?
}

class Cats : NSObject {
var rats : [Rats]?
var name : String?
}


here i retrieved the Dogs Class from my Backend and after that

for dog in dogs { // dogs is an array of Dogs Object
print(dog.id) // expected output
print((dog.cats) // here i'm getting the error


In debugger the value of
dog.cats
is :

id String? "dog1"
cats [MyPackage.ViewController.Cats]? Some


i also confirmed that cats have value in it by printing
dog.cats.count
which is returning the total objects (Expected behaviour)

any one knows why is this happening ??

Answer

You have declared your Cats class inside the ViewControllee class, which means that you array contains ViewComtroller.Cats objects, not Cats.

Declare your Cats (and your Dogs class) in their own .swift files

Comments