NSArray element failed to match the Swift Array Element type

Hi,
I’m still new with backendless and I’ve been trying to get my head around this for a while now. I followed the tutorials yet I’m still getting an error while trying to cast from BackendlessCollection to Swift array.
func loadMessages() {
let query = BackendlessDataQuery()
let whereClause = “user = ‘((currentUser?.objectId)!)’”
let queryOptions = QueryOptions()
queryOptions.sortBy([“updated DESC”])
query.whereClause = whereClause
query.queryOptions = queryOptions

let dataStore = backendless.data.of(Message.ofClass())
dataStore.find(query,
response: { (collection: BackendlessCollection!) -> Void in
self.messages.removeAll()
self.messages += collection.getCurrentPage() as! [Message]
self.tableView.reloadData()
}) { (error) -> Void in
print(“Error: (error)”)
}
}
class Message: NSObject {
var ownerId: String?
var objectId: String?
var created: NSDate?
var updated: NSDate?

var lastUser: String?
var user: String?
var lastMessage: String?
var groupId: String?
var counter: Int = 0
var descriptionM: String?
}
On this line: self.messages += collection.getCurrentPage() as! [Message], I get the following error: “fatal error: NSArray element failed to match the Swift Array Element type”
Can someone please point me in a good direction? I tried a lot of different ways to do this and I get the same error each time. A similar problem was reported here: http://support.backendless.com/t/how-to-downcast-a-groupscollection-array-to-a-swift-array, but I’ve had no luck trying all of those solutions.
Thanks

Hi Florin,

The “getCurrentPage()” method returns a collection of Message objects, therefore casting the whole collection to just one Message will cause the error. Take a look at the following example which shows how to get the individual objects from the returned collection:
https://backendless.com/feature-16-data-retrieval-api-how-to-load-objects-from-an-mbaas-storage/

Regards,
Mark

Hi Mark,

Thanks for your incredibly quick answer. I’ve tried this, from the tutorial you gave me:

func loadMessages() {

    let query = BackendlessDataQuery()

    let whereClause = "user = '\((currentUser?.objectId)!)'"

    let queryOptions = QueryOptions()

    queryOptions.sortBy(["updated DESC"])

    query.whereClause = whereClause

    query.queryOptions = queryOptions

    

    backendless.persistenceService.of(Message.ofClass()).find(query,

        response: { (collection: BackendlessCollection!) -> Void in




            let currentPage = collection.getCurrentPage()

            for item in currentPage as! [Message]{

                print("item: \(item.groupId)")

            }




        }) { (error) -> Void in

            print("Error while retrieved messages: \(error)")

            self.refreshControl?.endRefreshing()

    }

}

I get the same error on line “for item in currentPage as! [Message]{”
“fatal error: NSArray element failed to match the Swift Array Element type”

What am I missing?

Hi Florin,

What is you set a breakpoint and check in the debugger what the “currentPage” variable contains?

Regards,
Mark

Btw, is the Message class defined in a separate .swift file? If not, please make sure it is not defined in the controller code.

Yes, the Message class is defined in a separate .swift file.

The currentPage variable contains one (Message *) object. I attached a screenshot if there is anything else you’d like to check.

Thanks!

Florin,

The screenshot helped. I think what happened is the object in the collection was deserialized as our own Message class. I am not sure how to disambiguate between your class and the one included into the SDK (I’d need to defer to our iOS experts). A quick and dirty solution would be to rename the table and your class to something that does not cause the naming collision.

Regards,
Mark

Mark,

I guess I’ll take the dirty solution for now. Hopefully you’ll find a way to fix this so others won’t encounter the same issue.

Once again, thank you for answering me so quickly. I don’t always have the time to work on this so your quick response is really appreciated.

Florin

Hi Florin,

You should rename your Message class, because Backendless SDK already has public Message class - https://github.com/Backendless/ios-SDK/blob/master/SDK/backendlessAPI/Classes/Messaging/Message.h

On your screenshot you can see that Message objects, you’ve got by getCurrenPage, was casted as Backendless SDK class - not yours (take note on the names of the properties):

http://support.backendless.com/public/attachments/1f1465cf6a2d6f2abb27fc7f13e8fab2.png</img>

So, fix it and try again please.

Regards,
Slava

Backendless SDK Message class cannot be rename, because this will bring the problems for existing apps