Home>

I want to get the value from an NCMB object, but I get the following error: If i know the solution, I would appreciate it if you could let me know.
(Since it is possible to communicate with mBaaS and acquire objects from the DB, I think that NCMB cooperation itself is possible.)

code
// Create query
var query = NCMBQuery.getQuery (className: TestClass)
query.where (field: "keyNo", greaterThanOrEqualTo: 0)
// do a search
query.findInBackground (callback: {result in
    switch result {
      case let .success (array):
           print ("Successfully retrieved: \ (array.count)")
           if array.count>1 {
                let data01 = array [0] as! NCMBObject
                print (data01) // Data is displayed normally (the number of displayed items is also correct)
// Problem area
                let value01 = data01.object (forkey: "testValue") as! String
// ↑ I think it was such a syntax before (the error below)
                let value02 = data01.objectForkey ("testValue") as! String
// ↑ I changed it a little (the error below)
                let value03 = data01 ["testValue"] as! String
// ↑ I tried using the official document writing method (error below)
           }
     case let .failure (error):
     print ("Failed to get: \ (error)")
    }
})
Error message (syntax error)
// value01
Value of type'NCMBObject' has no member'object'
// value02
Value of type'NCMBObject' has no member'objectForkey'
// value03
Generic parameter'T' could not be inferred
What I tried

I tried this, but it didn't work.
https://github.com/NIFCloud-mbaas/UserCommunity/issues/949

Supplement

NCMB cooperation was carried out by the following "How to download and use SDK"
https://mbaas.nifcloud.com/doc/current/introduction/quickstart_swift.html#SDK%E3%82%92%E3%83%80%E3%82%A6%E3%83%B3%E3%83% AD% E3% 83% BC% E3% 83% 89% E3% 81% 97% E3% 81% A6% E5% 88% A9% E7% 94% A8% E3% 81% 99% E3% 82% 8B% E6% 96% B9% E6% B3% 95

  • Answer # 1

    NCMBObjectCan be specified by subscript (Subscript is defined), so

    let value01 = data01.object (forkey: "testValue") as! String

    not,

    let value01: String = data01 ["testValue"]

    I think you can get the desired field with.

    By the way, downcasting doesn't work, so it seems that it is necessary to explicitly give the type to the receiving property so that type inference can be performed (detailed cause is not traced).

    The sample code seems to be partly out of date.

    I think the online documentation below is the most up-to-date (although some updates don't seem to be catching up and I have to use the Objc documentation as well).

    https://mbaas.nifcloud.com/doc/current/datastore/basic_usage_swift.html