Back4app - Swift - 将PFObject转换为Class

时间:2017-07-28 15:10:21

标签: swift parse-platform casting

我正在尝试cast PFObject custom class Swift class,我读了很多关于它的帖子,然后我需要继承我的来自PFObject的{​​{1}}。问题是我的class已经从NSObject继承而且之间存在冲突。

是否有另一种方法可以将PFObject转换为custom class

Usuario.swift

class Usuario: NSObject, NSCoding {

    //MARK: Propriedades
    var nome: String?
    var foto: String?
    var dataNascimento: Date?
    var numeroTelefone: String?
    var pais: PaisCodigo?
    var telefoneE164: String?

    var objectId: String?
    var created: Date?
    var updated: Date?

    override init() {}

    required init(coder aDecoder: NSCoder) {

        nome = aDecoder.decodeObject(forKey: "nome") as? String
        foto = aDecoder.decodeObject(forKey: "foto") as? String
        dataNascimento = aDecoder.decodeObject(forKey: "dataNascimento") as? Date
        numeroTelefone = aDecoder.decodeObject(forKey: "numeroTelefone") as? String
        pais = aDecoder.decodeObject(forKey: "pais") as? PaisCodigo
        telefoneE164 = aDecoder.decodeObject(forKey: "telefoneE164") as? String

        objectId = aDecoder.decodeObject(forKey: "objectId") as? String
        created = aDecoder.decodeObject(forKey: "created") as? Date
        updated = aDecoder.decodeObject(forKey: "updated") as? Date
    }

    func encode(with aCoder: NSCoder) {

        if let nomeUsuario = nome {
            aCoder.encode(nomeUsuario, forKey: "nome")
        }

        if let fotoUsuario = foto {
            aCoder.encode(fotoUsuario, forKey: "foto")
        }

        if let dataNascimentoUsuario = dataNascimento {
            aCoder.encode(dataNascimentoUsuario, forKey: "dataNascimento")
        }

        if let numeroTelefoneUsuario = numeroTelefone {
            aCoder.encode(numeroTelefoneUsuario, forKey: "numeroTelefone")
        }

        if let paisUsuario = pais {
            aCoder.encode(paisUsuario, forKey: "pais")
        }

        if let telefoneE164Usuario = telefoneE164 {
            aCoder.encode(telefoneE164Usuario, forKey: "telefoneE164")
        }

        if let objectIdUsuario = objectId {
            aCoder.encode(objectIdUsuario, forKey: "objectId")
        }

        if let createdUsuario = created {
            aCoder.encode(createdUsuario, forKey: "created")
        }

        if let updatedUsuario = updated {
            aCoder.encode(updatedUsuario, forKey: "updated")
        }
    }
}

使用objectId的Parse结果会返回以下结果:

<Usuario: 0x6080000abb20, objectId: 7NwpmD81w3, localId: (null)> {
    nome = "Pablo Cavalcante";
    numeroTelefone = 67992497386;
    pais = "<PaisCodigo: 0x6080000abb80, objectId: rA5wdIWEFt, localId: (null)>";
    telefoneE164 = "+5567992497386"; }

因此它返回一个Usuario对象,我需要将其强制转换。

1 个答案:

答案 0 :(得分:1)

您可以使用PFUser。我看到您正在声明一个用户类,因此您可以将class User: PFUser, PFSubclassing { //MARK: Propriedades dynamic var nome: String? dynamic var foto: String? dynamic var dataNascimento: Date? dynamic var numeroTelefone: String? dynamic var pais: PaisCodigo? dynamic var telefoneE164: String? dynamic var objectId: String? dynamic var created: Date? dynamic var updated: Date? } 作为子类,然后编写如下内容:

init(with:)

当然,如果你正在使用encode(with:)~(?i)(?<=")((?!mailto:)(?:[a-z]*:\/\/)?(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{a1}-\x{ffff}0-9]+-?)*[a-z\x{a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{a1}-\x{ffff}0-9]+-?)*[a-z\x{a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{a1}-\x{ffff}]{2,})))|localhost)(:\d{2,5})?(?:\/(?:[^\s/]*/)*[^\s/.]+)?)(?=")~,你必须实施它......