PLIST Array对的NSDictionary(Plist Array to NSDiction

2019-06-21 04:43发布

我有一个plist中:

<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这样的:

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数组计数,但零字典计数。 所以,我想通过数组循环并将其添加到字典:

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'

状态与匹配我的plist属性的类。 我究竟做错了什么? 我看到各种相关的问题在这里,但我不能得到它。

Answer 1:

这两个问题:

  • 加载的plist到一个NSDictionary:

这是一个简单的问题,看来你已经想通了。 在你的plist全局对象是一个数组,而不是一个字典,所以当你将其加载到字典,它不知道该怎么办(不相容类型),使你得到一个空的字典。

  • 通过词典的阵列循环:

从异常你得到,你在呼唤“的setObject:forKey:”在字典中,它被初始化为一个N​​SDictionary,而不是一个NSMutableDictionary。 指针的类型为的NSMutableDictionary,但不是在内存中对象的实际。 您需要从改变你的线。

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

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

实际上,因为从文件加载字典为您提供了一个空的字典,你是在浪费周期试图从文件加载它,应该只创建一个新的:

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


Answer 2:

一种更灵活的方式的Plist加载到内存中,也允许您创建可变的Plist:

NSData* data = [NSData dataWithContentsOfFile:path];
NSMutableArray* plist = [NSPropertyListSerialization propertyListFromData:data
                                                         mutabilityOption:NSPropertyListImmutable
                                                                   format:NSPropertyListXMLFormat_v1_0
                                                         errorDescription:NULL];

因此,你的代码可以实现:

NSString *path = [[NSBundle mainBundle] pathForResource:@"stateInfo" ofType:@"plist"];
NSData* data = [NSData dataWithContentsOfFile:path];
NSMutableArray* array = [NSPropertyListSerialization propertyListFromData:data
                                                         mutabilityOption:NSPropertyListImmutable
                                                                   format:NSPropertyListXMLFormat_v1_0
                                                         errorDescription:NULL];
if (array) {
  NSMutableDictionary* myDict = [NSMutableDictionary dictionaryWithCapacity:[array count]];
  for (NSDictionary* dict in array) {
    State* state = [[State alloc] initWithDictionary:dict];
    [myDict setObject:state forKey:state.name;
    [state release];
  }
  NSLog(@"The count: %i", [myDic count]);
} else {
  NSLog(@"Plist does not exist");
}


文章来源: Plist Array to NSDictionary