gpt4 book ai didi

iphone - NSMutableArray 问题 - iPhone

转载 作者:行者123 更新时间:2023-12-03 17:44:55 25 4
gpt4 key购买 nike

我正在尝试获取 UITableView 从文件中读取数据。我已经尝试过这样的操作:

NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fullFileName = [NSString stringWithFormat:@"%@/entries.plist", documentsDirectory];

self.dataForTable = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];

这编译得很好,但是当将某些内容保存到以下代码片段中的文件时,该文件不会保存,也不会写入任何内容到数组中:

NSMutableDictionary*userDictionary;
userDictionary = [[NSMutableDictionary alloc] init];
[userDictionary setObject:name.text forKey:@"name"];
[userDictionary setObject:email.text forKey:@"email"];
[userDictionary setObject:serial.text forKey:@"serial"];
[userDictionary setObject:notes.text forKey:@"notes"];
[userDictionary setObject:[NSNumber numberWithInt:[licenseType selectedRowInComponent:0]] forKey:@"license_type"];
[userDictionary setObject:[date date] forKey:@"date"];
[userDictionary setObject:[NSNumber numberWithBool:[paymentSwitch isOn]] forKey:@"payment"];

NSString*dirToSaveTo = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

NSString*fileName = [NSString stringWithFormat:@"%@.plist",name.text];

NSString*saveName = [dirToSaveTo stringByAppendingPathComponent:fileName];

[userDictionary writeToFile:saveName atomically:NO];

NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fullFileName = [NSString stringWithFormat:@"%@/entries.plist", documentsDirectory];

[self.dataForTable addObject:name.text];

NSLog(@"%@",self.dataForTable);

[self.dataForTable writeToFile:fullFileName atomically:YES];

NSLog 仅返回 (null)。 *plist 文件永远不会被写入。我做错了什么?

最佳答案

几乎可以肯定这一行失败了,并且您没有检查它是否返回 nil:

self.dataForTable = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];

一旦它返回 nil,对它的所有其他调用都不会执行任何操作。最可能的问题是您构建的文件路径不正确并且没有指向该文件,或者该文件不是正确的 plist。

关于iphone - NSMutableArray 问题 - iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2898614/

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