如何使用swift在Parse上存储字典? [英] How do you store a dictionary on Parse using swift?
问题描述
谢谢!
var user = PFUser()
var friendsPFUser:[PFUser] = []
var friendListDict:[PFUser:String] = Dictionary()
var query = PFUser.query()
query!.findObjectsInBackgroundWithBlock {
(users:[AnyObject] ?,错误:NSError?) - >空虚
如果错误== nil {
//找到成功。
println(成功检索\(users!.count)用户。)
//如果让用户=用户,请使用找到的对象
执行某些操作? [PFUser] {
friendsPFUser = users
在friendsPFUser中的用户{
friendListDict [user] =已确认
}
用户[friends] = friendListDict / /这行打破东西
user.saveInBackground()
}
} else {
//记录故障的详细信息
println(Error:\(error !)\(error!.userInfo!))
}
}
要清楚,这段代码编译,但是当我添加
user [friends] = friendListDict
我的应用程序崩溃。
对于那些可能有这个问题的人。 NSInternalInconsistencyException与原因PFObject包含未缓存的容器项。
为了安全起见,Parse将用户添加到用户(如数组或字典)中,用户将要修改的字段必须是当前用户。
尝试在块内注册并使用addObject,不要忘记保存它!
它帮助我遇到了类似的问题。
I am very new to swift and I don't know Obj C at all so many of the resources are hard to understand. Basically I'm trying to populate the dictionary with PFUsers from my query and then set PFUser["friends"] to this dictionary. Simply put I want a friends list in my PFUser class, where each friend is a PFUser and a string. Thanks!
var user = PFUser()
var friendsPFUser:[PFUser] = []
var friendListDict: [PFUser:String] = Dictionary()
var query = PFUser.query()
query!.findObjectsInBackgroundWithBlock {
(users: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(users!.count) users.")
// Do something with the found objects
if let users = users as? [PFUser] {
friendsPFUser = users
for user in friendsPFUser{
friendListDict[user] = "confirmed"
}
user["friends"] = friendListDict //this line breaks things
user.saveInBackground()
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
To be clear, this code compiles but when I add
user["friends"] = friendListDict
my app crashes.
For those who might have this issues with. "NSInternalInconsistencyException" with reason "PFObject contains container item that isn't cached." Adding Objects to a user (such as arrays or dictionaries) for security reasons on Parse, the user for such field that will be modified must be the current user. Try signing up and using addObject inside the block and don't forget do save it! It helped for a similar problem I had.
这篇关于如何使用swift在Parse上存储字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!