swift - Loading Serialised JSON Into Table? -


im having hard time wrapping head around process, have made api call , received json, use model serialise json can used in view controller table, im having issues how call api in view controller have result fit serialisation model. hope explained correctly?

here code of api request:

open class apiservice: nsobject {      open func getdata(completionhandler: @escaping (nsdictionary?, nserror?) -> void) -> self {          let requesturl = "https://wger.de/api/v2/exercise/?format=json"          alamofire.request(requesturl, method: .get, encoding: urlencoding.default)             .responsejson { response in                 switch response.result {                 case .success( let data):                      completionhandler(data as? nsdictionary, nil)                  case .failure(let error):                     print("request failed error: \(error)")                     completionhandler(nil, error nserror?)                 }         }         return self     } } 

and here code of serialisation

final public class exercise: responseobjectserializable {     var id: int!     var description: string!     var name: string!     var muscles: string!     var equipment: string!      public init?(response: httpurlresponse, representation: any) {          guard             let representation = representation as? [string: any],             let id = representation["id"] as? int,             let description = representation["description"] as? string,             let name = representation["name"] as? string,             let muscles = representation["muscles"] as? string,             let equipment = representation["equipment"] as? string          else { return nil }          self.id = id         self.description = description         self.name = name         self.muscles = muscles         self.equipment = equipment      }  } 

but cant work out how fit view controller function call this

    let apiservice = apiservice() let searchcontroller = uisearchcontroller(searchresultscontroller: nil)  var arrres: [string] = [] var filtered: [string] = [] var searchactive: bool = false  var id: int? var description: string? var name: string? var muscles: string? var equipment: string?   override func viewdidload() {     super.viewdidload()     exercisestableview.delegate = self     exercisestableview.datasource = self     exercisesearchbar.delegate = self     getapidata() }  func getapidata() {      let _ = apiservice.getdata() {         (data, error) in         if let data = data {              if let arr = data["results"] as? [string] {                  self.arrres = arr                 self.exercisestableview.reloaddata()             }          } else if let error = error {             print(error)         }     } } 

  • first of http response not affect custom class @ left out.
  • second of values keys muscles , equipment arrays rather strings.
  • third of since json data seems immutable declare properties in class constant (let)

with few changes custom class

final public class exercise {    let id : int    let description: string    let name: string    let muscles : [int]    let equipment : [int]     public init?(dictionary: [string: any]) {        guard          let id = dictionary["id"] as? int,          let description = dictionary["description"] as? string,          let name = dictionary["name"] as? string,          let muscles = dictionary["muscles"] as? [int],          let equipment = dictionary["equipment"] as? [int]           else { return nil }        self.id = id       self.description = description       self.name = name       self.muscles = muscles       self.equipment = equipment    } } 

then have declare data source array

var exercises = [exercise]() 

and in method getapidata() populate array

     ...      if let results = data["results"] as? [[string:any]] {         result in results {            if let exercise = exercise(dictionary: result) {               self.exercises.append(exercise)            }         }         self.exercisestableview.reloaddata() // might dispatched on main thread.      } 

note: any used in swift 3, in swift 2 replace occurrences of any anyobject


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -