CreateFileAtPath: Content: Attributes Storing NSString

createFileAtPath:contents:attributesfor Objective-C: contentsa type is required NSData, but I want to create a file with the contents NSString. How to do it?

I know this is probably really the main answer, but I'm just starting to learn how to use Objective-C, and it has so many different types of data and objects compared to other languages ​​that I used.

Thank!

+3
source share
1 answer

You can try creating NSDatalines with the contents, for example:

NSString *s = @"Hello, world!";
NSData *d = [s dataUsingEncoding:NSUTF8StringEncoding];

You can also directly save NSStrngto a file:

[s writeToFile:@"/my/file/path/file.txt" atomically:NO encoding:NSUTF8StringEncoding error:nil];

NOTE This answer is edited after a note by Ken Thomases.

+3
source

All Articles