Modifying the Cache Name

- name

Returns the name of the cache.

Example

NSCache *cache = [[NSCache alloc]init];
[cache setName:@"eezyCache"];
NSLog(@"%@",[cache name]);		

Output

2014-04-04 08:49:44.697 iOS-Tutorial[622:a0b] eezyCache
		

- setName:

Sets the cache’s name attribute to a specific string.

Example

NSCache *cache = [[NSCache alloc]init];
[cache setName:@"eezyCache"];
NSLog(@"%@",[cache name]);		
		

Output

2014-04-04 08:49:44.697 iOS-Tutorial[622:a0b] eezyCache
		

Getting a Cached Value

- objectForKey:

Returns the value associated with a given key.

Example

NSURL *url = [NSURL URLWithString:@"http://ios.eezytutorials.com/sample-files/sample-dictionary-plist.plist"];
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfURL:url];
NSCache *cache = [[NSCache alloc]init];
[cache setObject:dict forKey:url];
NSLog(@"%@",[cache objectForKey:url]);
	

Output

2014-04-04 09:00:14.695 iOS-Tutorial[649:a0b] {
    key1 = Eezy;
    key2 = Tutorials;
}
		

Adding and Removing Cached Values

- setObject:forKey:

Sets the value of the specified key in the cache.

Example

NSURL *url = [NSURL URLWithString:@"http://ios.eezytutorials.com/sample-files/sample-dictionary-plist.plist"];
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfURL:url];
NSCache *cache = [[NSCache alloc]init];
[cache setObject:dict forKey:url];
NSLog(@"%@",[cache objectForKey:url]);
		

Output

2014-04-04 09:00:14.695 iOS-Tutorial[649:a0b] {
    key1 = Eezy;
    key2 = Tutorials;
}		

- setObject:forKey:cost:

Sets the value of the specified key in the cache, and associates the key-value pair with the specified cost.

Example

NSURL *url = [NSURL URLWithString:@"http://ios.eezytutorials.com/sample-files/sample-dictionary-plist.plist"];
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfURL:url];
NSCache *cache = [[NSCache alloc]init];
[cache setObject:dict forKey:url cost:10];
NSLog(@"%@",[cache objectForKey:url]);
		

Output

2014-04-04 09:02:53.876 iOS-Tutorial[667:a0b] {
    key1 = Eezy;
    key2 = Tutorials;
}
		

- removeObjectForKey:

Removes the value of the specified key in the cache.

Example

NSURL *url = [NSURL URLWithString:@"http://ios.eezytutorials.com/sample-files/sample-dictionary-plist.plist"];
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfURL:url];
NSCache *cache = [[NSCache alloc]init];
[cache setObject:dict forKey:url cost:10];
[cache removeObjectForKey:url];
NSLog(@"%@",[cache objectForKey:url]);
		

Output

2014-04-04 09:04:48.587 iOS-Tutorial[681:a0b] (null)
		

- removeAllObjects

Empties the cache.

Example

NSURL *url = [NSURL URLWithString:@"http://ios.eezytutorials.com/sample-files/sample-dictionary-plist.plist"];
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfURL:url];
NSCache *cache = [[NSCache alloc]init];
[cache setObject:dict forKey:url cost:10];
[cache removeAllObjects];
NSLog(@"%@",[cache objectForKey:url]);

		

Output

2014-04-04 09:04:48.587 iOS-Tutorial[681:a0b] (null)
		

Managing Cache Size

- countLimit

Returns the maximum number of objects that the cache can currently hold.

Example

NSCache *cache = [[NSCache alloc]init];
[cache setCountLimit:10];
NSLog(@"%d",[cache countLimit]);
		

Output

2014-04-04 09:06:33.960 iOS-Tutorial[694:a0b] 10
		

- setCountLimit:

Sets the maximum number of objects that the cache can hold.

Example

NSCache *cache = [[NSCache alloc]init];
[cache setCountLimit:10];
NSLog(@"%d",[cache countLimit]);
		

Output

2014-04-04 09:06:33.960 iOS-Tutorial[694:a0b] 10
		

- totalCostLimit

Returns the maximum total cost that the cache can have before it starts evicting objects.

Example

NSCache *cache = [[NSCache alloc]init];
[cache setTotalCostLimit:10];
NSLog(@"%d",[cache totalCostLimit]);
		

Output

2014-04-04 09:08:11.813 iOS-Tutorial[712:a0b] 10
		

- setTotalCostLimit:

Sets the maximum total cost that the cache can have before it starts evicting objects.

Example

NSCache *cache = [[NSCache alloc]init];
[cache setTotalCostLimit:10];
NSLog(@"%d",[cache totalCostLimit]);
		

Output

2014-04-04 09:08:11.813 iOS-Tutorial[712:a0b] 10
		

Managing Discardable Content

- evictsObjectsWithDiscardedContent

Returns whether or not the cache will automatically evict discardable-content objects whose content has been discarded.

Example

NSCache *cache = [[NSCache alloc]init];
[cache setEvictsObjectsWithDiscardedContent:YES];
NSLog(@"%d",[cache evictsObjectsWithDiscardedContent]);
		

Output

2014-04-04 09:08:11.813 iOS-Tutorial[712:a0b] 10
		

- setEvictsObjectsWithDiscardedContent:

Sets whether the cache will automatically evict NSDiscardableContent objects after the object’s content has been discarded.

Example

NSCache *cache = [[NSCache alloc]init];
[cache setEvictsObjectsWithDiscardedContent:YES];
NSLog(@"%d",[cache evictsObjectsWithDiscardedContent]);
		

Output

2014-04-04 09:08:11.813 iOS-Tutorial[712:a0b] 10
		

Managing the Delegate

- delegate

Returns the cache’s delegate.

Example

NSCache *cache = [[NSCache alloc]init];
[cache setDelegate:self];
NSLog(@"%@",[cache delegate]);
		

Output

2014-04-04 09:10:50.718 iOS-Tutorial[728:a0b] 		

- setDelegate:

Makes the given object the cache’s delegate.

Example

NSCache *cache = [[NSCache alloc]init]; [cache setDelegate:self]; NSLog(@"%@",[cache delegate]);

Output

2014-04-04 09:10:50.718 iOS-Tutorial[728:a0b] 		

Advertisements