gpt4 book ai didi

ios - 使用键检索 PFObject

转载 作者:行者123 更新时间:2023-11-29 02:25:02 29 4
gpt4 key购买 nike

我在使用 key 成功检索 PFObject 时遇到问题。对象最后为零:

    var object = PFObject(className:"Topic")
var query = PFQuery(className:"Topic")
query.whereKey("title", equalTo:"Default")
query.getFirstObjectInBackgroundWithBlock {
(object: PFObject!, error: NSError!) -> Void in
if object != nil {
NSLog("The getFirstObject request failed.")
} else {
// The find succeeded.
NSLog("Successfully retrieved the object.")
}
}

用它来创建关注关系

user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if error == nil {
NetworkManager.sharedInstance.follow(object, completionHandler: {
(error) -> () in
if error == nil {
}
else {
println("follow topic failure")
}
})
}
else {
println("signup failure")
}
}
}

测试对象的检索是否成功:

    println("this is the title of the default topic")
println(object["title"] as String)

控制台打印:

this is the title of the default topic
fatal error: unexpectedly found nil while unwrapping an Optional value

最佳答案

如果您尝试在 getFirstObjectInBackgroundWithBlock block 之外打印 object["title"],您当前的代码将会导致一些问题。

(1) 我认为您正试图从您在代码顶部创建的 object PFObject 打印,即 var object = PFObject(className:"Topic"),而不是您的 getFirstObjectInBackgroundWithBlock 创建的用于其范围内的 object;但是从未检索到 getFirstObjectInBackgroundWithBlock 之外的 PFObject 对象

(2) getFirstObjectInBackgroundWithBlock block 是异步运行的,这意味着它是在后台获取的,可能不会立即可用;因此,即使 getFirstObjectInBackgroundWithBlock 之外的 PFObject 对象 被设置为包含 getFirstObjectInBackgroundWithBlock 返回的 PFObject 对象,您也可以'保证对象将准备好在 block 外使用,因为当您尝试在 block 外打印 object["title"]getFirstObjectInBackgroundWithBlock 仍然可以获取对象 block 。

(3) 即使在您的 block 中,您似乎也不理解您的条件,因为 if object != nil 您正在打印请求已失败,但实际上已成功。

以下是我建议的打印对象并为 signUpInBackgroundWithBlock 使用相同的 object 的方法:

// fetchedObject is a global variable created to store
// the retrieved object, but make sure the
// getFirstObjectInBackgroundWithBlock has had time to
// fetch the object before trying to use fetchedObject;
// also check to make sure it's non-nil
var fetchedObject:PFObject!

func signUp () {

var query = PFQuery(className:"Topic")
query.getFirstObjectInBackgroundWithBlock {
(object: PFObject!, error: NSError!) -> Void in
if object != nil {

// Save the object as a global object for use
// outside of this method
self.fetchedObject = object

// The find succeeded.
println("Successfully retrieved the object.")
println("This is the title of the default topic:")
if let defaultTopic:String = object["title"] as? String {
println(defaultTopic)
}

user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if error == nil {
NetworkManager.sharedInstance.follow(object, completionHandler: {
(error) -> () in
if error == nil {
}
else {
println("follow topic failure")
}
})
}
else {
println("signup failure")
}
}
} else {
println("The getFirstObject request failed with error: \(error)")
}
}
}

关于ios - 使用键检索 PFObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27662932/

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