Getting NSArray values into Picker View

Hello,


I am trying to get values from an NSArray into a Picker View. I have written a server side php script called positions.php that will output records from a mySQL database in json format using:


  $host='localhost';
  $user='username';
  $password='access';
  $dbname = "SwiftApp";


        // Create connection
        $connection = new mysqli($host, $user, $password, $dbname);

        //Check connection
  if ($connection->connect_error) {
            die("Connection to positions table failed: " . $conn->connect_error);
        }

        $sql = "SELECT position FROM positions ORDER BY position";
        $result = $connection->query($sql);
        $records = array();




        if ($result->num_rows > 0) {
             // output data of each row
             while($row = $result->fetch_assoc()) {
                  $records[] = $row; 
             }
        } else {
             echo "0 positions retrieved.";
        }
        echo json_encode($records);


        $connection->close();



I am able to read the values into my Swift App like this:




var positions:NSArray = []



func getPositions(){
        let url = NSURL(string: "http:/blah.com/positions.php")
        let data = NSData(contentsOfURL: url!)
        positions = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
    }


    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

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

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
   
      return stuck here! // I have tried return positions[row] and I get an error.
    
    }


if I print the results of the positions array to the console it looks like this:


(

{

position = QB;

},

{

position = RB;

},

{

position = WR;

}

)


I don't know how to return those values to into the Picker View.


Thanks for the help!

What error do you get?

Hello,


Here is the error i get when using


return positions[row] as! String


Could not cast value of type '__NSDictionaryM' (0x1030e4fc0) to 'NSString' (0x1034dcb48).

(lldb)

Apparently positions is an array of Dictionaries, not an array of Strings.

Wonder why.


positions = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray

Based on what is displayed when you printed it, it looks like your PHP code is generating a JSON structrue that is an array of dictionaries, with each dictionary having one key "position". Another way to visualize it:

Array Index   Dictionary Key   Dictionary Value
0             "position"       "QB"
1             "position"       "RB"
2             "position"       "WR"

I'm having the same issue. Did you ever figure out the solution?

"guywithmazda" already answered correctly. The JSON is in fact an array of dictionaries. So, this:


positions[row]


is a single dictionary. If the OP wanted the value of the "position" key, the correct code would have been:


positions[row]["position"] // a string


There was never any real issue.


What is the actual issue you're having. At this point, I suggest you ask again (with more information) in a new thread. Apart from anything else, the code in this thread is for an older version of thread, and shouldn't be used as a pattern for current code.

Getting NSArray values into Picker View
 
 
Q