如何将另一个数组中的数组存储到从Swift中的JSON传递的全局变量中?

时间:2016-11-11 09:36:20

标签: ios arrays json swift

我有一个JSON,它接收来自API调用的数组

在该数组中还有3个其他数组:

userDetailsuserStatscommunities

此API调用的一个示例是:

["communities": <__NSArrayI 0x6000002540a0>(
{
id = 5;
name = South;
},
{
id = 13;
name = HurraHarry;
},
{
id = 15;
name = EnclliffeT;
}
)
, "userStats": {
totalDraws = 3;
totalLosses = 10;
totalWins = 1;
}, "userDetails": {
id = 31;
"user_email" = "steve@gmail.com";
"user_name" = "Steve Base";
}]

我想将数组userStats存储在一个可以传递给另一个VC的变量中。

我的课程中有一个全局变量var userStatsArray = [AnyObject]()

以下代码处理JSON:

 let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
                  print (json!)

                  if let arr = json?["communities"] as? [[String:String]] {
                        self.communitiesArray = arr.flatMap { $0["name"]!}
                        self.communityIdsArray = arr.flatMap { $0["id"]!}

                    }

                    if let dict = json?["userDetails"] as? [String:String] {
                      self.tempPlayerId = [dict["id"]!]
                        let characterArray = self.tempPlayerId.flatMap { String.CharacterView($0) }
                        let newPlayerId = String(characterArray)
                        self.playerId = newPlayerId
                    }
                    if  let tempArray = json?["userStats"] as? [String:AnyObject]{
                        print ("here ", tempArray)

                    }

print命令成功打印userStats数组及其所有标题(totalWins,totalDraws,totalLosses ...) -

如何将此数组存储到我的全局变量var userStatsArray = [AnyObject]()中,以便将其传递给另一个VC?

2 个答案:

答案 0 :(得分:0)

最好创建一个这样的自定义类,并使用该自定义类类型声明该数组。然后将userStats对象转换为自定义类类型。

class userStats: NSObject {

var totalDraws: NSNumber?
var totalLosses: NSNumber?
var totalWins: NSNumber?

    init(totalDraws: NSNumber?, totalLosses: NSNumber?, totalWins: NSNumber?) {
        self.totalDraws = totalDraws
        self.totalWins = totalWins
        self.totalLosses = totalLosses
    }

}

var userStatsArray = [userStats]()

//改变你的代码

let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
              print (json!)

              if let arr = json?["communities"] as? [[String:String]] {
                    self.communitiesArray = arr.flatMap { $0["name"]!}
                    self.communityIdsArray = arr.flatMap { $0["id"]!}

                }

                if let dict = json?["userDetails"] as? [String:String] {
                  self.tempPlayerId = [dict["id"]!]
                    let characterArray = self.tempPlayerId.flatMap { String.CharacterView($0) }
                    let newPlayerId = String(characterArray)
                    self.playerId = newPlayerId
                }
                if  let tempArray = json?["userStats"]as? userStats {

                    userSytatsArray.append(tempArray)
                }

答案 1 :(得分:0)

看看ObjectMapper!使用这个功能强大的框架,您可以创建API返回的数据的可映射模型,并让它为您执行整个工作:)

声明你的模型类:

class UserInfo: Mappable {
var communities : [Community]?
var stats: UserStats?
var details: UserDetails?

required init?(map: Map) {
}

func mapping(map: Map) {
    communities <- map["communities"]
    stats <- map["userStats"]
    details <- map["userDetails"]
 }
}

class Community: Mappable {
var id: Int!
var name: String!

required init?(map: Map) {
}

func mapping(map: Map) {
    id <- map["id"]
    name <- map["name"]
 }
}

class UserStats: Mappable {
var totalDraws : Int!
var totalLosses : Int!
var totalWins : Int!

required init?(map: Map) {
}

func mapping(map: Map) {
    totalDraws <- map["totalDraws"]
    totalLosses <- map["totalLosses"]
    totalWins <- map["totalWins"]
 }
}

class UserDetails: Mappable {
var id : Int!
var email : String!
var username : String!

required init?(map: Map) {
}

func mapping(map: Map) {
    id <- map["id"]
    email <- map["user_email"]
    username <- map["user_name"]
 }
}

后来只是:

let user = UserInfo(JSONString: JSONString)