gpt4 book ai didi

ios - findObjectsInBackgroundWithBlock 查询成功后返回 nil

转载 作者:行者123 更新时间:2023-11-30 13:58:30 24 4
gpt4 key购买 nike

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

我的自定义类是一个 JSON,它是使用 {results: [{}]} 语法导入的。我的 JSON 中有 22 个字典 - {结果:[ 22 个字典]}。我的自定义类在“let query...”声明中正确声明和拼写。当我运行代码时,“print(query)”行输出“PFQuery: 0x7faadb527f40”,所以我假设查询成功?我所说的成功是指查询识别出我的解析站点上有一个名为“BarLibrary”的类 - 确实存在。

使用下面的代码如果我更改 [PFObject]?到[任何对象]?我收到分段 11 错误。与[PFObject]?作为 -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)
}
}
}
}

最佳答案

这是从查询中获取对象的正确方法。如果你仍然得到 nil,那么你应该检查你的“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)
}
}
}
}

关于ios - findObjectsInBackgroundWithBlock 查询成功后返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33291979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com