iOS 收藏功能

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;
@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"];
        self.nextURL = [aDecoderdecodeObjectForKey:@"_nextURL"];  
    }
    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;
@end
///
                 LJCollectTool.m
///
#import"LJCollectTool.h"
#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 ;
}
//从文件中读取了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)];
}
@end
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值