我不知道如何使用NSCashe
,但我在阅读本主题如何使用 NSCache后尝试过,但它没有用。
我得到这个问题的原因:'-[NSCache setObject:forKey:cost:]:尝试插入 nil 值(键: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"];
});
});
}