Unrecognized Selector Sync vs. Async

I am having an issue with the following line of code when used in an asynchronous find method. The odd part is that it works in the synchronous version without any issues. Am I doing something wrong?

let username = user.getProperty("username") as! String

Synchronous Code

var fault: Fault?
let result = backendless.data.of(BackendlessUser.ofClass()).find(dataQuery, fault: &fault)
 if fault == nil
 {
 if result.totalObjects.intValue == 0
 {
 ...
 }
 else if result.totalObjects.intValue >= 1
 {
 let user = result.data[0]
 let username = user.getProperty("username") as! String
 }
 }
 else
 {
 // Handle Errors
 }

Asynchronous Code

self.backendless.data.of(NewEmailUser.ofClass()).find(dataQuery, response: {(result : BackendlessCollection!) -> () in
 
 if result.totalObjects.intValue == 0
 {
 ...
 }
 else if result.totalObjects.intValue >= 1
 {
 let user = result.data[0]
 let username = user.getProperty("username") as! String
 }
 }, error: { (fault : Fault!) -> ()in
 
 // Handle errors.
 })

Hi Jon,

Let we investigate your code. In Synchronous Code you fetch BackendlessUser objects, but in Asynchronous Code you retrieve NewEmailUser objects. Looks like your NewEmailUser class does not contain '“username” property, so line 10

let username = user.getProperty("username") as! String

crashes on a casting nil to String.

If this will not be helpful, please provide your appId here or to support@backendless.com, we will investigate this issue with your data.

Regards,
Slava

Slava,

I’m sorry. I should have caught this. I appreciate the help. This fixed the problem.

Take care,

Jon