用单例来临时保存缓存数据

2015-12-08 0 646
用单例来临时保存缓存数据
//进行缓存的步骤:

//1.创建单例(返回自己或者id)
static DYPDataCache *cache = nil;

+(id)sharedInstance{
    //保证线程安全
    @synchronized(self){
    if (cache == nil) {
        //cache = [[DYPDataCache alloc]init];
        //通过[self class]方法可以获取到当前类的类名
        cache = [[[self class]alloc]init];
        
        //初始化有效时间
        cache.validTime = 20*60;
    }
    }
    return cache;
}
+(id)allocWithZone:(struct _NSZone *)zone{

    if (cache == nil) {
        cache = [super allocWithZone:zone];
    }
    return cache;
}

遇见资源网 objective-c 用单例来临时保存缓存数据 http://www.ox520.com/18159.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务