0

I have a UIPickerView. I have a blank array set up until my code parses an XML request. So when it does I want it to populate the array with that data. Below is how I have everything set up.

var pickerData = []

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return pickerData[row] as String
    }

func parser(parser: NSXMLParser!, foundCharacters string: String!) {
        if (seperatedSoap == "pickerDataString") {
            if (currentElementName == "name") {
                pickerData = [string]
                println(pickerData)
            }

        }

I obviously didn't paste every bit of code but you should be able to understand what's going on here. Now my console prints this as my pickerData after parsing.

(
    "2x1 on recv-small"
)
(
    "2x1 on ship-small"
)
(
    "3x2 on avs-1"
)
(
    "2.25x2 on photo-large"
)
(
    "2.25x2 on recv-large"
)
(
    "2.25x2 on repair-large"
)
(
    "2x3 on wireless-1"
)
(
    "4x6 on ship-shiplbl"
)
(
    "4x6 on recv-shiplbl"
)
(
    "4x6 on wireless-shiplbl"
)

Which is exactly what I want but it's not showing up in my UIPickerView because apparently it's blank? What am I doing wrong here?

2 Answers2

0

First you should use

pickerData.append(string)

since your assignment will always set the array to contain a single (the last) element.

Then you probably need to call reloadComponent

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • Can't believe I forgot about that! It's been awhile haha, you are awesome! I also changed my array to only accept strings. var pickerData = [String]() –  Feb 23 '15 at 06:39
0

Append arrays addition assignment operator (+=):

pickerData += [string]

See Swift Collection Types