Есть некоторые данные, которые я сохраняю, когда человек вписывает свое имя и фамилию в Text Field. Как я могу эти данные сохранять в .txt файл?
1 ответ
Swift 3.0
let file = "file.txt" //this is the file. we will write to and read from it
let text = "some text" //just a text
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let path = dir.appendingPathComponent(file)
//writing
do {
try text.write(to: path, atomically: false, encoding: String.Encoding.utf8)
}
catch {/* error handling here */}
//reading
do {
let text2 = try String(contentsOf: path, encoding: String.Encoding.utf8)
}
catch {/* error handling here */}
}
для других свифтов найдешь код здесь: https://stackoverflow.com/questions/24097826/read-and-write-data-from-text-file
-
Использую Swift 3.0, но этот способ не записывает данные. Открываю после загрузки файл и там пусто 31 мая 2017 в 5:40
-
пройди по линке и попробуй другие способа описанные там) 31 мая 2017 в 15:06