#import <Foundation/Foundation.h>
#import "Movie.h"
#import <UIKit/UIAlertView.h>
@interface SaveTool : NSObject
+(NSString *)documentModel:(id)model; //找路径
+(void)saveMovie:(id)model
delegate:(id<UIAlertViewDelegate>) delegate; // 保存
+(void)removeMovie:(id)model; // 删除
+(BOOL)haveModel:(id)model; // 是否在档
@end<pre name="code" class="objc">#import "SaveTool.h"
@interface SaveTool()
@end
@implementation SaveTool
+(NSString *)documentModel:(id)model{
NSString *className = [NSString stringWithFormat:@"%@", [model class]];
NSString *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// 拼接文件名
NSString *filePath = [document stringByAppendingPathComponent:className];
if (![NSKeyedUnarchiver unarchiveObjectWithFile:filePath]) {
NSFileManager *manger = [NSFileManager defaultManager];
[manger createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *doucumetPath = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", className]];
NSLog(@"%@", doucumetPath);
return doucumetPath;
}
+(void)saveMovie:(id)model delegate:(id<UIAlertViewDelegate>)delegate{
NSString *doucumetPath = [self documentModel:model];
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:doucumetPath];
UIAlertView *sucess = [[UIAlertView alloc] initWithTitle:@"成功" message:@"已经成了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
sucess.delegate = delegate;
UIAlertView *warning = [[UIAlertView alloc] initWithTitle:@"失败" message:@"已收藏" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"移除", nil];
warning.tag = 999;
warning.delegate = delegate;
if (!arr) {
arr = @[model];
[NSKeyedArchiver archiveRootObject:arr toFile:doucumetPath];
[sucess show];
}else {
NSMutableArray *tempArr = [NSMutableArray arrayWithArray:arr];
if ([self haveModel:model]) {
[warning show];
} else {
[tempArr addObject:model];
[NSKeyedArchiver archiveRootObject:tempArr toFile:doucumetPath];
[sucess show];
}
}
}
#pragma mark 便利数组找到model在否,model的位置
typedef void(^ModelBlock)(id temp);
+(void)myblockArr:(NSArray *)arr
model:(id)model
blcok:(ModelBlock)tempBlock{
ModelBlock block = Block_copy(tempBlock);
for (id temp in arr) {
NSString *tempId = [temp Id];
NSString *modelId = [model Id];
// NSString *modelId = [model performSelector:NSSelectorFromString(@"Id")];
if ([tempId isEqualToString:modelId]) {
block(temp);
Block_release(block);
return;
}
}
Block_release(block);
}
+(void)removeMovie:(id)model{
NSString *doucumetPath = [self documentModel:model];
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:doucumetPath];
[self myblockArr:arr model:model blcok:^(id temp) {
NSMutableArray *movieArr = [NSMutableArray arrayWithArray:arr];
[movieArr removeObject:temp];
[NSKeyedArchiver archiveRootObject:movieArr toFile:doucumetPath];
}];
}
+(BOOL)haveModel:(id)model{
NSString *doucumetPath = [self documentModel:model];
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:doucumetPath];
__block BOOL haved = NO;
[self myblockArr:arr model:model blcok:^(id temp) {
haved = YES;
}];
return haved;
}
@end
自己建工具类
最新推荐文章于 2023-05-04 16:42:29 发布