Donovan
Donovan

Reputation: 6142

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 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.

Upvotes: 2

Views: 340

Answers (2)

apod
apod

Reputation: 106

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];
}

Upvotes: 0

diciu
diciu

Reputation: 29343

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];

Upvotes: 7

Related Questions