remy boys remy boys - 1 year ago 362
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


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( // expected output
print((dog.cats) // here i'm getting the error

In debugger the value of
is :

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

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

any one knows why is this happening ??

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download