Swift NSArray element failed to match the Swift Array Element type

This is a strange one, on one of my swift projects the code runs fine. On another it does not. I am testing out retrieving data with backendless as I begin to rewrite some code from Parse. It seems to be with the format the data is coming back in from the datastore.

var backendless = Backendless.sharedInstance()

            let whereClause = "VoucherID = " + voucherID




            

            let dataQuery = BackendlessDataQuery()

            dataQuery.whereClause = whereClause

            

            var error: Fault?

        

            let datastore = backendless.data.of(Voucher.ofClass()).find(dataQuery, fault: &error)

            

            if error == nil {

                print("Vouchers have been found: \(datastore.data)")

                

                let vouchers = datastore.data  

                for voucher in vouchers! as! [Voucher] {

                   

                    print("\(voucher)")

                                        




                    

                }

            }

            else {

                print("Server reported an error: \(error)")

            }

OK fairly basic stuff. The project which works gives me output like this. voucher is the correct class. I have defined Voucher class in a swift file outside of the view controller. This all works perfectly.

Vouchers have been found: [<voucher.Voucher: 0x13773dd20>]
<voucher.Voucher: 0x13773dd20>

However the project throwing the error is giving me data in this format… This is the same output from exactly the same code…
Vouchers have been found: [{

AD1 = "";

AD2 = "";

AD3 = "";

AD4 = Kent;

AD5 = "&lt;null&gt;";

BookingDate = "29/02/2016";

BookingTime = "18 : 34";

Email = "";

FirstName = Michael;

Init = Michael;

PartyOptions = "|Players,5";

PartyTotal = "";

PartyType = "";

PostCode = "";

Redeemed = FALSE;

Surname = Turner;

Tel = 044;

Title = MR;

Valid = TRUE;

VoucherID = 92;

"___class" = Voucher;

"__meta" = "{\"relationRemovalIds\":{},\"selectedProperties\":[\"BookingDate\",\"Redeemed\",\"Email\",\"ownerId\",\"Init\",\"PartyTotal\",\"VoucherID\",\"___class\",\"PartyType\",\"objectId\",\"FirstName\",\"created\",\"BookingTime\",\"Title\",\"AD1\",\"AD2\",\"AD3\",\"AD4\",\"AD5\",\"Valid\",\"PartyOptions\",\"Tel\",\"PostCode\",\"updated\",\"Surname\"],\"relatedObjects\":{}}";

created = "2016-02-29 18:34:16 +0000";

objectId = "8A09BAAB-FB3C-7F12-FF3D-C6E8C3A88700";

ownerId = "&lt;null&gt;";

updated = "&lt;null&gt;";

}]

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

I am using exactly the same code, and have set the projects up to use Backendless in the same way, but for some reason something is very different. I have been on this all day so I hope you can help. It seems to be the data is coming through in a completely different format on one project compared to the other? I have followed other threads regarding NSArray element failed to match the Swift Array Element to no avail. Please help, been on this all day and I just can’t solve it!

Mike

Hi Mike,

1.Your Voucher class must be implemented in separate .swift file (i.e. outside another target classes, for example, ViewController)
2. Please check - it is need that target’s Bundle Name == $(PRODUCT_NAME)
http://support.backendless.com/public/attachments/808fc618cac2630cbecfeb5968594549.png&lt;/img&gt;

You shouldn’t change this attribute working with Backendless SDK.

Regards,
Slava

Genius. Number 2 solved the problem. It works like magic after that. Thanks very much for your quick reply, it was driving me bonkers!