2
votes

In a document based application I am able to save/read from a file, but the problem comes when I have to read from a pure plain text file.
I have only a string to read/write, so that's the code:

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    code=[codeView string];
    return [NSKeyedArchiver archivedDataWithRootObject: code];
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
    code=[NSKeyedUnarchiver unarchiveObjectWithData: data];
    return YES;
}

The problem is that saving a NSData may produce a file like this one:

bplist00‘X$versionX$objectsY$archiverT$top�܆£
U$null“ 
V$classYNS.stringÄP“Z$classnameX$classes_NSMutableString£XNSStringXNSObject_NSKeyedArchiver—TrootÄ#-27;AFMWYZ_jsÖâíõ≠∞µ����������������������������∑

Unreadable for humans, and unreadable for other applications like text edit.I need to be able to read and save plain text files, in my plist file I already tried to achieve this by changing the document type name to public.text.
How do I save/read a pure string, encoded in UTF-8?

2

2 Answers

3
votes

That gobbledygook is the output of an NSKeyedArchiver. You don't want an archiver here. Instead, just ask the string for its dataUsingEncoding:, passing whatever encoding you'd like to use for the text (usually UTF-8 these days).

1
votes

@RamyAlZuhouri: check out the "Document types" in the file "Info-TextEdit.plist" from the "TextEdit"-Project (Apple Code-Example).

Probably here: ~/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleOSX10_8.CoreReference.docset/Contents/Resources/Documents/samplecode/TextEdit