在iPhone文档编码问题中创建的doc文件

时间:2010-06-11 07:51:27

标签: iphone

我正在尝试通过以下代码在文档目录中编写MSword文件:

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString* path;
NSString* gradeDoc = [self fetchCommentsDesc];
NSString* str = [self.objStudent.strName stringByAppendingFormat:@".doc"];
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:str];
[gradeDoc writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

[self fetchCommentsDesc] returns NSString.
self.student.strName is a String

问题: 当我打开在iphone文档目录中创建的doc文件时,doc中的所有特殊字符都显示为方框或一些阿拉伯语字符。

请帮忙!

1 个答案:

答案 0 :(得分:0)

你正在写一些UTF-8字节,这不是“.doc”。尝试将其写入“.txt”文件。

此外,您可能必须为某些文本编辑器写一个“字节顺序标记”,以注意它是UTF-8或UTF-16(在NS / CF-land中,这称为“外部表示”):

NSData * d = [(id)CFStringCreateExternalRepresentation(NULL, (CFStringRef)gradeDoc, kCFStringEncodingUTF8, 0) autorelease];
[d writeToFile:path atomically:YES];

还可以尝试使用kCFStringEncodingUnicode和kCFStringEncodingUTF16。