Parse.com~PFObject到Array Swift

时间:2016-04-24 12:25:10

标签: arrays json swift parse-platform

我已经设置了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?

提前致谢

1 个答案:

答案 0 :(得分:2)

如果我很了解你,你就会问如何显示提取的数据。

首先,您应该声明一个数组来保存提取的数据:

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