gpt4 book ai didi

objective-c - 将对象添加到从文件读取的数组中

转载 作者:行者123 更新时间:2023-12-03 18:06:52 25 4
gpt4 key购买 nike

文件中没有任何内容,但我随后将 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];
}

请帮忙!

提前致谢,

马特

最佳答案

我看到两种可能性:

  1. 包含此代码的对象未正确设置为表的数据源。

  2. 您正在查看的路径中存在一个文件,但其内容无法解析为数组。在这种情况下,您将命中 if 子句的第一个分支,但 initWithContentsOfFile: 将返回 nil。您可以通过在调用该方法后检查 nil 来轻松诊断此问题。

关于objective-c - 将对象添加到从文件读取的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1135113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com