快速从解析中提取数据作为数组 [英] Pulling data from parse in swift as an array
问题描述
我在名为Competitions"的解析类中有一个竞赛列表,我需要从所述类中提取竞赛列表以填充数组以填充 UI PickerView.完成此任务的最简单方法是什么?
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?
谢谢.
//用代码编辑
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!)
}
}
}
//编辑有新错误由于我将对象添加到 UIPicker 以供用户选择一个值,因此我使用它来将对象作为字符串数组获取,
// 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]]
但是我收到了我的班级没有成员competitionArray"的错误
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)")
}
}
推荐答案
(1) 要在 Swift 中向数组添加元素,您必须使用 addObject
,而不是 +=
和 (2) 你确保你正在初始化你的数组,例如:
(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!)
}
}
}
这篇关于快速从解析中提取数据作为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!