简体   繁体   中英

Is it possible to save an NSMutableArray with NSKeyedArchiver into Core Data?

I have 2 NSMutableArrays in my project and was told that since Core Data doesn't support NSMutableArray that I would have to archive and unarchive it with NSKeyedArchiver to be able to save it to Core Data. I've implemented an NSFetchResultsController for my tableview, how would I go about saving the NSMutableArray in the the NSKeyedArchiver and then using that in Core Data? I'll post some code below of my arrays and the NSKeyArchiver.

- (instancetype)init {
    self = [super init];
    if (self) {
    self.nameList = [NSMutableArray arrayWithObjects:@"1",
                                                   @"2",
                                                   @"3",
                                                   @"4",
                                                   @"5",
                                                   nil];


   self.descArray = [NSMutableArray  arrayWithObjects:@"desc1",
                                                      @"desc2",
                                                      @"desc3",
                                                      @"desc4",
                                                      @"desc5",
                                                      nil];
                   }
    return self;
}

// How arrays are archived
NSData *titleData = [NSKeyedArchiver archivedDataWithRootObject: self.nameList];

.... //Not sure what to do with the archived data

NSData *descData = [NSKeyedArchiver archivedDataWithRootObject: self.descArray];

....

What would I do with the two arrays now that they are archived? How would I be able to add to them later?

My Core Data model is simple it is an entity called "Data" and has 2 attributes of type string called "title" and "desc". The 2 attributes are used to store the arrays.

My hope for this project is to allow the user to add new objects to the tableview, on top of the existing data that make up the cells already, and I want this done in Core Data. I'm aware of NSFetchResultController but am having trouble bringing this all together to get it to work, any help to steer me in the right direction is welcomed

You can save immutable data in core data. U can go with mutable to immutable after that you can by NSKeyedArchiver. Same things with NSUserDefaults.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM