1

I have an NSMutableArray created storing song names and artists. I am trying to convert this array to NSData format so that I can save it to a text file. When attempting to do so, the output I get is unreadable:

bplist00ÔghX$versionX$objectsY$archiverT$top † ¯$ !")-./3459:;?@AEFGKLMQRSWXY]^_cU$nullÒ R$0V$class€€#Ò ZNS.objectsª€€€ € ...etc.

The code I'm using to convert from an NSMutableArray to NSData is:

NSFileManager *fm;

NSData *dataCache = [NSKeyedArchiver archivedDataWithRootObject:myList];

fm = [NSFileManager defaultManager];

if ([fm createFileAtPath:@"/users/nicholasvogler/desktop/Mysongs.txt" contents:dataCache  attributes:nil] == NO)

    {
        NSLog (@"Could not create data file");
        return 1;
    }

All objects in the array are NSStrings and I've added the following method for the strings and the array:

-(void) encodeWithCoder:(NSCoder *)encoder
{

    [encoder encodeObject: songname];
    [encoder encodeObject: artist];
}
Lefteris
  • 14,550
  • 2
  • 56
  • 95

2 Answers2

4

What exactly are you expecting the output to be? What you have there is a binary plist. That's what archived data (e.g. NSKeyedArchiver) emits. There's an XML format as well, which can be accessed via NSPropertyListSerialization, although nobody uses the XML format for archived data because the actual plist representation is an implementation detail.

So despite the fact that it's an unreadable binary blob to you, it still encodes what you want, and you can decode it again with NSKeyedUnarchiver.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • The expected output is: songName artist – Nicholas Vogler Dec 12 '12 at 17:10
  • Is it possible to transfer the data from an array to a NSData object without archiving it? I'm just trying to get the data from the array, readable, to the text file. For the life of me I can't make it work. – Nicholas Vogler Dec 12 '12 at 20:10
  • @NicholasVogler: Sure. But that depends entirely on how you want the output to look. If your only goal is "make it human-readable", then you can construct an `NSArray*` of `NSDictionary*` objects that contain the same information as your class, and use `NSPropertyListSerialization` to output an XML plist. It's more work to convert between your objects and the `NSDictionary`s, but this way you can get an XML plist. – Lily Ballard Dec 12 '12 at 20:37
  • @NicholasVogler: Alternatively if you want to just see "songname artist" on each line, you could just build an `NSString*` by iterating over your objects and using `[mutstr appendFormat:@"%@ %@\n", obj.songname, obj.artist]`. Then output that string to a file. – Lily Ballard Dec 12 '12 at 20:38
  • Thanks, I appreciate the help. Although, I'm required to create the array, sort it, and then save it directly to a .txt file. Simply using the writeToFile would be prefered, but my NSMutableArray doesn't recognize it. I've tried the exact code here as well (http://stackoverflow.com/questions/11159206/saving-array-to-file-objective-c) and method's aren't being recognized. – Nicholas Vogler Dec 12 '12 at 21:51
  • @NicholasVogler: You can only use the `-writeToFile:...` methods when your data is comprised entirely of plist-compatible classes, meaning `NSDictionary`, `NSArray`, `NSString`, `NSNumber`, `NSData`, and `NSDate`. – Lily Ballard Dec 12 '12 at 21:54
  • I have two NSString's (songname and artist) which are combined into another NSString (track) and then into NSMutableArray (list). My method to add tracks into the array is as follows: [list addObject: theSong]; Would that prevent me from being able to use the writeToFile method? – Nicholas Vogler Dec 12 '12 at 22:08
  • @NicholasVogler: If you ultimately have an `NSArray` containing just `NSString`s, then you can go ahead and use `-writeToFile:...` – Lily Ballard Dec 12 '12 at 22:35
0

If all objects in your array are strings (or dictionaries containing strings), it seems to me like it would make more sense to save it as JSON.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151