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


    #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

  • 相关阅读:
    工程师的十层楼,上
    工程师的十层楼 (下)
    2011CCTV中国经济年度人物评选结果揭晓
    IT行业程序员薪水差距之大的原因是什么
    单片机C应用开发班
    【分享】对输入子系统分析总结
    P6156 简单题 题解
    P3911 最小公倍数之和 题解
    dp 做题记录
    UVA12298 Super Poker II 题解
  • 原文地址:https://www.cnblogs.com/guligei/p/2958869.html
Copyright © 2020-2023  润新知