NSData -writeToFile type methods
By boyfarrell at Fri, May 4 2007 5:01am |
Hello everyone,
I'm using NSData following one of Drew's posts (http://www.macresearch.org/cocoa_for_scientists_part_iv_good_references), see the comments section.
My NSData represents an array of doubles. Is it possible to use the built in '-writeToFile...' methods to write my data in human readable form? Or do I have todo this myself by reading my doubles into a string then using NSStrng's '-writeToMethods'?
Cheers,
Dan.




NSData to file
If you use writeToFile:, the data will be written in raw, unreadable form. So, yes, you whould have to either add the doubles to a string using methods like stringWithFormat:, or you could write them out using the C printf function. If you are just writing the data for debugging purposes, consider NSLog, which works like printf.
Drew
---------------------------
Drew McCormack
http://www.maccoremac.com
http://www.macanics.net
http://www.macresearch.org
Hi,Thanks Drew, I decided
Hi,
Thanks Drew. I decided the simplest way to proceed was to write the data into a NSMutableString and then piggy-back on it's -writeToFile method. I guess the advantage of using NSData's -writeToFile: is that less interpretation has to be done, so it should be faster to bouce data between RAM to disk and vica versa. However, I rarely will use this method so it shouldn't slow me down too much.
Cheers,
Dan.
[CODE]
- (void) writeToFile:(NSString*)address
{
NSMutableArray *array = [NSMutableArray new];
unsigned i;
for (i=0; i < count; i++)
{
double value = [self valueAtIndex:i];
[array addObject: [NSNumber numberWithDouble:value]];
}
NSString *delimitedContents = [array componentsJoinedByString:@"\n"];
[delimitedContents writeToFile:address atomically:YES];
}
[/CODE]