Reputation: 1
I'm new to Xcode. I have the following code working perfectly to read a text file located in the project bundle.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"txt"];
NSString *textFromFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
My question is how to read the text file from URL?
Upvotes: 0
Views: 4217
Reputation: 3
let str = "Text Here"
let filename = getDocumentsDirectory().appendingPathComponent("file.txt")
do {
try str?.write(to: filename, atomically: true, encoding: String.Encoding.utf8)
}catch {
print("Something Went Wrong")
}
Upvotes: 0
Reputation: 185862
It's a bit more involved than reading a file. Use the NURLConnection
class.
Upvotes: 2