PDA

View Full Version : nsarray to nsdata




BollywooD
Oct 25, 2009, 07:04 AM
I have an nsarray of strings i would like to save as a text file of strings on separate lines.

I am using NSFileManager createFileAtPath, and I need to convert the NSArray into NSData first. I have tried this:

NSData *data = [NSArchiver archivedDataWithRootObject:filters];

but this doesnt give the desired results.....

here is the code i am using:

NSData *data = [NSArchiver archivedDataWithRootObject:myArray];
NSFileManager *fm = [NSFileManager defaultManager];
[fm createFileAtPath:[savePanel filename] contents:data attributes:nil];

where myArray, is simply an array of strings.



kainjow
Oct 25, 2009, 01:18 PM
If you want just plain text, use componentsJoinedByString: to create an NSString from the array with each item separated by a new line. Then use writeToFile:atomically:encoding:error: to write it to file.

chown33
Oct 25, 2009, 01:21 PM
See the NSArray method -componentsJoinedByString:.

Edit: I see kainjow a few minutes earlier.

BollywooD
Oct 25, 2009, 03:43 PM
If you want just plain text, use componentsJoinedByString: to create an NSString from the array with each item separated by a new line. Then use writeToFile:atomically:encoding:error: to write it to file.

thanks kainjow, that worked perfectly!
:D