problem writing a NSMutableArray to file in cocoa

前端 未结 1 1444
无人共我
无人共我 2021-02-10 00:29

A real beginners question. I have a NSView subclass in which I create a NSMutableArray containing NSValues. When I want to write the array to a file using writetofile:atomatical

相关标签:
1条回答
  • 2021-02-10 00:50

    NSValues can't be saved in a plist (which is what writeToFile:atomically: does). Take a look here for the values you can save. (NSNumber is a kind of NSValue you can save, but other NSValues will fail.)

    If you want to save your array with NSValues, you can use archiving instead of writeToFile:atomically:. NSArray and NSValue both support archiving, so you just convert the array to an archive and save that data to a file. (It will include the NSValues as well.) The code looks something like this:

    [NSKeyedArchiver archiveRootObject:myArray toFile:@"myPath"];
    

    To load it, just use:

    NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithFile:@"myPath"];
    
    0 讨论(0)
提交回复
热议问题