2009-07-16 29 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); 
相关问题