当前位置: 代码迷 >> 综合 >> iOS Plist,NSUserDefault,归档总结
  详细解决方案

iOS Plist,NSUserDefault,归档总结

热度:37   发布时间:2023-12-20 23:10:45.0

最近总结iOS本地化策略,,还是5种策略,Plist,NSUserDefault,归档,sqlite,苹果自带的coreData

Plist

这是比较常用的明文本地存储方式,全名属性列表,像app的配置文件等的信息,一般后缀为.plist,文件的格式以XML的形式展现。优点就是用起来比较方便,缺点就是容易暴露信息。

因为是本地的东西,所以需要存储路径,先创建本地路径

    //plist文件的创建//获取应用程序沙盒的Documents目录NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);NSString *plistPath = [paths objectAtIndex:0];//创建字典 保存数据NSMutableDictionary *dictTest=[[NSMutableDictionary alloc]init];[dictTest setObject:@"1" forKey:@"test"];//得到完整的文件名NSString *filename = [plistPath stringByAppendingPathComponent:@"test.plist"];//输入写入[dictTest writeToFile:filename atomically:YES];//plist文件的读取NSMutableDictionary *dictRead = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];NSLog(@"dictRead:%@",dictRead);

NSUserDefault

这是苹果官方指定的存储方式,而且是单例的形式,就像是通知,用起来十分方便,但是一般是存比较小的数据,比如用户的个人信息等,如果存储大的数据,如果还不是异步线程保存会卡住主线程

他的存取方式类似于字典的键值存取,但是字典只能存取对象,字符串等的信息,但是NSUserDefault可以存取例如BOOL,Interger等的类型。

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];//存储[defaults setObject:@"abc" forKey:@"name"];[defaults setInteger:10 forKey:@"age"];UIImage *image =[UIImage imageNamed:@"testImage.jpg"];NSData *imageData = UIImageJPEGRepresentation(image, 1);//把image归档为NSData[defaults setObject:imageData forKey:@"image"];//读取NSString *nameRead = [defaults objectForKey:@"name"];NSInteger ageRead = [defaults integerForKey:@"age"];NSData *imageReadData = [defaults dataForKey:@"image"];UIImage *imageRead = [UIImage imageWithData:imageReadData];NSLog(@"nameRead:%@ ageRead:%ld imageRead:%@",nameRead,ageRead,imageRead);

归档作为轻量级的存储方案(相对于数据库),既可以利用NSCoding存储对象,又可以存储IOS的遵守NSCoding的NSString等的对象,这样更适合于日常的开发。

归档分为系统归档和自定义归档

系统归档

    //获取应用程序沙盒的Documents目录NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);NSString *plistPath = [paths objectAtIndex:0];//创建字典 保存数据NSMutableDictionary *dictTest=[[NSMutableDictionary alloc]init];[dictTest setObject:@"1" forKey:@"test"];//得到完整的文件名NSString *filename = [plistPath stringByAppendingPathComponent:@"test.archiver"];//写入[NSKeyedArchiver archiveRootObject:dictTest toFile:filename];//读取NSMutableDictionary *dictRead = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];NSLog(@"dictRead:%@",dictRead);

自定义归档

自定义归档通常需要自定义继承于NSObject的对象

#import <Foundation/Foundation.h>@interface Engine : NSObject <NSCoding>
@property (nonatomic,copy) NSString *name;
@property (nonatomic) int weight;
@end

#import "Engine.h"@implementation Engine//归档调用
- (void)encodeWithCoder:(NSCoder *)aCoder{[aCoder encodeObject:self.name forKey:@"name"];[aCoder encodeInt:self.weight forKey:@"weight"];
}
//读档调用
- (id)initWithCoder:(NSCoder *)aDecoder{if (self = [super init]) {self.name = [aDecoder decodeObjectForKey:@"name"];self.weight = [aDecoder decodeIntForKey:@"weight"];}return self;
}@end

具体的写入和读取

    //获取应用程序沙盒的Documents目录NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);NSString *plistPath = [paths objectAtIndex:0];//得到完整的文件名NSString *filename = [plistPath stringByAppendingPathComponent:@"test.archiver"];Engine *engine = [[Engine alloc] init];engine.name = @"德国";engine.weight = 10;//写入[NSKeyedArchiver archiveRootObject:engine toFile:filename];//读取Engine *engineRead = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];NSLog(@"name:%@ engine:%d",engineRead.name,engineRead.weight);

以上就是三种iOS轻量级的存取方式,demo比较简单

其中有小技巧

1 创建一个父对象 在.m中导入MJExtension的声明

MJExtensionCodingImplementation
可以自动归档和解档对象之后继承父类的对象可以直接存取,方便快捷