Support Topics Documentation Slack YouTube Blog

Class to table mapping for custom service

I’m trying to get objects from the backend and map them to classes defined in Swift from the SDK BackendlessSwift v6.0.4.1 so simliar to Android however I cannot find this functionality from customService.invoke topic relationed

  • App ID: BAFA98AD-EB6A-DCBD-FF35-67E7A51C7300
    • image
  • Sample client code:
@objcMembers 
        class Person : NSObject {
            var age: Int?
            var name: String?
        }
        let response = { (data: Any?) -> Void in
            print("--------------")
            print(data as! Person)
            print("--------------")
        }
        let errorHandler = { (error: Fault) -> Void in
            print(error)
        }
        Backendless.shared.customService.invoke(serviceName: "test", method: "get", parameters: nil , returnType: Person.self, executionType: .sync, responseHandler: response, errorHandler: errorHandler) 
  • Exception: Could not cast value of type 'Swift._SwiftDeferredNSDictionary<Swift.String, Any>'

Hello @David_Delagneau,

Your Person table has the age property of type INT, and you’re trying to make it String:

class Person : NSObject {
    var age: String?
    var name: String?
}

Your code will work fine if you change the age property to Int:

@objcMembers class Person : NSObject {
     var age: Int = 0
     var name: String?
 }

Regards,
Olha

I made the change (age: Int = 0) but I get the same exception Could not cast value of type 'Swift._SwiftDeferredNSDictionary<Swift.String, Any>' checking the source code .invoke receives the type of object to return but does not do extra processes to transform it

@olhadanylova I can see class based approach from ‘database store’ but not in custom services

Could you please share your archived project here because I cannot reproduce this issue and your code works fine for me.

Regards,
Olha

1 Like

Thank you very much @olhadanylova I have already noticed my error, it is that I defined the @objcMembers within another class

1 Like