文件中沒有任何內容,但是我立即將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];
}
請幫忙!
由於提前,
馬特
不要忘了`release`數組和字典,因爲你每個人的使用`alloc`和創建`init`方法。請參閱Cocoa的內存管理編程指南:http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/ – 2009-07-16 08:44:15