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?
}

你可能感兴趣的:(swift4.0 txt文件json取出转model)