开发者

Save NSmutableArray to file and load

i am written app for my iphone and i want to save some NSmutablearray to file and load it later. i read about NSArchiver but didnt understand how to work with my calss is :

@interface Shop : NSObject 
{    
    NSString       *m_Name;
  开发者_运维百科  NSString       *m_Description;
    NSString       *m_Category;
    NSString       *m_BestSaleDesc;
    NSString       *m_AdditionalInfo;   
    NSInteger       m_ShopId;
    NSInteger       m_ChainId;
    NSMutableArray *m_SalesList;
    NSData         *m_Icon;
    bool            m_Filtered;
    bool            m_Deleted;
    bool            m_Hidden;
}

can someone give me example code of how to save an NSmutableArray of calss Shop to file name ShopFile.sav and then how to load it again to NSmutableArray object.

thanks so much


EDIT: Redid the entire thing.
Saving:

NSMutableDictionary *saveDict = [NSMutableDictionary dictionary];
[saveDict setValue:m_Name forKey:@"m_Name"];
[saveDict setValue:m_Description forKey:@"m_Description"];
[saveDict setValue:m_Category forKey:@"m_Category"];
[saveDict setValue:m_BestSaleDesc forKey:@"m_BestSaleDesc"];
[saveDict setValue:m_AdditionalInfo forKey:@"m_AdditionalInfo"];
[saveDict setValue:[NSNumber numberWithInt:m_ShopId] forKey:@"m_ShopId"];
[saveDict setValue:[NSNumber numberWithInt:m_ChainId] forKey:@"m_ChainId"];
[saveDict setValue:m_SalesList forKey:@"m_SalesList"];
[saveDict setValue:m_Icon forKey:@"m_Icon"];
[saveDict setValue:[NSNumber numberWithBool:m_Filtered] forKey:@"m_Filtered"];
[saveDict setValue:[NSNumber numberWithBool:m_Deleted] forKey:@"m_Deleted"];
[saveDict setValue:[NSNumber numberWithBool:m_Hidden] forKey:@"m_Hidden"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:@"/ShopFile.sav"];
[saveDict writeToFile:filePath atomically:YES];

Loading:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:@"/ShopFile.sav"];
NSDictionary *loadDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
m_Name =            [loadDict valueForKey:@"m_Name"];
m_Description =     [loadDict valueForKey:@"m_Description"];
m_Category =        [loadDict valueForKey:@"m_Category"];
m_BestSaleDesc =    [loadDict valueForKey:@"m_BestSaleDesc"];
m_AdditionalInfo =  [loadDict valueForKey:@"m_AdditionalInfo"];
m_ShopId =         [[loadDict valueForKey:@"m_ShopId"] intValue];
m_ChainID =        [[loadDict valueForKey:@"m_ChainId"] intValue];
m_SalesList =       [loadDict valueForKey:@"m_SalesList"];
m_Icon =            [loadDict valueForKey:@"m_Icon"];
m_Filtered =       [[loadDict valueForKey:@"m_Filtered"] boolValue];
m_Deleted =        [[loadDict valueForKey:@"m_Deleted"] boolValue];
m_Hidden =         [[loadDict valueForKey:@"m_Hidden"] boolValue];

Should work better ;)


Why not use Core Data? Core Data

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜