从swift中解析数据作为数组

时间:2015-01-24 16:24:20

标签: ios swift parse-platform

我在一个名为“竞赛”的解析类中有一个竞赛列表,我需要从所述类中提取竞赛列表以填充数组以填充UI PickerView。什么是最简单的方法来完成这个?

谢谢。

//使用代码编辑

    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以供用户选择值,我使用它来将对象作为字符串数组来获取,

var competitionArrayString:Array = [competitionArray as AnyObject as [String]]

但是我收到的错误是我的班级没有成员“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 个答案:

答案 0 :(得分:3)

(1)要在Swift中向数组添加元素,您必须使用addObject,而不是+=和(2)确保您正在初始化数组,例如:< / p>

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!)
        }
    }
}