将JsonObject传递给Model SWIFT

时间:2015-07-15 20:38:42

标签: json swift swifty-json

我正在尝试将返回结果列表中的JSON对象传递给User的Model类。我想在用户对象中进行所有赋值/解析。

我一直收到消息 - 无法使用JSON类型的参数调用User

任何提示?

  let post = JSON(data)

                println("The post is: " + post.description)

                var user : User

                user(post[0])

                println(user.getName())



 import SwiftyJSON

class User {

var ObjectId = ""
  var FirstName = ""
  var LastName = ""
  var Organization = ""
  var CallSign = ""

init(sObjectId : String, sFirstName : String, sLastName : String,     sOrganization : String, sCallSign : String)
  {
ObjectId = sObjectId
FirstName = sFirstName
LastName = sLastName
Organization = sOrganization
CallSign = sCallSign


}

init(sUser : JSON) {

    self.ObjectId = sUser["_id"].string!
    self.FirstName = sUser["firstName"].string!
    self.LastName = sUser["lastName"].string!
    self.Organization = sUser["organization"].string!



}

1 个答案:

答案 0 :(得分:1)

你必须直接调用适当的初始化程序

let post = JSON(data)
println("The post is: " + post.description)
var user = User(sUser: post[0])