DenVog
DenVog

Reputation: 4286

Save BOM with File

Can someone please tell me how to save the byte order marker (BOM) with a file? For example, I save a text file now like:

NSString *currentFileContent = @"This is a string of text to represent file content.";
NSString *currentFileName = @"MyFileName";
NSString *filePath = [NSString stringWithFormat:@"%@/%@.%@", [self documentsDirectory], currentFileName, rtf];
[currentFileContent writeToFile:filePath atomically:YES encoding:NSUTF16StringEncoding error:&error];

My understanding of BOM is:

The BOM character is the "ZERO WIDTH NO-BREAK SPACE" character, U+FEFF, in the Unicode character set.

I have an iPhone application that allows users to save text to an RTF file. All works fine if I use NSUTF8StringEncoding, unless the user has double-byte characters such as Japanese, or Chinese. The simple answer would seem to be saving the file with NSUTF16StringEncoding which is allowed in more recent RTF specs, except that Microsoft Word can only automatically open UTF-16 files if a BOM is defined.

My hope is that If I can set a generic BOM, I won't need to identify the user's character set as I have no way to know what that is in advance. But they will still be able to open RTF Files with double-byte characters.

Thanks for suggestions or insight.

Upvotes: 3

Views: 728

Answers (1)

ughoavgfhw
ughoavgfhw

Reputation: 39905

If you first convert the string to data, and then write the data to file, then NSString's dataUsingEncoding:allowLossyConversion: will add the BOM for you. You can write your file as follows:

NSData *data = [currentFileContent dataUsingEncoding:NSUTF16StringEncoding];
[data writeToFile:filePath options:NSDataWritingAtomic error:&error];

Upvotes: 1

Related Questions