天天看點

swift4.0 txt檔案json取出轉model

func chooseTxtContent() -> Void {

guard

let url = Bundle.main.url(

forResource: "source/AllCountry/zh_Hans_CNCountry",

withExtension: "txt") else {

return

}

struct DataModel: Codable {
        let data: [CountryModel]?
    }
    
    do {
        let data = try Data(contentsOf: url)
        let model = try JSONDecoder().decode(DataModel.self, from: data)
        dataArray = model.data
    } catch {
        print(error)
    }
}
           

//

// CountryModel.swift

// chooseCountry

// Created by iOS on 2018/2/26.

// Copyright © 2018年 iOS. All rights reserved.

import UIKit

struct CountryModel : Codable {

let countryName : String?

let countryPinyin : String?

let phoneCode : String?

let countryCode : String?