从 parse user object 子类PFUser
化时,使用自己的方法时会出现错误指令错误。你们有什么想法为什么会发生这种情况
用户.swift
import UIKit
import Parse
class User: PFUser {
// Instance Variables
@NSManaged var photo: PFFile
// Parse Setup
override class func initialize() {
struct Static {
static var onceToken : dispatch_once_t = 0;
}
dispatch_once(&Static.onceToken) {
self.registerSubclass()
}
}
// Instance Methods
func fetchPhoto(callback: (image: UIImage) -> Void) {
guard let url = self.photo.url else {
return
}
let request = NSURLRequest(URL: NSURL(string: url)!)
if let image = Globals.imageCache.imageForRequest(request) {
callback(image: image)
return
}
Globals.imageDownloader.downloadImage(URLRequest: request) { response in
if let image: UIImage = response.result.value {
callback(image: image)
Globals.imageCache.addImage(image, forRequest: request)
} else {
print(response)
}
}
}
}
ViewController.swift
导入 UIKit
class ProfileHeaderController: UIViewController {
@IBOutlet weak var avatarButton: UIButton!
@IBOutlet weak var userLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!
private var user = User.current()
func updateHeader() {
self.userLabel.text = self.user.username
self.nameLabel.text = self.user.fullName
self.user.fetchPhoto { (image) -> Void in
self.avatarButton.setImage(image, forState: .Normal)
}
}
}