Objective-c saving raw text
I implemented saving and loading methods in my document-based application. In the saving method, I have
[NSArchiver archivedDataWithRootObject:[self 开发者_Python百科string]];
Where [self string]
is a NSString.
When saving a file with just "normal content" inside of it, the contents of the file created are:
streamtypedè@NSStringNSObject+normal content
Is there a way to store in a file just raw text?
Thanks for your help.
There are methods inside NSString for saving in a file:
NSString * s = @"Foo bar";
NSError * err = NULL;
BOOL result = [s writeToFile:@"/tmp/test.txt" atomically:YES encoding:NSASCIIStringEncoding error:&err];
Since i am new with cocoa, i don't know if this is the right way to do it or even a valid way.
But after a quick look at the documentation i found this method of NSString instances, - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
A quick try on a sample project it worked fine with:
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
So something like this might work for you:
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
return [[self string] dataUsingEncoding:NSUnicodeStringEncoding];
}
精彩评论