내 응용 프로그램에서는 saveURL.txt라는 프로그램의 문서 디렉토리에있는 텍스트 파일에 데이터를 추가하여 나중에 호출하고 구문 분석 할 수 있도록하려고합니다. 내가Swift 3 텍스트 파일에 쓰기
let fileName = "saveURL"
let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("txt")
print("FilePath: \(fileURL.path)")
let writeString = "Write this text to the fileURL as text in iOS using Swift"
do {
// Write to the file
try writeString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
print("past try writeString.write")
} catch let error as NSError {
print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
}
var readString = "" // Used to store the file contents
do {
// Read the file contents
readString = try String(contentsOf: fileURL)
} catch let error as NSError {
print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
}
print("File Text: \(readString)")
있는 내 코드를 콘솔에 표시되는 출력은
FilePath를 :/사용자/마이크/라이브러리/개발자/CoreSimulator/디바이스/752A5025-0AA0-4BE1-B70F-644A904F2D8A/데이터/용기/데이터/응용 프로그램/0084AD99-63DB-439F-8427-DDDF161CFAC0/문서/saveURL.txt
지난 writeString.write
파일의 텍스트를보십시오 사용하여 아이폰 OS에서 텍스트로 fileURL이 텍스트를 쓰기 스위프트
실제로 저장되어있는 출력을 표시하고 있지만 실제로 파일에 추가되었는지 확인하기 위해 파일을 검사 할 때 여전히 비어 있습니다. 나는 신속한 파일은 읽기 전용 일 뿐이라고 생각하지만, 프로그램에서 작동하지만 작동하지 않는 예제를 보았습니다.
게시 한 코드의 'print' 문의 실제 출력과 함께 질문을 업데이트하십시오 (코멘트를 게시하지 마십시오). – rmaddy
* "실제로 파일에 추가되었는지 확인하기 위해 파일을 검사 할 때 여전히 비어 있습니다"*는 무엇을 의미합니까? 너는 어떻게 지내니? 비어있는 것은 무엇입니까? – rmaddy
내 프로그램 파일 디렉토리의 saveURL 파일은 너무 분명하게 쓰여지고 있지만 여전히 빈 텍스트 파일입니다. 파일을 확인하고 아무것도 없는지 확인하기 위해 체크 중입니다. – Kolorbox