ios - 以数组形式快速从解析中提取数据

标签 ios swift parse-platform

我在名为“Competitions”的解析类中有一个竞赛列表,我需要从该类中提取竞赛列表以填充数组以填充 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]]

但是我收到错误消息说我的类(class)没有成员“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) 您确保正在初始化您的数组,例如:

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

关于ios - 以数组形式快速从解析中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28127561/

相关文章:

ios - 使用 Photo Kit 和 Swift 在 UICollectionViewCell 中模糊图像

ios - 如何移动 CGContextRef 上的图像

ios - 创建自定义 Collection View 布局

arrays - 为什么我的 Swift 应用程序在我尝试解包可选时会抛出 fatal error ?

java - 如何使用 Parse.com ParseRelation

ios - 快速网络服务通信

ios - 用户界面 TableView : Change cell height dynamically when clicked the custom button

ios - 我如何与 AppDelegate 的特定 View 进行交互?

ios - Facebook登录错误 "User is not allowed to see the application."

rest - 解析 REST 如何使用用户指针创建对象?