遍历 plist 文件并将数据存储在数组或字典中

iterating through plist file and storing data in array or Dictionary

我是 swift 编程新手。我正在尝试学习 plist 文件。我有一个包含国家、州和城市数据的 plist 文件。我想遍历 plist 文件的数据。但我不明白如何创建数组或字典来存储国家、州和城市的数据。你能帮我了解如何操作 plist 文件的以下数据吗?

    {
Country =     (
            {
        CountryName = India;
        State =             (
                            {
                City =                     (
                    Ahmedabad,
                    Vadodara,
                    Surat,
                    Aanand,
                    Bharuch
                );
                StateName = Gujrat;
            },
                            {
                City =                     (
                    Mumbai,
                    Pune,
                    Nagpur,
                    Nasik,
                    Thane
                );
                StateName = Maharastra;
            },
                            {
                City =                     (
                    Kochi,
                    Kanpur,
                    Alleppey,
                    Thrissur,
                    Thiruvananthapuram
                );
                StateName = Kerala;
            }
        );
            },

您可以使用对象映射器

https://github.com/Hearst-DD/ObjectMapper

在哪里,安装pod然后 将你的 class 创建为

    class State : Mappable {

      var stateName: String?
      var city: [String]?

      required init?(map: Map) {}

     func mapping(map: Map) {
         stateName <- map["StateName"]
         city <- map["City"]

     }
 }

 class Country : Mappable {
     var countryName: String?
     var state: [State]?

    required init?(map: Map) {}

    func mapping(map: Map) {
        countryName <- map["CountryName"]
        state <- map["State"]

    }

}

class CountryData: Mappable {
    var countries: [Country]?

    required init?(map: Map) {}

    func mapping(map: Map) {
        countries <- map["Country"]

    }
}

让你的 plist 内容变得像

 var dictRoot: NSDictionary?
 var countryItems: [String:AnyObject]?
    if let path = Bundle.main.path(forResource: "MyCountryList" , ofType: "plist") {
        dictRoot = NSDictionary(contentsOfFile: path)
    }

    if dictRoot != nil
    {
        // Your dictionary contains an array of dictionary
        // Now pull an Array out of it.
        countryItems  = dictRoot as? [String:AnyObject]
        tableView.reloadData()

    }


let countryData  = CountryData(JSON: countryItems)

现在可以随心所欲地使用了

// swift 3 使用结构。手动解析数据。

struct ListData {
    var countries : [Country]?

    init(dict:[String:AnyObject]) {
        if let countryDict = dict["Country"] as? [[String:AnyObject]] {
            self.countries = parseArray(dictArray: countryDict)
        }
    }

    func parseArray(dictArray:[[String:AnyObject]]) -> [Country] {
        var array = [Country]()
        for dict in dictArray {
            let country = Country(dict: dict)
            array.append(country)
        }
        return array
    }
}
struct Country  {
    var countryName : String?
    var states : [State]?

    init(dict:[String:AnyObject]) {
        countryName = dict["CountryName"] as? String

        if let stateDict = dict["State"] as? [[String:AnyObject]] {
            states = parseArray(dictArray: stateDict)
        }
    }

    func parseArray(dictArray:[[String:AnyObject]]) -> [State] {
        var array = [State]()
        for dict in dictArray {
            let state = State(dict: dict)
            array.append(state)
        }
        return array
    }
}
struct State {
    var stateName : String?
    var cities : [String]?

    init(dict:[String:AnyObject]) {
        self.stateName = dict["StateName"] as? String

        if let cityDict = dict["City"] as? [AnyObject] {
            cities = parseArray(dictArray: cityDict)
        }
    }

    func parseArray(dictArray:[AnyObject]) -> [String] {
        var array = [String]()
        for dict in dictArray {
            array.append(dict as! String)
        }
        return array
    }
}

 var listData : ListData? = nil

 if let path = Bundle.main.path(forResource: "Property List" , ofType: "plist") {
            let rootDict = NSDictionary(contentsOfFile: path) as? [String:AnyObject]
            listData = ListData(dict: rootDict!)
    }

// 使用 swift 4 和可编码协议。

struct ListData : Codable {
    var countries : [Country]?

    enum CodingKeys : String, CodingKey {
        case countries =  "Country"
    }

}

struct Country : Codable {
    var countryName : String?
    var states : [State]?

    enum CodingKeys : String, CodingKey {
        case countryName =  "CountryName"
        case states = "State"
    }

}

struct State : Codable {
    var stateName : String?
    var cities : [String]?

    enum CodingKeys : String, CodingKey {
        case stateName =  "StateName"
        case cities = "City"
    }

}

var listData : ListData? = nil
if  let url = Bundle.main.url(forResource: "Property List", withExtension: "plist") {
            if let data = try? Data(contentsOf: url) {
                let decoder = PropertyListDecoder()
                do {
                    listData = try decoder.decode(ListData.self, from: data)
                } catch (let err) {
                    print(err.localizedDescription)
                }
            }
        }