我正在写一个字符串到这样的文件:
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt", nameOfFile]];
[fileString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
由于某种原因,TextEdit无法识别NSUTF8StringEncoding
。我怎样才能确定它呢?
感谢一些帮助,Fabian
答案 0 :(得分:1)
我刚刚重新创建了代码,它对我来说很好。 TextEdit可以很好地读取文件。
#import <Cocoa/Cocoa.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *fileString = @"Blah blah.";
NSString *nameOfFile = @"blah";
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt", nameOfFile]];
printf("fileString:\n%s\n", [fileString UTF8String]);
printf("docsDirectory:\n%s\n", [docsDirectory UTF8String]);
printf("path:\n%s\n", [path UTF8String]);
/*
fileString:
Blah blah.
docsDirectory:
/Users/daniel/Documents
path:
/Users/daniel/Documents/blah.txt
*/
[fileString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
[pool release];
return 0;
}
我唯一能想到的是你的TextEdit偏好,这是我的: