[wordlist writeToFile:[[NSBundle mainBundle]
pathForResource:@"wordlist" ofType:@"txt"] atomically: YES];
wordlist 是一个 NSMutableArray,剩下的你就知道了。
问题是当我在 Xcode 中运行代码时,wordlist.txt 中没有保存任何内容。前面这段代码nslog显示wordlist中有4个对象。怎么来的?
编辑:对!
这些代码有效!:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *yourArrayFileName = [documentsDirectory stringByAppendingPathComponent:@"wordlist.txt"];
[wordlist writeToFile:yourArrayFileName atomically:YES];
NSLog (@"%@", yourArrayFileName);
2011 年 8 月 29 日编辑。
NSString *documentsDirectory = @"/Users/YOURNAME/Desktop/";
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"norsk.txt"];
NSString *content = @"æ ø å";
BOOL ok = [content writeToFile:appFile atomically:YES encoding:NSUnicodeStringEncoding error:nil];
if (!ok) {
NSLog(@"Error writing file !");
}
也有效!