我编写了一个将txt文件加载到数组中的方法。然而,我对它并不满意,因为它对我的初学者来说看起来非常麻烦(我确信我不需要我的代码的50%),我不知道如何指定我的txt的确切格式文件,例如NSUTF8StringEncoding。如何将txt文件加载到数组中
这里是我的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Sample.txt"];
if (filePath) { // check if file exists - if so load it:
NSString *myText = [NSString stringWithContentsOfFile:filePath];
if (myText) {textView.text=myText;}
}
有关如何擦亮这件事,并指定正确的格式任何建议,我会非常感激。
@Nick韦弗:据我所知,文件目录=资源目录 – gd1 2011-03-22 22:29:12
谢谢!将相应地更新代码。 – 2011-03-23 08:34:17
欢迎您,n.evermind – gd1 2011-03-24 20:31:37