Backendless Support
 
Solved

Why getting an "Unexpectedly found nil" error when calling readStore?.find()?

One of my student groups is trying to read from a table that exists in the project -- it is visible from the web interface. He is using exactly the same code that I had them use when doing a tutorial lab, and that code worked fine. But when he runs his own code, he gets this error:

Unexpectedly found nil while unwrapping an Optional value.

The nil value is the return from readStore?.find() -- why is that returning nil? Here is the code snippet:

let readStore = backendless.data.of(RateComment.ofClass())

Types.tryblock({() -> Void in

let data = readStore?.find()

let rateComments = data as! [RateComment]

Any ideas what is wrong?

Leave a Comment

Comments (9)

photo
1

Hello,

does the RateComment class contains @objcMembers attribute?

Regards, Olga

photo
1

Yes, it does.

photo
photo
1

Hello,

please, zip the project and attach it to the topic

photo
1

Here it is -- too big for attachment but here is a link to Google Drive:

https://drive.google.com/file/d/1-7jY77v_CuDAd7d0TCtCRMIngTDfY9HQ/view?usp=sharing

photo
1

Any thoughts?

photo
1

Hi Roger,

We've created an internal task BKNDLSS-16738 to investigate your problem. Our iOS engineer will reach you soon in this topic for additional details.

photo
photo
1

Hello Roger,

First of all, the project you've attached doesn't represent the problem with the find method - it doesn't even contain that method..

For the future please attach the simple projects that represents the problem, not UI.

Secondly, there is no @objcMembers attribute in the RateComment class - thats the problem. iOS-SDK uses Obj-C runtime so it is necessasry to add @objcMembers before the data classes. You can find more info here.

The RateComment class:

  1. @objcMembers
  2. class RateComment: NSObject {
  3. var objectId : String?
  4. var salad_rating : NSNumber?
  5. var soup_rating : NSNumber?
  6. var veg_rating : NSNumber?
  7. var updated : Date?
  8. var main_rating : NSNumber?
  9. var comment : String?
  10. var created : Date?
  11. var side_rating : NSNumber?
  12. var ownerId : String?
  13. }

Find method:

  1. Types.tryblock({ () -> Void in
  2. let data = readStore?.find()
  3. let rateComments = data as! [RateComment]
  4. print(rateComments)
  5. },
  6. catchblock: { (exception) -> Void in
  7. print(exception!)
  8. })

Everything works fine for me.

Regards, Olga

photo
1

Thanks for your reply. I'm terribly sorry -- I had provided the wrong link in my earlier email. This is the correct URL to the ZIP file that contains the problem:

https://drive.google.com/file/d/1HYQUPDw2ZCON_bhjGKjwn_tJbxuFSLNz/view?usp=sharing

Respectfully, I thought it better to provide a file containing the entire project because we aren't sure which part of the project is causing the problem. There is definitely a problem, though!

photo
1

Hello Roger,

There are multiple inits of Backendless (RateCommentViewController and ResultsViewController) - thats the problem, there must be only one.

Please place the initApp method to the AppDelegate class and remove from anywhere else. And everything will be fine.

  1. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  2. backendless.hostURL = SERVER_URL
  3. backendless.initApp(APPLICATION_ID, apiKey: API_KEY)
  4. return true
  5. }

Regards, Olga