iOS使用归档和反归档收藏对象
///
LJCollectModel.h
///
#import
<Foundation/Foundation.h>
@interface
LJCollectModel :
NSObject
<
NSCoding
>
@property
(
copy
,
nonatomic
)
NSString
*placeName;
@property ( copy , nonatomic ) NSString *imgPathMobile;
@property ( copy , nonatomic ) NSString *placeAddress;
@property ( copy , nonatomic ) NSString *nextPage; // 下一个界面
@property ( copy , nonatomic ) NSString *nextURL; // 下一个界面的 URL
// 额外的属性
@property ( nonatomic , assign , getter =isCollected) BOOL collected; // 是否被收藏
// 转化为 model
- ( instancetype )initWithCollectModelPlaceName:( NSString *)placeName imgPathMobile:( NSString *)imgPathMobile placeAddress:( NSString *)placeAddress nextPage:( NSString *)nextPage nextURL:( NSString *)nextURL;
@property ( copy , nonatomic ) NSString *imgPathMobile;
@property ( copy , nonatomic ) NSString *placeAddress;
@property ( copy , nonatomic ) NSString *nextPage; // 下一个界面
@property ( copy , nonatomic ) NSString *nextURL; // 下一个界面的 URL
// 额外的属性
@property ( nonatomic , assign , getter =isCollected) BOOL collected; // 是否被收藏
// 转化为 model
- ( instancetype )initWithCollectModelPlaceName:( NSString *)placeName imgPathMobile:( NSString *)imgPathMobile placeAddress:( NSString *)placeAddress nextPage:( NSString *)nextPage nextURL:( NSString *)nextURL;
@end
///
LJCollectModel.m
///
#import"LJCollectModel.h"
@implementation LJCollectModel
- (instancetype)initWithCollectModelPlaceName:(NSString *)placeName imgPathMobile:(NSString *)imgPathMobile placeAddress:(NSString *)placeAddress nextPage:(NSString *)nextPage nextURL:(NSString *)nextURL
{
if (self = [super init]) {
self.placeName = placeName;
self.imgPathMobile = imgPathMobile;
self.placeAddress = placeAddress;
self.nextPage = nextPage;
self.nextURL = nextURL;
}
return self;
}
//当判断2个对象是否相等的内部机制是调用isEqual方法,默认是判断2个对象的内存地址是否相等
- (BOOL)isEqual:(LJCollectModel *)other
{
return [other.nextURL isEqualToString:_nextURL];
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_placeNameforKey:@"_placeName"];
[aCoder encodeObject:_imgPathMobileforKey:@"_imgPathMobile"];
[aCoder encodeObject:_placeAddressforKey:@"_placeAddress"];
[aCoder encodeObject:_nextPageforKey:@"_nextPage"];
[aCoder encodeObject:_nextURLforKey:@"_nextURL"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
self.placeName = [aDecoder decodeObjectForKey:@"_placeName"];
self.imgPathMobile = [aDecoderdecodeObjectForKey:@"_imgPathMobile"];
self.placeAddress = [aDecoder decodeObjectForKey:@"_placeAddress"];
self.nextPage = [aDecoder decodeObjectForKey:@"_nextPage"];
{
if (self = [super init]) {
self.placeName = placeName;
self.imgPathMobile = imgPathMobile;
self.placeAddress = placeAddress;
self.nextPage = nextPage;
self.nextURL = nextURL;
}
return self;
}
//当判断2个对象是否相等的内部机制是调用isEqual方法,默认是判断2个对象的内存地址是否相等
- (BOOL)isEqual:(LJCollectModel *)other
{
return [other.nextURL isEqualToString:_nextURL];
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_placeNameforKey:@"_placeName"];
[aCoder encodeObject:_imgPathMobileforKey:@"_imgPathMobile"];
[aCoder encodeObject:_placeAddressforKey:@"_placeAddress"];
[aCoder encodeObject:_nextPageforKey:@"_nextPage"];
[aCoder encodeObject:_nextURLforKey:@"_nextURL"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
self.placeName = [aDecoder decodeObjectForKey:@"_placeName"];
self.imgPathMobile = [aDecoderdecodeObjectForKey:@"_imgPathMobile"];
self.placeAddress = [aDecoder decodeObjectForKey:@"_placeAddress"];
self.nextPage = [aDecoder decodeObjectForKey:@"_nextPage"];
self.nextURL = [aDecoderdecodeObjectForKey:@"_nextURL"];
}
return self;
}
return self;
}
@end
///
LJCollectTool.h
///
#import <Foundation/Foundation.h>
#import "Singleton.h"
@class LJCollectModel;
@interface LJCollectTool : NSObject
+ (
instancetype
)sharedCollectTool;
//获得所有的收藏信息
@property (nonatomic, strong, readonly) NSArray *collectedArrays;
/**
* 处理是否收藏
*
* @param model收藏模型
*/
- (void)handleCollect:(LJCollectModel *)model;
/**
* 添加收藏
*
* @param model收藏模型
*/
- (void)collectCollect:(LJCollectModel *)model;
/**
* 取消收藏
*
* @param model收藏模型
*/
- (void)uncollectCollect:(LJCollectModel *)model;
@property (nonatomic, strong, readonly) NSArray *collectedArrays;
/**
* 处理是否收藏
*
* @param model收藏模型
*/
- (void)handleCollect:(LJCollectModel *)model;
/**
* 添加收藏
*
* @param model收藏模型
*/
- (void)collectCollect:(LJCollectModel *)model;
/**
* 取消收藏
*
* @param model收藏模型
*/
- (void)uncollectCollect:(LJCollectModel *)model;
@end
///
LJCollectTool.m
///
#import"LJCollectTool.h"
#import "LJCollectModel.h"
#define kCollectFileName @"collects.data"
@interface LJCollectTool()
{
NSMutableArray *_collectedArrays;
}
#import "LJCollectModel.h"
#define kCollectFileName @"collects.data"
@interface LJCollectTool()
{
NSMutableArray *_collectedArrays;
}
@end
staticLJCollectTool *_instance;
@implementation LJCollectTool
+ (
instancetype
)sharedCollectTool
{
static dispatch_once_t onceToken;
dispatch_once (&onceToken, ^{
_instance = [[ self alloc ] init ];
});
return _instance ;
{
static dispatch_once_t onceToken;
dispatch_once (&onceToken, ^{
_instance = [[ self alloc ] init ];
});
return _instance ;
}
//从文件中读取了2个对象
//
- (id)init
{
if (self = [super init]) {
// 1.加载沙盒中的收藏数据
_collectedArrays = [NSKeyedUnarchiverunarchiveObjectWithFile:kDocumentToPlistName(kCollectFileName)];
// 2.第一次没有收藏数据
if (_collectedArrays == nil) {
_collectedArrays = [NSMutableArrayarray];
}
}
return self;
}
- (void)handleCollect:(LJCollectModel *)model
{
model.collected = [_collectedArrayscontainsObject:model];
}
- (void)collectCollect:(LJCollectModel *)model
{
model.collected = YES;
[_collectedArrays insertObject:model atIndex:0];
[NSKeyedArchiver archiveRootObject:_collectedArraystoFile:kDocumentToPlistName(kCollectFileName)];
}
- (void)uncollectCollect:(LJCollectModel *)model
{
model.collected = NO;
[_collectedArrays removeObject:model];
[NSKeyedArchiver archiveRootObject:_collectedArraystoFile:kDocumentToPlistName(kCollectFileName)];
}
{
if (self = [super init]) {
// 1.加载沙盒中的收藏数据
_collectedArrays = [NSKeyedUnarchiverunarchiveObjectWithFile:kDocumentToPlistName(kCollectFileName)];
// 2.第一次没有收藏数据
if (_collectedArrays == nil) {
_collectedArrays = [NSMutableArrayarray];
}
}
return self;
}
- (void)handleCollect:(LJCollectModel *)model
{
model.collected = [_collectedArrayscontainsObject:model];
}
- (void)collectCollect:(LJCollectModel *)model
{
model.collected = YES;
[_collectedArrays insertObject:model atIndex:0];
[NSKeyedArchiver archiveRootObject:_collectedArraystoFile:kDocumentToPlistName(kCollectFileName)];
}
- (void)uncollectCollect:(LJCollectModel *)model
{
model.collected = NO;
[_collectedArrays removeObject:model];
[NSKeyedArchiver archiveRootObject:_collectedArraystoFile:kDocumentToPlistName(kCollectFileName)];
}
@end