iOS数据cache

释放双眼,带上耳机,听听看~!
#import <Foundation/Foundation.h>
@class User;
@interface CacheUtility : NSObject
+ (BOOL)archiveUserWithArray:(NSArray<User *> *)array;
+ (NSArray<User *> *)unArchiveUserFromCache;
+ (BOOL)writeToCacheFile:(id)collection;
+ (NSArray *)readArrayFromCacheFile;
@end
static NSString *const archiveFilePath = @"/Users/zhz/Desktop/test/zhz.plist";
static NSString *const writeFilePath = @"/Users/zhz/Desktop/test/zhz2.plist";
@implementation CacheUtility
// -----------archive--------------
+ (BOOL)archiveUserWithArray:(NSArray<User *> *)array {
BOOL isSuccess = [NSKeyedArchiver archiveRootObject:array toFile:archiveFilePath];
if (isSuccess) {
NSLog(@"archive--缓存成功");
} else {
NSLog(@"archive--缓存失败");
}
return isSuccess;
}
+ (NSArray<User *> *)unArchiveUserFromCache {
NSArray<User *> *array = [NSKeyedUnarchiver unarchiveObjectWithFile:archiveFilePath];
if (!array) {
array = [NSArray array];
}
return array;
}
// -----------写入plist--------------
+ (BOOL)writeToCacheFile:(id)collection {
if ([collection isKindOfClass:[NSDictionary class]]) {
NSDictionary *dict = (NSDictionary *)collection;
return [dict writeToFile:writeFilePath atomically:YES];
}else if( [collection isKindOfClass:[NSArray class]]){
NSArray *array = (NSArray *)collection;
return [array writeToFile:writeFilePath atomically:YES];
}
else if ([collection isKindOfClass:[NSString class]]) {
NSString *string = (NSString *)collection;
return [string writeToFile:writeFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
return NO;
}
+ (NSArray *)readArrayFromCacheFile {
NSArray *fetchArray = [NSArray arrayWithContentsOfFile:writeFilePath];
return fetchArray;
}
@end

人已赞赏
iOS文章

iOS数组排序sortedArrayUsingComparator

2020-3-4 10:08:40

iOS文章

iOS开发Expected selector for Objective-C and Expected method body

2020-3-4 11:35:40

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索