iOS数据持久化

/** NSUserDefaults */
    //  NSData、NSString、NSNumber、NSDate、NSArray、NSDictionar
    
    static NSString* const key = @"key";
    
    [[NSUserDefaults standardUserDefaults] setValue:@"key123" forKey:key];
    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"key2"];
    [[NSUserDefaults standardUserDefaults] setValue:@2 forKey:@"key3"];
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"key4"];
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"key5"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    NSString *string = [[NSUserDefaults standardUserDefaults] valueForKey:key];
    NSString *string2 = [[NSUserDefaults standardUserDefaults] valueForKey:@"key2"];
    NSNumber *number3 = [[NSUserDefaults standardUserDefaults] valueForKey:@"key3"];
    NSString *string4 = [[NSUserDefaults standardUserDefaults] objectForKey:@"key4"];
    NSString *string5 = [[NSUserDefaults standardUserDefaults] objectForKey:@"key5"];
/** 属性列表plist */
 //序列化对象(serialized object):指可以被转换为字节流以便于存储到文件中或通过网络进行传输的对象
    /*
     可以被序列化的类型只有如下几种:
     NSArray
     NSMutableArray
     NSDictionary
     NSMutableDictionary
     NSData
     NSMutableData
     NSString
     NSMutableString
     NSNumber
     NSDate
     */
    // 路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = paths.firstObject;
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"data.plist"];
    
    // 桌面路径
    NSString *desktopPath = @"/Users/xp/Desktop";
    desktopPath = [desktopPath stringByAppendingPathComponent:@"data1.plist"];
    
    // 存储
    NSArray *array = @[@1,@2,@3,@4];
    [array writeToFile:desktopPath atomically:YES];
    
    // 读取
    if ([[NSFileManager defaultManager]fileExistsAtPath:desktopPath]) {
        // 确认路径存在
        NSArray* arr = [[NSArray alloc]initWithContentsOfFile:desktopPath];
        NSLog(@"arr: %@",arr);
    }


/** NSKeyedArchiver 对象归档 */
    // 在Cocoa中,Archiver是另一种形式的序列化,是任何对象都可实现的更常规的类型
    // 只有遵守了NSCoding或 NSSecureCoding(更为安全的归档协议)协议,并且实现了协议里归档与解归档的方法的的类创建的对象才能够进行归档
    // 最好也实现以下NSCopying,NSCopying与NSCoding一起实现好处在于允许复制对象,使用数据模型对象时有较大的灵活性
    // 桌面路径
    NSString *desktopPath = @"/Users/xp/Desktop";
    desktopPath = [desktopPath stringByAppendingPathComponent:@"data1.plist"];
    
    // 存储
    FourLines *lines = [[FourLines alloc] init];
    lines.lines = @[@1,@3,@5,@10];
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:lines forKey:@"kRootKey"];
    [archiver finishEncoding];
    
    [data writeToFile:desktopPath atomically:YES];
    
    // 读取
    if ([[NSFileManager defaultManager]fileExistsAtPath:desktopPath]) {
        NSData *data = [[NSMutableData alloc]initWithContentsOfFile:desktopPath];
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
        FourLines *four = [unarchiver decodeObjectForKey:@"kRootKey"];
        [unarchiver finishDecoding];
        for (int i = 0; i < 4; i++) {
            //to do
        }
    }

// 协议实现
#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.lines forKey:klinesKey];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        _lines = [aDecoder decodeObjectForKey:klinesKey];
    }
    return self;
}

#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone {
    FourLines *copy = [[[self class] allocWithZone:zone] init];
    NSMutableArray *linsCopy = [NSMutableArray array];
    for (id line in self.lines) {
        [linsCopy addObject:[line copyWithZone:zone]];
    }
    copy.lines = linsCopy;
    return copy;
}

你可能感兴趣的:(iOS数据持久化)