pod 'CSV.swift', '~> 2.4.3'
实现代码
import UIKit
import Foundation
import CSV
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
print(documentsPath)
let filePath = documentsPath + "/" + "file.csv"
let stream = OutputStream(toFileAtPath: filePath, append: false)!
let csv = try! CSVWriter(stream: stream)
if let url = Bundle.main.url(forResource: "Localizable", withExtension: "strings"),
let stringsDict = NSDictionary(contentsOf: url) as? [String: String] {
try! csv.write(row: ["English","Spanish"])
for key in stringsDict.keys {
try! csv.write(row: [key, stringsDict[key]!])
}
}
csv.stream.close()
}
}