Support Topics Documentation Slack YouTube Blog

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

(Roger Wistar) #1

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 =
    Types.tryblock({() -> Void in
        let data = readStore?.find()
        let rateComments = data as! [RateComment]

Any ideas what is wrong?

(Olga Danylova) #2


does the RateComment class contains @objcMembers attribute?

Regards, Olga

(Roger Wistar) #3

Yes, it does.

(Sergey Kukurudzyak) #4


please, zip the project and attach it to the topic

(Roger Wistar) #5

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

(Roger Wistar) #6

Any thoughts?

(Sergey Chupov) #7

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.

(Olga Danylova) #8

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:

class RateComment: NSObject {

    var objectId : String?
    var salad_rating : NSNumber?
    var soup_rating : NSNumber?
    var veg_rating : NSNumber?
    var updated : Date?
    var main_rating : NSNumber?
    var comment : String?
    var created : Date?
    var side_rating : NSNumber?
    var ownerId : String?

Find method:

Types.tryblock({ () -> Void in
            let data = readStore?.find()
            let rateComments = data as! [RateComment]
                       catchblock: { (exception) -> Void in

Everything works fine for me.

Regards, Olga

(Roger Wistar) #9

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:

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!

(Olga Danylova) #10

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.

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

Regards, Olga