1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| func affichage(contents: String, sep: String) {
let lines = contents.components(separatedBy: "\n")
for line in lines {
let words = line.components(separatedBy: " ")
print("\(words[0]) is \(words[1]) and likes \(words[4])")
}
}
let file = "file.txt" // ficheir à lire
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let path = dir.appendingPathComponent(file)
// Lecture
do {
let contents = try String(contentsOf: path, encoding: String.Encoding.utf8)
let sep = " "
// passage dans la fonction pour affichage résultats
affichage(contents: String, sep: String)
}
catch {/* erreur handling ici */}
} |
Partager