1. 程式人生 > >iOS儲存的幾種方式

iOS儲存的幾種方式

1.寫檔案

NSData *dat = [[NSDataalloc]initWithContentsOfURL:[NSURLURLWithString:imageUrl]];

NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[NSStringstringWithFormat:@"DocumentDataF"]];

        [dat writeToFile:fullPath atomically:NO

];

        UIImage *img = [UIImage imageWithContentsOfFile:fullPath];

2.物件序列化儲存

2.1匯入第三方MJExtension檔案

2.2將你要存入的模型.m檔案里加上

MJCodingImplementation

2.3使用下列方法存取

儲存陣列(自定義物件)NSUserDefaults

+ (void)userDefaultsArchiveCustomArray:(NSMutableArray *)array withKey:(NSString *)key{

NSData *data = [NSKeyedArchiver

archivedDataWithRootObject:array];

NSUserDefaults *userDefaults = [NSUserDefaultsstandardUserDefaults];

    [userDefaults setObject:data forKey:key];

    [userDefaults synchronize];

}

NSUserDefaults解析出陣列(自定義物件)

+ (NSMutableArray *)userDefaultsUnarchiveArrayWithKey:(NSString *)key{

NSUserDefaults *userDefaults = [

NSUserDefaultsstandardUserDefaults];

    NSData *data = [userDefaults objectForKey:key];

NSMutableArray *array = (NSMutableArray *)[NSKeyedUnarchiverunarchiveObjectWithData:data];

    return array;

}

3.資料庫(sqlite)

匯入FMDataBase檔案

實現資料酷的增刪改查

4.圖片快取

匯入SDWebImage檔案