在iOS中创建公共的.json或.txt文件?

时间:2015-10-30 06:42:24

标签: ios objective-c json text-files nsfilemanager

我想创建一个.json文件和.text文件public,以便NSItemProvider可以读取它。我想以编程方式创建文件。

2 个答案:

答案 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];