我想创建一个.json文件和.text文件public,以便NSItemProvider可以读取它。我想以编程方式创建文件。
答案 0 :(得分:0)
您能否参考此链接,这将有助于玩文件创建,删除等...
答案 1 :(得分:0)
使用以下代码在应用的Documents目录中编写/创建 .txt 文件:
let lastName:String? = self.userDict.objectForKey("last_name")
self.lblLastName.text = "Last Name: " + lastName? //--ERROR: Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?
读取/获取文本文件:
NSError *error;
NSString *stringToWrite = @"hello>!!new file created..";
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"myfile.txt"];
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];