我不知道如何使用NSCashe
,但我在阅读了这个主题How to use NSCache后尝试了它,但它没有用。
我遇到此问题原因:' - [NSCache setObject:forKey:cost:]:尝试插入nil值(key:indexPath)'
我做错了吗?
.h
@interface ViewController : UIViewController
@property (nonatomic, strong) NSMutableArray *jsonArray;
@property (nonatomic, strong) NSMutableArray *jsonArray1;
@property (nonatomic, retain) NSCache *Cache;
.m
- (void)viewDidLoad
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://kalkatawi.com/jsonTest.php"]];
NSError *parseError = nil;
jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];
jsonArray1 = [[NSMutableArray alloc] init];
Cache = [[NSCache alloc]init];
for(int i=0;i<[jsonArray count];i++)
{
NSString * city = [[jsonArray objectAtIndex:i] objectForKey:@"city"];
[jsonArray1 addObject:city];
}
dispatch_sync(dispatch_get_main_queue(), ^{
self.title = @"İller";
[self.mytable1 reloadData];
jsonArray1 = [Cache objectForKey:@"indexPath"];
[Cache setObject:jsonArray1 forKey:@"indexPath"];
});
});
}
答案 0 :(得分:1)
您正在初始化jsonArray1,就在设置之前。
jsonArray1 = [Cache objectForKey:@"indexPath"]; // here jsonArray1 = nil; hence remove this line and run your code again. Yo
[Cache setObject:jsonArray1 forKey:@"indexPath"]; // hence you are setting nil value to this key "indexpath"
在那个教程中,他们说的是
[Cache setObject:jsonArray1 forKey:@“indexPath”];
jsonArray1 = [Cache objectForKey:@“indexPath”];
在您的情况下,只需发表评论
jsonArray1 = [Cache objectForKey:@“indexPath”];
排队并再次运行。它不会给出错误。