Writing and reading custom object to file IOS

前端 未结 3 1927
盖世英雄少女心
盖世英雄少女心 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:31

    For those who are seeking the solution in swift, I was able to write and read dictionary to file system as follows :

    Write:

    let data = NSKeyedArchiver.archivedData(withRootObject: dictionary)  
    do {
        try data.write(to: destinationPath)
    } catch let error {
        print("\(error.localizedDescription)")
    }
    

    Read:

    do
    {
        let data = try Data.init(contentsOf: path)
        // path e.g. file:///private/var/ .... /Documents/folder/filename
        if let dict = NSKeyedUnarchiver.unarchiveObject(with: data){
            return dict                 
        }
    }
    catch let error
    {
        print("\(error.localizedDescription)")
    }
    

提交回复
热议问题