Search code examples
iosswiftuipickerview

Calling a function more than once swift, returning the same data


So in swift I call in a function in the UI Picker

 func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        switch row{
        case 0:
            //switch out for returning the actual selected item from picker not using the 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 you can see it calls getpokemon, passes through the pokemon string, the getpokemon function is 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 and swifty json. It takes the string, appends it to the url for the API calls and returns the data which I am currently printing to the console.

This is all working fine when I select a row the 1st time, it makes the call and returns the right data from the API.

However if I select another row then it returns the same data? Any reason why the call would not refresh with the new string I passed though?


Solution

  • Just a possibility here, but what happens if you change your print from:

    println("\(self.pokemon[0])")
    println("\(self.pokemon[1])")
    

    To:

    println("\(self.pokemon.last)")
    

    I think you're getting new data but not logging it because new entries are inserted to the end of the array, and you're logging the first element.