swift - Cannot Invoke Value of type Trip into Type Json -


i have struct follows :

struct trip {     var status: int?     var name: int?     var id: int?     var petid: [int]     var timestart: int? } 

i use struct load data json using function.

func starttrips( _ petidss: [int], callback: @escaping apitrip) {     let data = ["pets" : petidss]     apimanager.instance.perform(call: .starttrip, with: data) { (error, json) in         if let error = error {             callback(error, nil)         } else if let data = json{              callback(nil, trip(data))         }      }  } 

but when try cast it's type trip in callback callback(nil, trip(data)) either

missing argument parameters

that's if leave struct or initialise optionals. if convert parmeters in struct optionals get

cannot invoke value of type trip type json.

so not sure what's wrong here?

if don't specify initialiser struct 1 have access member wise initialiser.

what need write custom initialiser taking json data parameter. implementation uses swiftyjson.

like this:

struct trip {    var status: int?   var name: int?   var id: int?   var petid: [int]   var timestart: int?    init(status: int?, name: int?, id: int?, petid: [int], timestart: int?) {     self.status = status     self.name = name     self.id = id     self.petid = petid     self.timestart = timestart   }    init?(_ json: json) {     if let petid = json["petid"].number as? int {       self = trip(status: json["status"].number as? int, name: json["name"].number as? int, id: json["id"] as? int, petid: petid, timestart: json["timestart"] as? int)     } else {       return nil     }   } } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -