M.Aguzzi M.Aguzzi - 9 months ago 23
Swift Question

Could not cast value of type '__NSDictionaryM' to 'InLIsta_.Lista' searching to use my own class in find API by Backendless

i'm trying to retrieve data from an online data storage using the func that i found online and on the official Backendless docs! but when i try to use persona like a Lista(my own class) Object, i get the error: Could not cast value of type '__NSDictionaryM' (0x10c1ccfc0) to 'InLIsta_.Lista' (0x108439790).
i search over this site but the answer aren't specific for the Backendless case, so i hope that anyone can help me

this is my code (obviously i've declared all the var and let necessary to the code to run):

class Lista : NSObject {

var nome: String?
var pr: String?
var pagamento = 0
var entrato: Bool = false
var commenti: String?
var objectId: String?
var created: NSDate?
var updated: NSDate?


func findQ() {

Types.tryblock({ () -> Void in

let startTime = NSDate()

let found = self.backendless.persistenceService.of(Lista.ofClass()).find(self.query)

let currentPage = found.getCurrentPage()
print("Loaded \(currentPage.count) name objects")
print("Total name in the Backendless storage - \(found.totalObjects)")

for person in currentPage {
let persona = person as! Lista // here i get error
print("Restaurant <\(Lista.ofClass())> name = \(persona.nome)")

print("Total time (ms) - \(1000*NSDate().timeIntervalSinceDate(startTime))")

catchblock: { (exception) -> Void in
print("Server reported an error: \(exception as! Fault)")


The backendless persistence service has a method -(void)mapTableToClass:(NSString *)tableName type:(Class)type; that you need to call for each of your custom classes so they'll be used during the deserialisation.

self.backendless.persistenceService.mapTableToClass("Lista", type: Lista.self)

This needs to be done before any calls are made to use the persistence service.

Note that the classes, if not defined in obj-c, must be exported to obj-c. Note that this also means you can't have any optionals.

Ideally you should use the platform code generation to create your model class definitions to ensure all of the attributes are created with the appropriate types. A failure to map to your custom class could be caused by type mismatches or name mismatches. Optionals will always fail in the current SDK implementation.