Plist阵列到NSDictionary [英] Plist Array to NSDictionary

查看:69
本文介绍了Plist阵列到NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个清单:

<plist version="1.0">
  <array>
    <dict>
      <key>name</key>
      <string>Alabama</string>
      <key>abreviation</key>
      <string>AL</string>
      <key>date</key>
      <string>1819</string>
      <key>population</key>
      <string>4,627,851</string>
      <key>capital</key>
      <string>Montgomery</string>
      <key>largestCity</key>
      <string>Birmingham</string>
    </dict>
    <dict>
      <key>name</key>
      <string>Alaska</string>
      <key>abreviation</key>
      <string>AK</string>
      <key>date</key>
      <string>1959</string>
      <key>population</key>
      <string>683,478</string>
      <key>capital</key>
      <string>Juneau</string>
      <key>largestCity</key>
      <string>Anchorage</string>
    </dict>
    ...
  </array>
</plist>

我正在尝试像这样将其加载到NSDictionary中:

I am trying to load it into an NSDictionary like this:

NSString *path = [[NSBundle mainBundle] pathForResource:@"stateInfo" ofType:@"plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
    NSLog(@"The file exists");
} else {
    NSLog(@"The file does not exist");
}

NSMutableDictionary *myDic = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
//NSDictionary *myDic = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"The count: %i", [myDic count]);

NSArray *thisArray = [[NSArray alloc] initWithContentsOfFile:path];
NSLog(@"The array count: %i", [thisArray count]);

我总是得到一个数组计数为50,但字典计数为零.因此,我尝试遍历数组并将其添加到字典中:

I always get an array count of 50 but a dictionary count of zero. So I tried looping through the array and adding it to the dictionary:

NSDictionary *eachState;
for (eachState in thisArray) {
    State *thisState = [[State alloc] initWithDictionary:eachState];
    [myDic setObject:thisState forKey:thisState.name];
}

但是循环抛出异常:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'

State是一个类,其属性与我的plist相匹配.我究竟做错了什么?我在这里看到了各种各样的相关问题,但我不明白.

State is a class with properties matching my plist. What am I doing wrong? I see all kinds of related questions out here but I can't get it.

推荐答案

两个问题:

  • 将plist加载到NSDictionary中:

这是一个简单的问题,看来您已经知道了. plist中的全局对象是一个数组,而不是字典,因此将其加载到字典中时,它不知道要做什么(不兼容的类型),因此您得到的是空字典.

This is a simple problem, which it seems you have already figured out. The global object in your plist is an array, not a dict, so when you load it into the dictionary, it doesn't know what to do (incompatable types), so you're getting an empty dictionary.

  • 遍历字典数组:

从得到的异常中,您正在字典上调用'setObject:forKey:',该字典被初始化为NSDictionary,而不是NSMutableDictionary.指针的类型为NSMutableDictionary,但不是实际的内存对象.您需要更改行.

From the Exception you're getting, you are calling 'setObject:forKey:' on the dictionary, which is initialized as an NSDictionary, not an NSMutableDictionary. The pointer is typed as NSMutableDictionary, but not the actual in memory object. You need to change your line from.

NSMutableDictionary *myDic = [[NSDictionary alloc] initWithContentsOfFile:path];

NSMutableDictionary *myDic = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

实际上,由于从文件中加载字典为您提供了一个空的字典,因此您浪费了尝试从文件中加载字典的周期,应该创建一个新的字典:

and actually, since loading the dictionary from the file gives you an empty dictionary, you are wasting cycles trying to load it from the file, and should just create a new one:

NSMutableDictionary *myDic = [[NSMutableDictionary alloc] init];

这篇关于Plist阵列到NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆