1

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)")


}

}
7
  • This should be a fairly straight-forward task... What code have you tried so far? Commented Jan 24, 2015 at 16:41
  • @LyndseyScott I understand how to reference the objectID in the table by using the code as supplied in the documentation, however I am trying to reference a column and put the data in to an array Commented Jan 24, 2015 at 17:04
  • You can query an entire class using findObjectsInBackgroundWithBlock: with a PFQuery of your class name Commented Jan 24, 2015 at 18:12
  • I've got that. How do I get the queried data into the array Commented Jan 24, 2015 at 19:30
  • It's already returned in array form... You really have to post your code if you want any help beyond that. Commented Jan 24, 2015 at 19:31

1 Answer 1

3

(1) To add an element to an array in Swift you have to use addObject, not += and (2) you make sure you're initializing your array, ex:

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)
            }
            self.pickerControl.reloadAllComponents()  // <- reload your picker's data
        } else {
            // Log details of the failure
            NSLog("Error: %@ %@", error, error.userInfo!)
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

This did help, however check the original post for an update with new errors and see if you have any idea to why I am getting these. Thank you.
@TylerPatrick Where/what are the errors? I don't see anything about them in your post...
Just added them sorry for the confusion.
@TylerPatrick This line var competitionArrayString:Array = [competitionArray as AnyObject as [String]] can't be used outside of a method or outside of the class... That line doesn't belong there.
@TylerPatrick Actually it's the same issue. You can't have this line var competitionArrayString:Array = [competitionArray as AnyObject as [String]] outside of a function. Also it's a duplicate declaration of competitionArrayString and that's not OK...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.