我环顾四周,所有其他答案对我都不起作用,我正在尝试查询PFUser,但我一直得到一个例外'在使用Parse之前,必须在registerSubclass中注册PFUser类。'
这就是问题所在:
override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
let message = messages[indexPath.item]
if self.avatars[message.senderId] == nil {
var imageView = JSQMessagesAvatarImage(placeholder: UIImage(named: "profile"))
self.avatars[message.senderId] = imageView
let user = users[message.senderId]!
print(user)
var parseAvatar = PFFile()
PFUser.registerSubclass()
let imageQuery = PFUser.query()
imageQuery?.whereKey("objectId", equalTo: user.objectId!)
imageQuery?.limit = 1
imageQuery?.findObjectsInBackgroundWithBlock({ (images: [AnyObject]?, error: NSError?) -> Void in
if imageQuery?.countObjects() == 0 {
return
} else {
for image in images! {
if image.objectForKey("profileImage") == nil {
return
} else {
let userPic = image.objectForKey("profileImage") as! PFFile
userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
let image = UIImage(data: imageData!)
imageView.avatarImage = JSQMessagesAvatarImageFactory.circularAvatarImage(UIImage(data: imageData!), withDiameter: 30)
}
})
}
}
}
})
// Reload entire table now that the avatar is downloaded
self.collectionView?.reloadData()
}
return self.avatars[message.senderId]
}
知道我在这里可能缺少什么吗?我在另一个文件中这样做了,它没有那个问题。所以我有点困惑。
修改
喜欢这个吗?
import Foundation
import Parse
class TradeUser : PFUser {
override class func initialize() {
self.registerSubclass()
}
var userID : String {
get {return objectForKey("objectId") as! String}
set { setObject(newValue, forKey: "objectId") }
}
var emailAddress : String {
get {return objectForKey("email") as! String}
set { setObject(newValue, forKey: "email") }
}
var firstName : String {
get {return objectForKey("firstName") as! String}
set {setObject(newValue, forKey: "firstName")}
}
var lastName : String {
get {return objectForKey("lastName") as! String}
set {setObject(newValue, forKey: "lastName")}
}
var primaryQueue : String {
get {return objectForKey("primaryQueue") as! String}
set {setObject(newValue, forKey: "primaryQueue")}
}
var image : PFFile {
get { return self["profileImage"] as! PFFile }
set { self["profileImage"] = newValue }
}
}
答案 0 :(得分:0)
似乎子类的注册方式存在问题。
使用以下
更新PFUser子类class TradeUser : PFUser {
override class func initialize() {
struct Static {
static var onceToken : dispatch_once_t = 0;
}
dispatch_once(&Static.onceToken) {
self.registerSubclass()
}
}
...
}