成功查询后,findObjectsInBackgroundWithBlock返回nil

时间:2015-10-22 22:42:53

标签: ios json xcode swift parse-platform

我当前的问题是我的-findObjectsInBackgroundWithBlock方法返回nil以便成功查询。

我的自定义类是使用{results:[{}]}语法导入的JSON。我的JSON中有22个词典 - {结果:[22词典]}。我的自定义类在" let查询中正确声明和拼写..."宣言。当我运行我的代码"打印(查询)"行输出" PFQuery:0x7faadb527f40"所以我假设查询成功了?通过成功,我的意思是查询识别出有一个类调用" BarLibrary"在我的解析网站上 - 有。

如果我更改[PFObject],请使用以下代码?到[AnyObject]?我得到了分段11错误。用[PFObject]?作为"对象的类型" in -findObjectsInBackgroundWithBlock我总是得到一个零。

我的目标是从parse.com成功提取我的JSON数据并将其存储到"对象"中。

My Class Name and # of items in JSON as seen on my parse account

我的自定义JSON中的数据结构如下:

{ 
"results" :  [{
        "name": String,
        "address": String,
        "image": String,
        "latitude" : double,
        "longitude" : double,

        "Monday" : {   "key1": String,
            "key2":String,
            "key3": 16,
            "key4": 19,
            "key5": 999,
            "key6": 999,
            "key7": String,
            "key8": String
        },
        "Tuesday" : {   "key1": String,
            "key2":String,
            "key3": 16,
            "key4": 19,
            "key5": 999,
            "key6": 999,
            "key7": String,
            "key8": String
        },
        "Wednesday" : {   "key1": String,
            "key2":String,
            "key3": 16,
            "key4": 19,
            "key5": 999,
            "key6": 999,
            "key7": String,
            "key8": String
        },
        "Thursday" : {   "key1": String,
            "key2":String,
            "key3": 16,
            "key4": 19,
            "key5": 999,
            "key6": 999,
            "key7": String,
            "key8": String
        },
        "Friday" : {   "key1": String,
            "key2":String,
            "key3": 16,
            "key4": 19,
            "key5": 999,
            "key6": 999,
            "key7": String,
            "key8": String
        },
        "Saturday" : {   "key1": String,
            "key2":String,
            "key3": 16,
            "key4": 19,
            "key5": 999,
            "key6": 999,
            "key7": String,
            "key8": String
        },
        "Sunday" : {   "key1": String,
            "key2":String,
            "key3": 16,
            "key4": 19,
            "key5": 999,
            "key6": 999,
            "key7": String,
            "key8": String
        }
          ...... // 22 total dictionaries like this in the array "results"

有人能指出我正确的方向吗?

import Foundation
import Parse


struct getBarJSON {

init(){

    let query : PFQuery = PFQuery(className: "BarLibrary")
    print("\n")
    print("Result of the query")
    print("=========================")
    print(query) // <PFQuery: Hex Number>

    query.findObjectsInBackgroundWithBlock {(objects : [PFObject]?, error: NSError? )-> Void in

        if error == nil {
            // Do something upon successfull network request
            print("\n")
            print("Result of Network Request")
            print("=========================")

            if let objects = objects as [PFObject]! {
                    print(objects)
                }

        } else {
            // Report the error
            print("\n")
            print("Error Description:")
            print("==================")
            print(error)
        }
       }
      }
     }

2 个答案:

答案 0 :(得分:0)

这是从查询中获取对象的正确方法。如果你仍然没有,那么你应该检查你的“BarLibrary”类表有的数据(并且可能在这里发布样本内容,以便了解它的结构)当前登录打印出PFObjects而不管它们的属性。

import Foundation
import Parse


struct getBarJSON {

init(){

    let query = PFQuery(className: "BarLibrary")

    query.findObjectsInBackgroundWithBlock {(objects : [PFObject]?, error: NSError? )-> Void in
        print("\n")
        print("Result of Network Request")
        print("=========================")
        if error == nil {
            print("\n")
            print("Result of the query")
            print("=========================")

            if let objects = objects {   // unwrapping an optional
                for object in objects {  // iterating over the array of pfobjects
                    print(object)
                    if let customObject = object["someProperty"] as? String { // in case you store a string. The process in similar for numbers, arrays, etc.
                      print(customObject.characters.count)
                    }
                }
            }

        } else {
            // Report the error
            print("\n")
            print("Error Description:")
            print("==================")
            print(error)
        }
       }
      }
     }

答案 1 :(得分:0)

嘿,大家好,谢谢大家的回复。我终于让我的代码正常工作了。

我的代码:

import Foundation
import Parse


struct getBarJSON {

var barLibrary : [PFObject]?

init(){
    // Pull data from parse

    let query : PFQuery = PFQuery(className: "BarLibrary")

    query.findObjectsInBackgroundWithBlock {(objects : [PFObject]?, error: NSError? )-> Void in

        print("\n")
        print("Result of Network Request")
        print("=========================")

        if error == nil {
            // Do something upon successfull network request
            print("\n")
            print("Result of the query")
            print("=========================")
            if var objects = objects { // unrwapping the optional
                for object in objects { // iterating over the array of PFObjects
                    objects.append(object)
                }
            }
            print("\(objects!.count) dictionaries retrieved ")
            self.barLibrary = objects
        } else {
            // Report the error
            print("\n")
            print("Error Description:")
            print("==================")
            print(error)
        }
     // end of findObjectsInBackgroundWithBlock closure
    }

 // end of getBarJSON init()
}

// end of getBarJSON struct declaration
}

我总共得到22个[PFObjects]?存储在&#39; barLibrary&#39;这正是我想要的。 @ driver733的回应帮助我指导了正确的方向。

现在我的下一个挑战是弄清楚如何使用PFObject子类来解析我的数据!