1

Есть некоторые данные, которые я сохраняю, когда человек вписывает свое имя и фамилию в Text Field. Как я могу эти данные сохранять в .txt файл?

1 ответ 1

2

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

2
  • Использую Swift 3.0, но этот способ не записывает данные. Открываю после загрузки файл и там пусто
    – DanTheMan
    31 мая 2017 в 5:40
  • пройди по линке и попробуй другие способа описанные там) 31 мая 2017 в 15:06

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.