迭代NSDictionary并将其保存到对象types的数组中

正如标题所说,我想遍历NSDictionary并将单个条目保存到对象types的数组中。 我目前正在努力读出NSDictionary的单个条目,因为我不能简单地用entry [index]调用它们并向上计数。

我有一个名为“客户”的本地JSON文件,我很快就进入了。 该文件如下所示:

{ "clients": [ { "id": 3895, "phone": 0787623, "age" : 23, "customerSince" : 2008 }, { "id": 9843, "phone": 7263549, "age" : 39, "customerSince" : 2000 }, { "id": 0994, "phone": 1093609, "age" : 42, "customerSince" : 1997 } ] } 

我做了什么? 我创build了一个类ClientObject,看起来像这样:

 import Foundation class ClientObjects{ var id : Int = 0; var phone : Int = 0; var age : Int = 0; var customerSince : Int = 0; } 

接下来,我意识到JSON导入到Swift,它正常工作。 当我debugging会话时, jsonResultvariables包含JSON的数据。 但不知何故,我无法设法将数据处理到对象typesClientObjects的数组中。

  // instantiate the ClientObjects as an Array var clientsArray = [ClientObjects](); func getData() { if let path = NSBundle.mainBundle().pathForResource("Clients", ofType: "json") { if let jsonData = NSData(contentsOfFile:path) { do { if let jsonResult = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableLeaves) as? NSDictionary { //print(jsonResult["clients"]); // this print call returns the whole content of the JSON array if let clients = jsonResult["clients"] as? [[String: AnyObject]] { for client in clients { clientsArray.append(client); } catch let error as NSError { print("API error: \(error.debugDescription)") } } } } } 

我的目标是,我可以打印客户的单一属性,如:

 clientsArray[0].id; 

有人可以就我的问题给我一些build议吗?

首先我们来看看ClientObject。

  1. 它应该是一个struct
  2. 它应该简单地命名为Client
  3. 属性应该是没有默认值的常量
  4. 它应该有一个failable初始值设定项
  5. phone应该是一个String (请相应地更改您的JSON)

喜欢这个

 struct Client { let id: Int let phone: String let age: Int let customerSince: Int init?(json:[String:Any]) { guard let id = json["id"] as? Int, phone = json["phone"] as? String, age = json["age"] as? Int, customerSince = json["customerSince"] as? Int else { return nil } self.id = id self.phone = phone self.age = age self.customerSince = customerSince } } 

现在我们来阅读JSON

 func getData() { do { guard let path = NSBundle.mainBundle().pathForResource("Clients", ofType: "json"), jsonData = NSData(contentsOfFile:path), jsonResult = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableLeaves) as? NSDictionary, jsonClients = jsonResult["clients"] as? [[String: Any]] else { return } let clients = jsonClients.flatMap(Client.init) // <- now you have your array of Client } catch let error as NSError { print("API error: \(error.debugDescription)") } } 

您必须创buildClientObjects对象并将值分配给相应的属性。

 ... let jsonResult = try NSJSONSerialization.JSONObjectWithData(jsonData, options: []) as! [String:AnyObject] if let clients = jsonResult["clients"] as? [[String: Int]] { for client in clients { let newClient = ClientObjects() newClient.id = client["id"]! newClient.phone = client["phone"]! newClient.age = client["age"]! newClient.customerSince = client["customerSince"]! clientsArray.append(newClient); } } ... 

build议在这种情况下使用单数forms命名类ClientObject

而且, if - letNSJSONSerialization行中if - let类似NSDictionaryMutableLeaves选项的基础types也不行。