将 Parse 数组添加到字典 swift
Add Parse array to dictionary swift
我有一些对象正在解析,并且我正在成功获取数据作为 [PFObjects]。问题是我试图将数组元素 [PFObjects] 作为值添加到字典中。但是我一直得到一个空字典,所以这些值没有添加到字典中。字典计数也是0.
这是我目前尝试的方法:
var postDictionary = [String:[AnyObject]]()
query.findObjectsInBackground(block: { (posts: [PFObject]?, error:Error?) in
if let unwrappedPosts = posts {
for posts in unwrappedPosts {
if let postText = posts.object(forKey: "title") as?String {
self.titleArray.append(postText)
print("count", self.titleArray.count) // count 10
self.postDictionary["title"]?.append(self.titleArray as AnyObject)
**try to force unwrap **
self.postDictionary["title"]!.append(self.titleArray as AnyObject), and the app crashed
for (title, text) in self.postDictionary {
print("\(title) = \(text)")
}
print("Dictionay text count",self.postDictionary.count) // count is 0
}
}
}
})
添加到字典的正确方法是使用 updateValue,因为据我所知,您的字典中没有键 "title",我猜您是在将值附加到未知键。
这应该有帮助:
titleArray.append(postText)
postDictionary.updateValue(titleArray as [AnyObject], forKey: "title")
for (key,value) in postDictionary {
print("\(key) \(value)")
}
最后应该打印:
title [posts1, posts2, posts3]
这个语法很混乱
self.titleArray.append(postText)
self.postDictionary["title"]?.append(self.titleArray as AnyObject)
您将一个字符串追加到一个数组,然后您要将 数组 追加到字典中的数组。我想这不是故意的。
我建议 map
title
字符串并为键设置数组 title
一次
var postDictionary = [String:[String]]()
query.findObjectsInBackground(block: { (posts: [PFObject]?, error:Error?) in
if let unwrappedPosts = posts {
self.titleArray = unwrappedPosts.compactMap { [=11=].object(forKey: "title") as? String }
self.postDictionary["title"] = self.titleArray
for (title, text) in self.postDictionary {
print("\(title) = \(text)")
}
print("Dictionay text count",self.postDictionary.count) // count is 0
}
})
如果类型更具体,切勿使用 AnyObject
。
我有一些对象正在解析,并且我正在成功获取数据作为 [PFObjects]。问题是我试图将数组元素 [PFObjects] 作为值添加到字典中。但是我一直得到一个空字典,所以这些值没有添加到字典中。字典计数也是0.
这是我目前尝试的方法:
var postDictionary = [String:[AnyObject]]()
query.findObjectsInBackground(block: { (posts: [PFObject]?, error:Error?) in
if let unwrappedPosts = posts {
for posts in unwrappedPosts {
if let postText = posts.object(forKey: "title") as?String {
self.titleArray.append(postText)
print("count", self.titleArray.count) // count 10
self.postDictionary["title"]?.append(self.titleArray as AnyObject)
**try to force unwrap **
self.postDictionary["title"]!.append(self.titleArray as AnyObject), and the app crashed
for (title, text) in self.postDictionary {
print("\(title) = \(text)")
}
print("Dictionay text count",self.postDictionary.count) // count is 0
}
}
}
})
添加到字典的正确方法是使用 updateValue,因为据我所知,您的字典中没有键 "title",我猜您是在将值附加到未知键。
这应该有帮助:
titleArray.append(postText)
postDictionary.updateValue(titleArray as [AnyObject], forKey: "title")
for (key,value) in postDictionary {
print("\(key) \(value)")
}
最后应该打印:
title [posts1, posts2, posts3]
这个语法很混乱
self.titleArray.append(postText)
self.postDictionary["title"]?.append(self.titleArray as AnyObject)
您将一个字符串追加到一个数组,然后您要将 数组 追加到字典中的数组。我想这不是故意的。
我建议 map
title
字符串并为键设置数组 title
一次
var postDictionary = [String:[String]]()
query.findObjectsInBackground(block: { (posts: [PFObject]?, error:Error?) in
if let unwrappedPosts = posts {
self.titleArray = unwrappedPosts.compactMap { [=11=].object(forKey: "title") as? String }
self.postDictionary["title"] = self.titleArray
for (title, text) in self.postDictionary {
print("\(title) = \(text)")
}
print("Dictionay text count",self.postDictionary.count) // count is 0
}
})
如果类型更具体,切勿使用 AnyObject
。