应用数据plist存储方法
OSX或IOS应用数据存储的常用方式:
1. Plist归档
2. Preference(偏好设置)存储
3. NSKeyedArchiver归档
4. SQLite3
5. Core Data
其中,Preference存储,即是NSUserDefault存储,其实也就是存储成Plist文件。
本文主要探讨plist存储,自己coding实现存储的过程。基本的方法就是增、删、改、查吧。
基本功能大概如下:
1. 添加的方法,使用键值对key-value形式存储.
2. 取值方法,通过key来获取value.
3. 删除方法,通过key来删除value.
4. 修改方法,通过key来修改value.
主要提供了一些思路,希望能够起到抛砖引玉的作用。最后附上大致的代码:
@interface DataStorage : NSObject
@property (nonatomic,copy) NSString *savePath;
+(DataStorage *)sharedInstance;
//key-value存储
- (void)setItemValue: (id)value
forKey : (NSString *)key;
//通过key获取value
- (id)itemValueForKey : (NSString *)key;
//通过key删除value
- (void)removeItemValueForKey: (NSString *)key;
//通过一组key删除一组value
- (void)removeItemValueForKeys:(NSArray *)keyArray;
//通过key修改value
- (void)replaceItemValue:(NSString *)newValue forKey:(NSString *)key;
//删除所有的value
- (void)removeAllItemValues;
//获取所存值的个数
- (NSInteger)getItemValueCount;
@end
@implementation DataStorage
@synthesize savePath;
+ (DataStorage *)sharedInstance {
static DataStorage *_sharedManager = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedManager = [[self alloc] init];
});
return _sharedManager;
}
- (id)init {
self = [super init];
if (self) {
NSString *plistPath = [[self documentPath] stringByAppendingString:@"appData.plist"];
self.savePath = plistPath;
[self initPlist];
}
return self;
}
- (void)setSaveFolderPath:(NSString *)saveFolderPath {
self.savePath = saveFolderPath;
}
- (void)setItemValue:(id)value
forKey:(NSString *)key {
NSMutableDictionary *plistDictionary = [[NSMutableDictionary alloc]init];
if ([self isPlistFileExists]) {
NSMutableDictionary *plistExistDictionary = [self readPlist];
if (plistExistDictionary != nil) {
[plistDictionary setDictionary:plistExistDictionary];
}
}
[plistDictionary setValue:value forKey:key];
[self writeToPlist:plistDictionary];
}
- (id)itemValueForKey:(NSString *)key {
NSMutableDictionary *dataDictionary = [self readPlist];
return [dataDictionary objectForKey:key];
}
- (void)removeItemValueForKey:(NSString *)key {
NSMutableDictionary *dataDictionary = [self readPlist];
[dataDictionary removeObjectForKey:key];
[self writeToPlist:dataDictionary];
}
- (void)removeItemValueForKeys:(NSArray *)keyArray {
NSMutableDictionary *dataDictionary = [self readPlist];
[dataDictionary removeObjectsForKeys:keyArray];
[self writeToPlist:dataDictionary];
}
- (void)removeAllItemValues {
NSMutableDictionary *dataDictionary = [self readPlist];
[dataDictionary removeAllObjects];
[self writeToPlist:dataDictionary];
}
- (NSString *)getPlistPath {
if (self.savePath == nil) {
NSString *plistPath = [[self documentPath] stringByAppendingString:@"appData.plist"];
self.savePath = plistPath;
}
return self.savePath;
}
- (BOOL)isPlistFileExists {
NSString *plistPath =[self getPlistPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if( [fileManager fileExistsAtPath:plistPath]== NO ) {
NSLog(@"not exist");
return NO;
}else{
NSLog(@"exist");
return YES;
}
}
- (void)initPlist {
NSString *plistPath = [self getPlistPath];
if (![self isPlistFileExists]) {
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:plistPath contents:nil attributes:nil];
}
}
-(BOOL)isItemValueExistsForKey:(NSString *)key {
NSMutableDictionary *dataDictionary = [self readPlist];
if ([dataDictionary objectForKey:key]) {
NSLog(@"value not exist");
return YES;
}else{
NSLog(@"value exist");
return NO;
}
}
- (void)deletePlist {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *plistPath = [self getPlistPath];
[fileManager removeItemAtPath:plistPath error:nil];
}
- (NSMutableDictionary *)readPlist {
NSString *plistPath = [self getPlistPath];
NSData *plistData = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSMutableDictionary *resultDictionary = (NSMutableDictionary *)
[NSPropertyListSerialization propertyListWithData:plistData
options:0
format:nil
error:nil];
return [resultDictionary mutableCopy];
}
- (void)replaceItemValue:(NSString *)newValue forKey:(NSString *)key {
NSMutableDictionary *plistDictionary = [[NSMutableDictionary alloc]init];
if ([self isPlistFileExists]) {
NSMutableDictionary *plistExistDictionary = [self readPlist];
if (plistExistDictionary != nil) {
[plistExistDictionary removeObjectForKey:key];
[plistDictionary setDictionary:plistExistDictionary];
}
}
[plistDictionary setValue:newValue forKey:key];
[self writeToPlist:plistDictionary];
}
- (NSInteger)getItemValueCount {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
dictionary = [self readPlist];
return [dictionary count];
}
- (void)writeToPlist:(NSMutableDictionary *)plistDictionary {
if (plistDictionary != nil) {
NSString *plistPath = [self getPlistPath];
NSError *error = nil;
NSData *plistData = [NSPropertyListSerialization
dataWithPropertyList:plistDictionary
format:NSPropertyListBinaryFormat_v1_0
options:0
error:&error];
if ([plistData writeToFile:plistPath atomically:YES]) {
NSLog(@"write ok!");
} else {
NSLog(@"write error!");
}
}
}
- (void)generatePlist:dictionary {
[self writeToPlist:dictionary];
}
- (NSString *)documentPath{
NSString *budleIdentifier =[NSString stringWithFormat:@"/%@/",[[NSBundle mainBundle] bundleIdentifier]];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)[0] stringByAppendingString:budleIdentifier];
NSError *err = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&err];
if (err) {
NSLog(@"file err:%@",err);
}
}
return path;
}
@end