Bry*_*yan 11 objective-c nsdictionary plist nsarray
我有一个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>
Run Code Online (Sandbox Code Playgroud)
我试图将它加载到像这样的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]);
Run Code Online (Sandbox Code Playgroud)
我总是得到50的数组,但字典数为零.所以我尝试循环遍历数组并将其添加到字典中:
NSDictionary *eachState;
for (eachState in thisArray) {
State *thisState = [[State alloc] initWithDictionary:eachState];
[myDic setObject:thisState forKey:thisState.name];
}
Run Code Online (Sandbox Code Playgroud)
但循环抛出一个异常:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
State是一个属性与我的plist匹配的类.我究竟做错了什么?我在这里看到了各种相关的问题,但我无法理解.
bob*_*vil 17
这两个问题:
这是一个简单的问题,似乎你已经想到了.plist中的全局对象是一个数组,而不是一个dict,因此当你将它加载到字典中时,它不知道该做什么(不兼容的类型),所以你得到一个空字典.
从你得到的Exception,你在字典上调用'setObject:forKey:',它被初始化为NSDictionary,而不是NSMutableDictionary.指针的类型为NSMutableDictionary,但不是实际的内存对象.你需要改变你的线路.
NSMutableDictionary *myDic = [[NSDictionary alloc] initWithContentsOfFile:path];
Run Code Online (Sandbox Code Playgroud)
至
NSMutableDictionary *myDic = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
Run Code Online (Sandbox Code Playgroud)
实际上,因为从文件加载字典会给你一个空字典,你就是在浪费周期试图从文件中加载它,并且应该创建一个新字典:
NSMutableDictionary *myDic = [[NSMutableDictionary alloc] init];
Run Code Online (Sandbox Code Playgroud)
Pey*_*loW 10
将plist加载到内存中的一种更灵活的方法,也允许您创建可变的plist:
NSData* data = [NSData dataWithContentsOfFile:path];
NSMutableArray* plist = [NSPropertyListSerialization propertyListFromData:data
mutabilityOption:NSPropertyListImmutable
format:NSPropertyListXMLFormat_v1_0
errorDescription:NULL];
Run Code Online (Sandbox Code Playgroud)
因此,您的代码可以实现为:
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");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40221 次 |
| 最近记录: |