1.LXFileManager.h
#import <Foundation/Foundation.h>
/// 文件管理类
@interface LXFileManager : NSObject
/// 把对象归档存到沙盒里
+(void)saveObject:(id)object byFileName:(NSString*)fileName;
/// 通过文件名从沙盒中找到归档的对象
+(id)getObjectByFileName:(NSString*)fileName;
/// 根据文件名删除沙盒中的 plist 文件
+(void)removeFileByFileName:(NSString*)fileName;
/// 存储用户偏好设置 到 NSUserDefults
+(void)saveUserData:(id)data forKey:(NSString*)key;
/// 读取用户偏好设置
+(id)readUserDataForKey:(NSString*)key;
/// 删除用户偏好设置
+(void)removeUserDataForkey:(NSString*)key;
@end
2.LXFileManager.m
#import "LXFileManager.h"
@implementation LXFileManager
/// 把对象归档存到沙盒里
+(void)saveObject:(id)object byFileName:(NSString*)fileName
{
NSString *path = [self appendFilePath:fileName];
[NSKeyedArchiver archiveRootObject:object toFile:path];
}
/// 通过文件名从沙盒中找到归档的对象
+(id)getObjectByFileName:(NSString*)fileName
{
NSString *path = [self appendFilePath:fileName];
return [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}
// 根据文件名删除沙盒中的 plist 文件
+(void)removeFileByFileName:(NSString*)fileName
{
NSString *path = [self appendFilePath:fileName];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
/// 拼接文件路径
+(NSString*)appendFilePath:(NSString*)fileName
{
NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *file = [NSString stringWithFormat:@"%@/%@.archiver",documentsPath,fileName];
return file;
}
/// 存储用户偏好设置 到 NSUserDefults
+(void)saveUserData:(id)data forKey:(NSString*)key
{
if (data)
{
[[NSUserDefaults standardUserDefaults]setObject:data forKey:key];
[[NSUserDefaults standardUserDefaults]synchronize];
}
}
/// 读取用户偏好设置
+(id)readUserDataForKey:(NSString*)key
{
return [[NSUserDefaults standardUserDefaults]objectForKey:key];
}
/// 删除用户偏好设置
+(void)removeUserDataForkey:(NSString*)key
{
[[NSUserDefaults standardUserDefaults]removeObjectForKey:key];
}
@end