基于jsonkit,缓存(缓存对象 是服务器返回的整个json字符串)工具类,

本文介绍了一个用于iOS应用的缓存管理类CacheUtil,该类提供了获取文档目录、写入缓存、读取缓存和清除缓存的方法。通过使用JSONKit进行数据序列化和反序列化,实现了对不同类型数据的有效存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "CacheUtil.h"

#import "JSONKit.h"

 

@implementation CacheUtil

 

//获得访问目录

+ (NSString*)documentPath

{

    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,    NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    return documentsDirectory;

}

 

//写缓存

+ (void)writeCache:(id)json ofType:(NSString *)type

{

    NSString* content = [json JSONString];

    [content writeToFile:[[CacheUtildocumentPath] stringByAppendingPathComponent:type] atomically:YESencoding:NSUTF8StringEncodingerror:nil];

}

 

//读缓存 

+ (id)readCacheOfType:(NSString *)type

{

    NSString* path = [[CacheUtildocumentPath] stringByAppendingPathComponent:type];

    if (![[NSFileManagerdefaultManager] fileExistsAtPath:path]) {

        return nil;

    }

    NSError* error;

    NSData* content = [[NSData alloc] initWithContentsOfFile:path];

    if (error) {

        return nil;

    }

    id json = [content objectFromJSONData];

    return json;

}

 

//清除缓存

+ (void)clearCacheOfType:(NSString*)type

{

    NSString* path = [[CacheUtildocumentPath] stringByAppendingPathComponent:type];

    if ([[NSFileManagerdefaultManager] fileExistsAtPath:path]) {

        [[NSFileManagerdefaultManager] removeItemAtPath:path error:nil];

    }

}

 

@end

转载于:https://www.cnblogs.com/guligei/archive/2013/03/14/2958869.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值