ios - Cannot add JSON response to realm -


i'm trying add post response realm thrown error:

cannot convert value of type 'string' expected argument type 'object'

this code:

     alamofire.request(.post, data.loginendpoint, parameters: parameters)         .responseobject { (response: response<particulars, nserror>) in              print(response.request)             print(response.response)             print(response.result)               if let result = response.result.value             {                  do{                     print(realm.configuration.defaultconfiguration.fileurl)                     let realm = try realm()                     realm.add(result.name, update: true)                 }                  catch let err nserror {                     print("error realm: " + err.localizeddescription)                 }              }             else             {                 print("json data nil. 123")             }     } 

update

i getting error

fatal error: unexpectedly found nil while unwrapping optional value

at user.name = result["name"]!.string

also, i'd remove ! do{} catch{} swift won't allow me so. how fix it?

my code:

alamofire.request(.post, data.loginendpoint, parameters: parameters)         .responseobject { (response: response<particulars, nserror>) in              print(response.request)             print(response.response)             print(response.result)                if let result = response.result.value             {                  do{                     let user = particulars()                     user.name = result["name"]!.string                     user.apitoken = result["api_token"]!.string                     let realm = try realm()                     realm.add(user, update: true)                     print(realm.configuration.defaultconfiguration.fileurl)                 }                  catch let err nserror {                     print("error realm: " + err.localizeddescription)                 }              }             else             {                 print("json data nil. 123")             }     } 

the following snippet pulled straight realm docs. realm wanting class object inherits 'object' type. have create class object of properties need (name, email, etc) set properties when response , save object realm.

class dog: object {   dynamic var name = ""   dynamic var age = 0 } class person: object {   dynamic var name = ""   dynamic var picture: nsdata? = nil // optionals supported   let dogs = list<dog>() }  // use them regular swift objects let mydog = dog() mydog.name = "rex" mydog.age = 1 print("name of dog: \(mydog.name)")  // default realm let realm = try! realm()  // query realm dogs less 2 years old let puppies = realm.objects(dog).filter("age < 2") puppies.count // => 0 because no dogs have been added realm yet  // persist data try! realm.write {    realm.add(mydog) } 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -