【问题标题】:Parse.com ~ PFObject to Array SwiftParse.com ~ PFObject 到 Swift 数组
【发布时间】:2016-08-17 19:27:09
【问题描述】:

我已经设置了 Parse.com 后端。在我的 iOS 应用程序中,我与此后端建立了连接,并且能够检索存储的对象。

我希望将每个特定对象存储在一个数组中。 假设我的 Key:value 所在的对象,例如:Category = Coffeebars

目前我已经编写了以下代码:

let queryToGetBuildings = PFQuery(className: "Building")

queryToGetBuildings.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

if error == nil {
    for object in objects!{
         print("this is an object")
         print(object)
    }
 } else {
    print("something went wrong")
 }

分别返回我的每个对象:

"this is an object"
<Building: 0x7f, objectId: yh, localId: (null)> {
    BottomText = "Opened from";
    BottomTextExtra = "05:00 until 22:00";
    Category = Coffeebars;
    MiddleText = "Suggestion:";
    MiddleTextExtra = "Apple and cinnamon";
    TimeToDestination = 5;
    TopText = Starbucks;
}

"this is an object"
<Building: 0xr, objectId: Xj, localId: (null)> {
    BottomText = "Fee per person (not for babies):";
    BottomTextExtra = "0.20";
    Category = Toilets;
    MiddleText = "Men - Women - Babies";
    MiddleTextExtra = " ";
    TimeToDestination = 5;
    TopText = "Bathroom 2 terminal A";
 }

"this is an object"
<Building: 0pr, objectId: 2q, localId: (null)> {
    BottomText = "Fee per person (not for babies):";
    BottomTextExtra = "0.20";
    Category = Toilets;
    MiddleText = "Men - Women";
    MiddleTextExtra = " ";
    TimeToDestination = 8;
    TopText = "Bathroom 3 terminal A";
}

我现在的主要问题是如何打印出(并添加到特定数组中)具有 Key:Value = Category:Toilets 的对象的所有信息?

提前致谢

【问题讨论】:

  • 你知道 Parse.com 将被关闭吗?

标签: arrays json swift parse-platform


【解决方案1】:

如果我理解你的话,你是在问如何显示获取的数据。

首先你应该声明一个数组来保存获取的数据:

var someData = [PFObject]()

在你的闭包中添加这个self.someData.append(object),现在你的代码应该是这样的:

let queryToGetBuildings = PFQuery(className: "Building")

queryToGetBuildings.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

if error == nil {
    for object in objects!{
         print("this is an object")
         print(object)

         // Add this line
         self.someData.append(object)
    }
 } else {
    print("something went wrong")
 }

获取数据后,该显示了:

// if you are using tableView to display 
let thisData = someData[indexPath.row] 

let bottomText = thisData["columnName"] as? String
let bottomTextExtra = thisData["columnName"] as? String
let category = thisData["columnName"] as? SomeType
let middleText = thisData["columnName"] as? String
let middleTextExtra = thisData["columnName"] as? String
let timeToDestination = thisData["columnName"] as? NSNumber
let topText = thisData["columnName"] as? String

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多