1

我正在嘗試使用tableview /數組組合來從文檔目錄中刪除文件。 出於某種原因,指向Documents目錄路徑的我的NSString正在轉換爲NSCFType(經過一些調查,我明白因爲變量未被釋放而發生)。正因爲如此,該應用程序崩潰在行NSCFType一直在發生,有些東西沒有被釋放?

NSString *lastPath = [documentsDirectory stringByAppendingPathComponent:temp]; 

聲稱NSCFType無法識別方法stringByAppendingPathComponent

如果有人能幫助我,我將不勝感激(希望我已經足夠清楚地解釋了這一點)。

- (void) tableView: (UITableView *) tableView 
commitEditingStyle: (UITableViewCellEditingStyle) editingStyle 
forRowAtIndexPath: (NSIndexPath *) indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     NSString *temp = [directoryContent objectAtIndex:indexPath.row]; 

     NSLog(temp); 

     NSString *lastPath = [documentsDirectory stringByAppendingPathComponent:temp]; 


     [[NSFileManager defaultManager] removeItemAtPath:lastPath error:nil]; 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

     documentsDirectory = [paths objectAtIndex:0]; 
     directoryContent = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil] retain]; 

//tableview handling below 


} 
+0

後,您可以設置documentsDirectory –

+0

比較遺憾的是代碼,編輯。 – user1493543

+0

Documentsdirectory肯定是在某處發佈的。它是強大的還是保留的(ARC或MRC)。 – CodaFi

回答

2

似乎documentsDirectory被設置爲自動釋放的對象,以解決這個問題,

你要麼保留它

documentsDirectory = [[paths objectAtIndex:0] retain]; 

,或者你只是使用前初始化它每次都是這樣的

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
documentsDirectory = [paths objectAtIndex:0]; 
NSString *lastPath = [documentsDirectory stringByAppendingPathComponent:temp]; 
[[NSFileManager defaultManager] removeItemAtPath:lastPath error:nil]; 

我的建議是,不要添加所有這些變量,實例變量,你不需要,它更好地創建他們時,你需要他們

+0

這不是初始化,這與調用retain相同。 – CodaFi

+0

@CodaFi我的意思是不使用它們作爲實例變量,並且將它們的作用域降低到函數變量 –

+0

如果這個iVar減小了範圍,他就失去了跟蹤它的可能性,並且在以後引用它,這就失去了保留的目的。 – CodaFi

相關問題