我有 txt 文件的内容:
("All our dreams can come true, if we have the courage to pursue them.","Walt Disney")
("The secret of getting ahead is getting started","Mark Twain")
我想从中获取类型为 [(String, String)] 的元组数组。我尝试使用代码:
do {
if let path = Bundle.main.path(forResource: "quotes", ofType: "txt"){
let data = try String(contentsOfFile: path, encoding: .utf8)
let arrayOfStrings = data.components(separatedBy: "\n")
print(arrayOfStrings[0])
}
} catch let err as NSError {
// do something with Error
print(err)
}
但是有了它,我无法获得元组值。如何使用 Swift 从 txt 文件中获取元组数组?