2012-09-25 28 views
1

我將NSString保存爲NSCache,但在NSLog中給我(null)。NSCache一直給我(null)

我有這樣的代碼保存在NSCache後,我在viewDidLoad中初始化它

cache=[[NSCache alloc]init]; 

然後,我有一個名叫並保存函數對象

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1]; 
NSString *res = [cache objectForKey:indexPath]; 
if (!res) {      
    [email protected]"1"; 
    [cache setObject:res forKey:indexPath]; 
} 

然後我檢查是這樣的:

NSString *res = [cache objectForKey:indexPath]; 
if (!res) {  
    [email protected]"1"; 
    [cache setObject:res forKey:indexPath]; 
} 

可惜...

NSLog("%@",res); //always it gives me (null) 

任何人有過這個問題嗎?任何幫助讚賞。

+0

你在哪裏做記錄?你是否檢查過初始值不是NSNull?複製密鑰是否有所作爲(NSCache默認情況下不復制密鑰)? –

回答

0

保存對象後檢查是這樣的:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1]; 
NSLog(@"%@",[cache objectForKey:indexPath]); 
+0

感謝您的幫助,問題是indexPath不是正確的!無論如何,我的錯誤感謝傢伙! :) – stefanosn