swift - Returning an array of objects from a function -


i'm new swift.

i have following classes used map json response.

class basketballteamresponse: mappable {     var name: string?     var alias: string?     var market: string?     var founded: int?      var players: [players]?      required init?(_ map: map){ }      func mapping(map: map) {         name <- map["name"]         alias <- map["alias"]         market <- map["market"]         founded <- map["founded"]         players <- map["players"]     } }  class players: mappable {     var full_name: string?     var jersey_number: string?     var position: string?      init(full_name: string, jersey_number: string, position: string) {         self.full_name = full_name         self.jersey_number = jersey_number         self.position = position     }      required init?(_ map: map) { }      func mapping (map: map) {         full_name <- map["full_name"]         jersey_number <- map["jersey_number"]         position <- map["position"]     } } 

let's have viewcontroller , want populate finalroster array of objects of players. i've tried appending self , finalroster still empty , want use later on display them in tableview. tried setting return type -> [players]

class viewcontroller: uiviewcontroller {  var finalroster = [players]()  override func viewdidload() {     super.viewdidload()     // additional setup after loading view, typically nib.      getbasketballteamplayers() }  private func getbasketballteamplayers() {     let url = "http://api.sportradar.us/nba-t3/teams/583ec825-fb46-11e1-82cb-f4ce4684ea4c/profile.json?api_key=xxxxxxxxxxxxxxxxxxxxxxxx"     //var pool = [players]()     alamofire.request(.get, url, parameters: nil)         .validate()         .responseobject { (response: response<basketballteamresponse, nserror>) in             let gswresponse = response.result.value             if let roster = gswresponse?.players {                 players in roster {                     print(players.full_name!)                     print(players.jersey_number!)                     print(players.position!)                     let add = players(full_name: players.full_name!, jersey_number: players.jersey_number!, position: players.position!)                     self.finalroster.append(add)                 }             }         } } 

how can pass array of objects getbasketballteamplayers() finalroster? thanks

as jtbandes have comment it's better call class player instead of players.

typealias apisuccess = (result: player?) -> void typealias apifailure = (error: nsdictionary?) -> void  class viewcontroller: uiviewcontroller {     var finalroster = [player]()     override func viewdidload() {         super.viewdidload()         // additional setup after loading view, typically nib.          let myapisuccess: apisuccess = {(result: player?) -> void in             print ("api success : result is:\n \(result)")             // here can make whatever want result              self.finalroster.append(result!)         }         let myapifailure: apifailure = {(error: nsdictionary?) -> void in             print ("api failure : error is:\n \(error)")             // here can check errors error dictionary looking http error type or http status code         }         getbasketballteamplayers(success: myapisuccess, failure: myapifailure)     }      private func getbasketballteamplayers(success successblock :apisuccess,                                           failure failureblock :apifailure) {         let url = "http://api.sportradar.us/nba-t3/teams/583ec825-fb46-11e1-82cb-f4ce4684ea4c/profile.json?api_key=xxxxxxxxxxxxxxxxxxxxxxxx"         //var pool = [players]()         alamofire.request(.get, url, parameters: nil)             .validate()             .responseobject { (response: response<basketballteamresponse, nserror>) in                 if response.result.issuccess {                     let gswresponse = response.result.value                     if let roster = gswresponse?.players {                         players in roster {                             print(players.full_name!)                             print(players.jersey_number!)                             print(players.position!)                             let add = players(full_name: players.full_name!, jersey_number: players.jersey_number!, position: players.position!)                             successblock(result: add)                         }                     } else {                         print("i have kind of error in data")                     }                 } else {                     let httperror: nserror = response.result.error!                     let statuscode = httperror.code                     let error:nsdictionary = ["error" : httperror,"statuscode" : statuscode]                     failureblock(error: error)                 }             }         } } 

Comments

Popular posts from this blog

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

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

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