Using NSKeyedArchiver to store custom data model

后端 未结 3 1904
星月不相逢
星月不相逢 2021-02-06 00:45

I\'m working on a rss reader. It is just a tableview and each cell shows a custom data model RSSEntry. And I have a NSMutableArray allEntries which contains all RSSEntry I got f

3条回答
  •  爱一瞬间的悲伤
    2021-02-06 01:08

    You could store an NSDictionary as following

    NSDictionary *Your_NSDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                   @"Obj1", @"Key1",
                                   @"Obj2", @"Key2", nil];
    //store dictionary
    NSMutableData *yourData = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:yourData];
    [archiver encodeObject:Your_NSDictionary forKey: @"key"];
    archiver finishEncoding];
    [yourData writeToFile:@"FilePath" atomically:YES];
    [yourData release];
    [archiver release];
    
    
    //Load dictionary
    
    NSData *yourData = [[NSMutableData alloc]initWithContentsOfFile:@"FilePath"];
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:yourData];
    Your_NSDictionary = [unarchiver decodeObjectForKey: @"key"];
    [unarchiver finishDecoding];
    [unarchiver release];
    [yourData release];
    

提交回复
热议问题