I am working with NSCache in iOS i have following code in .h file for cache:
NSCache *_cache;
I am adding the image which are downloading from the url to the cache in .m:
-(void)cacheFromURL:(NSURL*)url
{
[_cache setCountLimit:50];
UIImage* newImage = [_cache objectForKey:url.description];
if( !newImage )
{
NSError *err = nil;
if(url!=Nil)
{
newImage=[UIImage imageWithData:[NSData dataWithContentsOfURL:url options:0 error:&err]];
}
if( newImage )
{
[_cache setValue:newImage forKey:url.description];
}
else
{
NSLog( @"UIImageView:LoadImage Failed: %@", err );
}
if(_cache==Nil)
{
NSLog(@"nil");
}
else
{
NSLog(@"cache is not nil");
}
}
}
But i am getting the nil cache every time . where i can see the downloading process in log .
why am i getting null cache?
You need to create/initialize the
NSCacheobject before you can add and object to it. Also you need to add object with thesetObject:forKey: