0

从 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)
        }
    }
}
4

1 回答 1

0

是的,您需要在 Appdelegate 中或在调用 Parse.setApplicationId 之前注册子类

它在此文本块中说明:https ://parse.com/docs/ios/guide#objects-subclasses

例子:

Appdelegate.swift
....
User.registerSubclass()
Parse.setApplicationId("appid", clientId:"clientid")
于 2016-01-10T00:42:58.707 回答