I have a list of competitions in a class on parse named "Competitions", and I need to pull the list of competitions from said class to populate an array to populate a UI PickerView. What is the easiest way possible to complete this?
Thank you.
//edit with code
override func viewDidLoad() {
super.viewDidLoad()
var compQuery = PFQuery(className:"Competitions")
compQuery.whereKey("teamID", equalTo:"1")
compQuery.findObjectsInBackgroundWithBlock {
(competitions: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// Do something with the found objects
for competition in competitions {
NSLog("%@", competition["compName"] as String)
self.competitionArray += [competition["compName"] as String]
}
} else {
// Log details of the failure
NSLog("Error: %@ %@", error, error.userInfo!)
}
}
}
// Edited with new errors Since I am adding the objects to a UIPicker for the user to select a value, I used this to get the objects as an array of strings,
var competitionArrayString:Array = [competitionArray as AnyObject as [String]]
however I am receiving the error that my class does not have the member "competitionArray"
import UIKit
import Swift
import Parse
class CompetitionViewController: UIViewController {
var competitionArrayString:Array = [""]
var competitionArray:NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
var compQuery = PFQuery(className:"Competitions")
compQuery.whereKey("teamID", equalTo:"1")
self.competitionArray = NSMutableArray()
compQuery.findObjectsInBackgroundWithBlock {
(competitions: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// Do something with the found objects
for competition in competitions {
NSLog("%@", competition["compName"] as String)
self.competitionArray.addObject([competition["compName"] as String])
}
} else {
// Log details of the failure
NSLog("Error: %@ %@", error, error.userInfo!)
}
}
}
var competitionArrayString:Array = [competitionArray as AnyObject as [String]]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return competitionArrayString.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return competitionArrayString[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
var selectedData = competitionArrayString[row]
var userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(selectedData, forKey: "competitionData");
userDefaults.synchronize();
println("You Selected: \(selectedData)")
}
}
findObjectsInBackgroundWithBlock:with a PFQuery of your class name