NSMutableArray 总是返回 null
我的问题是,当我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试访问应用程序包而不是文档目录,可以通过 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];
. The bundle cannot be modified, so the created array is never saved, hence why it is never loaded.NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Populator"];
首先,您不应该检查
plist == null
,而是检查plist == nil
第二次搜索下载文件应更改为以下内容:
第三:
我认为扩展名为
plist
的文件不会返回数组。它可能代表一本字典。尝试创建 NSMutableDictionary 而不是数组。
First you should not check for
plist == null
but check forplist == nil
Second searching for the download file should be changed into the following:
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.