我正在尝试优化我的应用,因为加载时间似乎非常慢。现在我正在处理新闻。我要做的第一件事是使用id和与该userId对应的PFUser保存[followingId:followingPFUser]的字典。出于某些原因,当我运行该函数时,它会给我这个错误:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<_TtGCSs29_NativeDictionaryStorageOwnerSSCSo6PFUser_ 0x13f5a5440> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key 2NWK7vm8cK.'
我认为字典有问题,但我无法理解。这是我的代码:
func updateFollowing(){
if self.followedUsers.count > 0{
print("found some data")
let getPeopleFollowedByUser = PFQuery(className: "Followers")
getPeopleFollowedByUser.whereKey("following", equalTo: (PFUser.currentUser()?.objectId!)!)
getPeopleFollowedByUser.findObjectsInBackgroundWithBlock({ (followingObjects, error) -> Void in
if error == nil {
for followingObject in followingObjects! {
let followed = followingObject.objectForKey("followed") as! String
var duplicate : Bool = false
for (userId, user) in self.followedUsers {
if followed == userId as! String {
duplicate = true
}
}
if (!duplicate){
let followedInfoQuery = PFUser.query()!
followedInfoQuery.getFirstObjectInBackgroundWithBlock({ (user, error ) -> Void in
if error == nil {
if let user = user as? PFUser{
self.followedUsers.setValue(user, forKey: followed)
}
}else{
print(error)
}
NSUserDefaults.standardUserDefaults().setObject(self.followedUsers, forKey: "followedUsers")
})
}
}
}else{
print(error)
}
})
}else{
print("No data saved")
var users : NSDictionary = [String : PFUser]()
if let permanentList = NSUserDefaults.standardUserDefaults().objectForKey("followedUsers") as? [String : PFUser] {
users = permanentList
}
if users.count > 0 {
for (userId, user) in users {
self.followedUsers.setValue(user, forKey: userId as! String)
}
}
let getPeopleFollowedByUser = PFQuery(className: "Followers")
getPeopleFollowedByUser.whereKey("following", equalTo: (PFUser.currentUser()?.objectId!)!)
do{
let followingObjects = try getPeopleFollowedByUser.findObjects()
for followingObject in followingObjects {
let followed = followingObject.objectForKey("followed") as! String
let followedInfoQuery = PFUser.query()!
do{
let user = try followedInfoQuery.getObjectWithId(followed) as! PFUser
print(user)
self.followedUsers.setValue(user, forKey: followed)
}catch let err{
print(err)
}
}
NSUserDefaults.standardUserDefaults().setObject(self.followedUsers, forKey: "followedUsers")
}catch let err{
print(err)
}
}
}
如果您发现可能导致问题的任何问题,请告诉我们! 谢谢!
编辑:我在调试时跟踪了错误,显然它在这一行崩溃了:
self.followedUsers.setValue(user, forKey: followed)
我尝试将其更改为self.followedUser[followed] = user
或self.followedUsers.setObject(user, forKey: followed)
,但不会使用这两者中的任何一个进行构建。
答案 0 :(得分:0)
首先(每个@JAL)您错误地设置了字典self.followedUsers
。您应该执行self.followedUsers[followed] = user
或self.followedUsers.setObject(user, forKey: followed)
其次,NSUserDefaults
只能存储某些类型的数据。来自documentation:
NSUserDefaults类提供了便于访问的方法 常见类型,例如浮点数,双精度数,整数,布尔值和URL。一个 默认对象必须是属性列表,即(或 对于集合的实例组合):NSData,NSString, NSNumber,NSDate,NSArray或NSDictionary。如果你想存储任何 在其他类型的对象中,通常应将其存档以创建 NSData的实例。
因此,您的错误是由于您尝试保存包含Followers
的字典而不是其中一个类的值。