开发者

Xcode: Read text file from URL

开发者 https://www.devze.com 2023-01-11 04:09 出处:网络
I\'m new to Xcode. I have the following code working perfectly to read a text file located in the project bundle.

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开发者_如何学JAVA" ofType:@"txt"];
NSString *textFromFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

My question is how to read the text file from URL?


It's a bit more involved than reading a file. Use the NURLConnection class.


I Found a Way to Read the File By Doing This:

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")
}
0

精彩评论

暂无评论...
验证码 换一张
取 消