ios - Calling a function more than once swift, returning the same data -
so in swift call in function in ui picker
func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) { switch row{ case 0: //switch out returning actual selected item picker not using picker array let poke = ("\(mooarray[0])") getpokemon(poke); case 1: let poke = ("\(mooarray[1])") getpokemon(poke); case 2: let poke = ("\(mooarray[2])") getpokemon(poke); default: println("stopping") } //self.view.backgroundcolor = newbackcolor; }
as can see calls getpokemon, passes through pokemon string, getpokemon function below
func getpokemon(poke: string) { println("\(poke)"); //var pokemon = "charizard" var url = "http://pokeapi.co/api/v1/pokemon/" + poke alamofire.request(.get, url) .responsejson { (req, res, json, error) in if(error != nil) { nslog("error: \(error)") println(req) println(res) } else { //nslog("success: \(url)") let json = json(json!) //println("\(json)") if let ability = json["abilities"][0]["name"].string { //println("the title is: \(ability)") } if let evolevels = json["evolutions"][0]["level"].number { //println("\(evolevels)") } if let attack = json["attack"].number { //println("\(attack)") self.pokemon.append("\(attack)"); } else { println(json["attack"].error) } if let name = json["name"].string { //println("\(name)") self.pokemon.append("\(name)"); } println("\(self.pokemon[0])") println("\(self.pokemon[1])") } } }
this uses alamo fire , swifty json. takes string, appends url api calls , returns data printing console.
this working fine when select row 1st time, makes call , returns right data api.
however if select row returns same data? reason why call not refresh new string passed though?
just possibility here, happens if change print from:
println("\(self.pokemon[0])") println("\(self.pokemon[1])")
to:
println("\(self.pokemon.last)")
i think you're getting new data not logging because new entries inserted end of array, , you're logging first element.
Comments
Post a Comment