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!