Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 重新启动项目后,NSCache返回null_Ios_Objective C_Nscache - Fatal编程技术网

Ios 重新启动项目后,NSCache返回null

Ios 重新启动项目后,NSCache返回null,ios,objective-c,nscache,Ios,Objective C,Nscache,我在Objective-C和iOS的Cocoa中与NSCache合作。每次我重新启动项目时,getCacheRecommend调用都返回null,我希望它返回一个值 #import <Foundation/Foundation.h> @class ASJsonDiscoverModel; @interface ASUserCache : NSObject + (ASUserCache *)sharedInstance; - (void)clear; - (void)setC

我在Objective-C和iOS的Cocoa中与NSCache合作。每次我重新启动项目时,getCacheRecommend调用都返回null,我希望它返回一个值

#import <Foundation/Foundation.h>
@class ASJsonDiscoverModel;
@interface ASUserCache : NSObject

+ (ASUserCache *)sharedInstance;    
- (void)clear;
- (void)setCacheRecommend:(ASJsonDiscoverModel *)discover;
- (ASJsonDiscoverModel *)getCacheRecommend;

代码在我看来没问题。请显示更多代码,演示问题。NSCache不会将内容持久化到磁盘-您希望它持久化吗?当项目运行时,[[ASUserCache sharedInstance]getCacheRecommend]返回一个结果。但每次我重新启动项目时,它都返回零。这是内存中的缓存。它还将在高内存消耗期间弹出对象。但不是在高内存中。每次重新启动时,它都返回nil。
#import "ASUserCache.h"
@interface ASUserCache ()
@property (nonatomic,strong) NSCache *cache;
@end

@implementation ASUserCache

+ (ASUserCache *)sharedInstance
{
    __strong static ASUserCache *cache = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cache = [[ASUserCache alloc] init];
    });
    return cache;
}

- (instancetype)init
{
    if (self = [super init]) {
         _cache = [[NSCache alloc] init];
    }
    return self;
}

- (void)setCacheRecommend:(ASJsonDiscoverModel *)discover
{
    NSString *key = @"channelRecommend";
    [_cache removeObjectForKey:key];
    [_cache setObject:discover forKey:key];
}

- (ASJsonDiscoverModel *)getCacheRecommend
{
    NSString *key = @"channelRecommend";
    return [_cache objectForKey:key];
}

- (void)clear
{
    if (_cache) {
        [_cache removeAllObjects];
    }
}

- (NSString *)keyforUserID:(NSString *)userID
{
    return [NSString stringWithFormat:@"**%@",userID];
}