2009-07-16 50 views
0

文件中沒有任何內容,但是我立即將row1添加到我的數組中。 NSLog告訴我該數組是空的。爲什麼不將row1添加到數組中?據我所知,我所有的其他代碼都很好。當我將硬值添加到數組中時,我的應用程序工作。現在我從文件加載,它不起作用。將一個對象添加到從文件中讀取的數組中

- (void)viewDidLoad { 
//array value 

//NSMutableArray *array; 

NSString *filePath = [self dataFilePath]; 
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
{ 
NSMutableArray *array/*array*/ = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; 

NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Study", @"Task", @"2 hours", @"Length", @"4", @"Hours", @"0", @"Minutes", @"15", @"Tiredness", nil]; 
[array addObject: row1]; 
self.tasks = array; 

NSLog(@"The contents of the array (file exists) is %@", array); 

[array release]; 
[myTableView reloadData]; 
} 

UIApplication *app = [UIApplication sharedApplication]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(applicationWillTerminate:) 
              name:UIApplicationWillTerminateNotification 
              object:app]; 
[super viewDidLoad]; 
} 

請幫忙!

由於提前,

馬特

+0

不要忘了`release`數組和字典,因爲你每個人的使用`alloc`和創建`init`方法。請參閱Cocoa的內存管理編程指南:http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/ – 2009-07-16 08:44:15

回答

0

確保實現代碼如下委託和數據源是否正確設置

1

兩種可能性我看:

  1. 包含此代碼的對象不正確設置爲表格的數據源。

  2. 文件存在於您正在查看的路徑中,但其內容不能被解析爲數組。在這種情況下,你會擊中if子句的第一個分支,但initWithContentsOfFile:將返回nil。調用該方法後,您可以通過檢查nil來輕鬆診斷此問題。

0

很難回答你的問題。這裏有兩個建議:

當你在這裏發佈代碼,清理它。刪除評論並很好地格式化。

顯示您的所有代碼。在你的代碼中可能有很多錯誤。也許self.tasks屬性不正確。也許你沒有正確設置數據視圖。也許你沒有實現正確的表視圖委託方法。很難說。

此外,嘗試排除最基本的東西。例如,如果你是在懷疑陣列是否設置正確,然後只需打印:

NSLog(@"The contents of the array is %@", array); 
相關問題