Writing and reading custom object to file IOS

前端 未结 3 1957
盖世英雄少女心
盖世英雄少女心 2021-01-07 06:05

i have this object.

@interface SeccionItem : NSObject 
{
    NSString * title;
    NSString * texto;
    NSArray * images;
}

@property (nona         


        
3条回答
  •  无人及你
    2021-01-07 06:41

    Take a look at the documentation of NSKeyedArchiver, especially the archiveWithRootObject:toFile: method.

    The path is basically where the file should be stored including the file name. For example you can store your array in your app Documents folder with file name called Storage. The code snippet below is quite common:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docDir = [paths objectAtIndex: 0]; 
    NSString* docFile = [docDir stringByAppendingPathComponent: @"Storage"];
    

    The method NSSearchPathForDirectoriesInDomains is used instead of absolute path because Apple can be changing the Documents folder path as they want it.

    You can use the docFile string above to be supplied to the toFile parameter of the archiveWithRootObject:toFile: method.

提交回复
热议问题