NSMutableArray 总是返回 null

发布于 2024-12-23 12:56:50 字数 875 浏览 1 评论 0原文

我的问题是,当我在 NSMutableArray 中读取 plist 文件的内容时,

NSString *resourceDocPath = [[NSString alloc] initWithString:[[NSBundle mainBundle]bundlePath]] ;

// Create the new dictionary that will be inserted into the plist.

NSMutableDictionary *nameDictionary = [NSMutableDictionary dictionary];
[nameDictionary setValue:@"walid" forKey:@"id"];
[nameDictionary setValue:@"555 W 1st St" forKey:@"lien"];
NSString *r = [NSString stringWithFormat:@"%@/download.plist", resourceDocPath];
NSLog(@"%@",r);
// Open the plist from the filesystem.
NSMutableArray *plist = [NSMutableArray  arrayWithContentsOfFile:r]; 
 NSLog(@"%@",plist);
if (plist == NULL) 
{
    plist = [NSMutableArray array];
}
[plist addObject:nameDictionary];
 NSLog(@"%@",plist);
[plist writeToFile:r atomically:YES];

当我查看 plist 文件时总是返回 null,我发现我只插入了一个数据,

您能帮我吗?

My problem is when I read content of plist file in an NSMutableArray always return null

NSString *resourceDocPath = [[NSString alloc] initWithString:[[NSBundle mainBundle]bundlePath]] ;

// Create the new dictionary that will be inserted into the plist.

NSMutableDictionary *nameDictionary = [NSMutableDictionary dictionary];
[nameDictionary setValue:@"walid" forKey:@"id"];
[nameDictionary setValue:@"555 W 1st St" forKey:@"lien"];
NSString *r = [NSString stringWithFormat:@"%@/download.plist", resourceDocPath];
NSLog(@"%@",r);
// Open the plist from the filesystem.
NSMutableArray *plist = [NSMutableArray  arrayWithContentsOfFile:r]; 
 NSLog(@"%@",plist);
if (plist == NULL) 
{
    plist = [NSMutableArray array];
}
[plist addObject:nameDictionary];
 NSLog(@"%@",plist);
[plist writeToFile:r atomically:YES];

when I look in the plist file I found the data that I insert only one

can you help me please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小帐篷 2024-12-30 12:56:50

您正在尝试访问应用程序包而不是文档目录,可以通过 NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 访问该目录。
NSString *sourcePath = [[[NSBundle mainBundle] resourcesPath] stringByAppendingPathComponent:@"Populator"];。捆绑包无法修改,因此创建的数组永远不会保存,因此永远不会加载它。

You're trying to access the application bundle rather than the documents directory, which can be accessed via NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Populator"];
. The bundle cannot be modified, so the created array is never saved, hence why it is never loaded.

独留℉清风醉 2024-12-30 12:56:50

首先,您不应该检查 plist == null ,而是检查 plist == nil

第二次搜索下载文件应更改为以下内容:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"download" withExtension:@"plist"];
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfURL:url];

第三:
我认为扩展名为 plist 的文件不会返回数组。
它可能代表一本字典。尝试创建 NSMutableDictionary 而不是数组。

First you should not check for plist == null but check for plist == nil

Second searching for the download file should be changed into the following:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"download" withExtension:@"plist"];
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfURL:url];

Third:
I do not think a file with the extension of plist will return an Array.
It will probably represent an dictionary. Try creating an NSMutableDictionary instead of an array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文