迭代plist文件并将数据存储在数组或字典中

时间:2018-03-09 03:50:02

标签: ios swift xcode nsarray plist

我是快速编程的新手。我正在尝试学习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;
            }
        );
            },

2 个答案:

答案 0 :(得分:6)

// 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)
                }
            }
        }

答案 1 :(得分:0)

您可以使用Object mapper

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

在哪里,然后安装pod 创建您的课程

    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)

现在您可以随意使用