Last updated 3 years ago
Was this helpful?
๐ ๅ่๏ผSwift Playgrounds โฉ Image Gallery ()
import Foundation extension FileManager { /// URL of the app's document directory. var documentDirectory: URL? { urls(for: .documentDirectory, in: .userDomainMask).first } }
๐ ๅ่๏ผPaul โฉ
struct ContentView: View { var body: some View { VStack { Text("Hello World").onTapGesture { // ๐ FileManager + .documentDirectory guard let dir = FileManager.default.documentDirectory else { fatalError("No Documents directory!") } // โญ๏ธ path to `Documents/message.txt` let url = dir.appendingPathComponent("message.txt") do { let str = "Test Message" // โญ๏ธ write: String -> file (UTF8 encoded) try str.write(to: url, atomically: true, encoding: .utf8) // โญ๏ธ read: file -> String (UTF8 decoded?) let msg = try String(contentsOf: url) print(msg) // "Test Message" } catch { print(error.localizedDescription) } } }.padding() } }
ๅ๏ผใ urls(for: .documentDirectory, in: .userDomainMask).firstๆๅฏ่ฝๆฏ nil ๅโ ใ
urls(for: .documentDirectory, in: .userDomainMask).first
nil
Paul โฉ
Foundation โฉ โฉ
(โญ๏ธ my question)
๐
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!