将AnyObject转换为特定类

时间:2016-07-14 13:27:27

标签: swift casting socket.io anyobject

我正在使用socket.io Swift Library。使用以下代码行,

socket.on("info") { (dataArray, socketAck) -> Void in
            let user = dataArray[0] as? User
            print(user._id)
}

dataArray[0]是一个有效的对象,但{strong> 之后,user似乎

由于dataArray[0]作为AnyObject返回, 如何将AnyObject转换为User对象?或者以某种方式设法用不同的方法做我想做的事情?

Socket Class

User Class

3 个答案:

答案 0 :(得分:0)

您需要创建一个接受AnyObject并读取其中数据的构造函数。 我想在这种情况下dataArray[0]是一个JSON对象。

class User {
  init(data: [String: AnyObject]) {
     username = data["username"] as? String ?? ""
  }
}

答案 1 :(得分:0)

自此之后

let user = dataArray[0] as? User

nil内有user值,表示您在User的第一个位置没有dataArray值。

由于dataArray来自服务器(我猜),它可能包含User的序列化版本。

现在我们真的需要知道dataArray [0]究竟是什么。然而...

如果dataArray [0]包含NSData

在这种情况下试试这个

let json = JSON(dataArray[0] as! NSData)
let user = User(json:json)

答案 2 :(得分:0)

这是我的管理方式:

// Structure used as parameter
struct InfoStruct {
var nome: String = ""
var sobrenome:String = ""
var nascimentoTimestamp: NSNumber = 0

init() {

}

// Struct to AnyObject
func toAnyObject() -> Any {
    var dic = [String:AnyObject?]()

    if (nome != "") { dic["nome"] = nome as AnyObject }
    if (sobrenome != "") { dic["sobrenome"] = sobrenome as AnyObject }
    if (nascimentoTimestamp != 0) { dic["nascimentoTimestamp"] = nascimentoTimestamp as AnyObject }

    return dic
}

// AnyObject to Struct
func fromAnyObject(dic:[String:AnyObject]) -> InfoStruct {
    var retorno = InfoStruct()

    if (dic["nome"] != nil) { retorno.nome = dic["nome"] as? String ?? "" }
    if (dic["sobrenome"] != nil) { retorno.sobrenome = dic["sobrenome"] as? String ?? "" }
    if (dic["nascimentoTimestamp"] != nil) { retorno.nascimentoTimestamp = dic["nascimentoTimestamp"] as? NSNumber ?? 0 }

    return retorno
} }


// User class
class Usuario: NSObject {
var key: String
var admin: Bool
var info: InfoStruct // Struct parameter

init(_ key: String?) {
    self.key = key ?? ""
    admin = false
    info = InfoStruct() // Initializing struct
}

// From Class to AnyObject
func toAnyObject() -> Any {
    var dic = [String:AnyObject?]()

    if (key != "") { dic["key"] = key as AnyObject }
    if (admin != false) { dic["admin"] = admin as AnyObject }
    dic["info"] = info.toAnyObject() as AnyObject // Struct

    return dic
}

// From AnyObject to Class
func fromAnyObject(dic:[String:AnyObject]) -> Usuario {
    let retorno = Usuario(dic["key"] as? String)

    if (dic["key"] != nil) { retorno.key = dic["key"] as? String ?? "" }
    if (dic["admin"] != nil) { retorno.admin = dic["admin"] as? Bool ?? false }
    if (dic["info"] != nil) { retorno.info = InfoStruct.init().fromAnyObject(dic: dic["info"] as! [String : AnyObject]) } // Struct

    return retorno
} }

// Using
let dao = FirebaseDAO.init(_type: FirebaseDAOType.firebaseDAOTypeUser)
dao.loadValue(key: uid) { (error, values:[String : AnyObject]) in
     if error == nil {
          let user = Usuario(values["key"] as? String).fromAnyObject(dic: values)
     }
}

希望对您有帮助!