将plist放入数组

发布于 2024-11-08 00:03:02 字数 700 浏览 0 评论 0原文

我见过很多类似的问题,但没有任何对我有用。我需要将 plist 的内容放入 NSMutableArray 中。

我的 plist 是一个包含 1 个数组的字典。该数组有多个字典。我在另一个应用程序中使用了相同的代码并且它有效:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"TeamsData" ofType:@"plist"];
NSMutableArray *temp = [[NSMutableArray arrayWithContentsOfFile:path] retain];
NSLog(@"TEMP: %@", temp);
data = [NSMutableArray arrayWithArray:[[temp objectAtIndex:0] objectForKey:@"Teams"]];

temp 返回 NULL 但我不明白为什么。有什么想法吗?


更新

我实际上发现了为什么我的代码可以在一个应用程序中运行,而不能在另一个应用程序中运行。在应用程序中,当我打开 plist 作为源时,它确实可以工作,整个 plist 都包含在一个数组中。不过,当我在标准 xcode plist 编辑器中查看它时,它并没有显示出来。因此,为了让相同的代码在新的 plist 上工作,我可以将其作为源代码打开并将其包含在数组标签中。

I have seen lots of similar questions but nothing is working for me. I need to get the contents of my plist into an NSMutableArray.

My plist is a dictionary containing 1 array. This array has multiple dictionaries. I have used the same code in another app and it worked:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"TeamsData" ofType:@"plist"];
NSMutableArray *temp = [[NSMutableArray arrayWithContentsOfFile:path] retain];
NSLog(@"TEMP: %@", temp);
data = [NSMutableArray arrayWithArray:[[temp objectAtIndex:0] objectForKey:@"Teams"]];

temp returns NULL but I can't figure out why. Any ideas?


Update

I actually discovered why my code was working in one app and not the other. In the app it did work in when I opened the plist as source the entire plist was enclosed in an array. This didn't show up when I viewed it in the standard xcode plist editor though. So to get the same code working on the new plist I could just open it as source and enclose it in array tags.

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

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

发布评论

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

评论(2

瑾夏年华 2024-11-15 00:03:02

你说你有一个 plist,它是一个包含字典数组的字典,但你尝试从包中读取文件,就好像它是一个数组一样。尝试使用 NSDictionary-initWithContentsOfFile: 代替。

该数组应该在 plist 字典中具有一个键,如下所示:

...
<dict>
    <key>Teams</key>
    <array>
        <dict>
            ...
        </dict>
        ...
    </array>
</dict>

因此,提取数组应该是向 plist 字典询问带有键 Teams 的对象:

teamsArray = [ plistDictionary objectForKey: @"Teams" ];

You say you have a plist which is a dictionary containing an array of dictionaries, but you try to read the file from the bundle as if it were an array. Try using NSDictionary's -initWithContentsOfFile: instead.

The array should have a key in the plist's dictionary, something like the folllowing:

...
<dict>
    <key>Teams</key>
    <array>
        <dict>
            ...
        </dict>
        ...
    </array>
</dict>

So extracting the array should be a matter of asking the plist dictionary for the object with key Teams:

teamsArray = [ plistDictionary objectForKey: @"Teams" ];
窗影残 2024-11-15 00:03:02

别紧张 :)

NSString *path = [[NSBundle mainBundle] pathForResource:@"menuConfig" ofType:@"plist"];
NSDictionary* amountData =  [NSDictionary dictionaryWithContentsOfFile:path];
NSMutableArray *teamsArray=[amountData objectForKey: @"InfoArrays" ];
for(amountData in teamsArray) 
{
    NSMutableArray *title = [amountData objectForKey:@"Exercesis"];
    for(amountData in title)
    {
        NSMutableArray *ExercisesDetails = [amountData objectForKey:@"ExercisesDetails"];
        for(amountData in ExercisesDetails)
        {
            NSString *exerciseDetail=[amountData objectForKey:@"exerciseDetail"];
            NSLog(@"%@",exerciseDetail);
        }
    }
}

Take it easy :)

NSString *path = [[NSBundle mainBundle] pathForResource:@"menuConfig" ofType:@"plist"];
NSDictionary* amountData =  [NSDictionary dictionaryWithContentsOfFile:path];
NSMutableArray *teamsArray=[amountData objectForKey: @"InfoArrays" ];
for(amountData in teamsArray) 
{
    NSMutableArray *title = [amountData objectForKey:@"Exercesis"];
    for(amountData in title)
    {
        NSMutableArray *ExercisesDetails = [amountData objectForKey:@"ExercisesDetails"];
        for(amountData in ExercisesDetails)
        {
            NSString *exerciseDetail=[amountData objectForKey:@"exerciseDetail"];
            NSLog(@"%@",exerciseDetail);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文