为什么naam输出(NULL)? (对不起,知道这是基本的东西,但我是新的plists)
这是plist:
以下是方法:
- (id) init {
self = [super init];
if (self) {
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
NSLog(@"path: %@",plistPath);
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
NSLog(@"temp: %@",temp);
if (!temp) {
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
self.personName = [temp objectForKey:@"Name"];
NSLog(@"NAAM:%@",[temp objectForKey:@"Name"]);
self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:@"Phones"]];
}
return self;}
答案 0 :(得分:1)
因为temp是一个只包含一个键的字典:@“Root”。您正在寻找内部词典中的对象:[[temp objectForKey:@"Root"] objectForKey:@"Name"]
答案 1 :(得分:1)
试
NSLog(@"NAAM: %@", [temp valueForKeyPath:@"Root.Name"]);
从Phones
开始使用第一部手机:
NSDictionary *root = [temp valueForKey:@"Root"];
[[root valueForKey:@"Phones"] objectAtIndex:0];
答案 2 :(得分:1)
首先尝试提取ROOT。
NSDictionary* root=[temp objectForKey:@"Root"];
NSLog(@"NAAM:%@",[root objectForKey:@"Name"]);
答案 3 :(得分:0)
我的“Words.plist”
<dict>
<key>Root</key>
<array>
<string>sunday</string>
<string>monday</string>
<integer>44</integer>
</array>
</dict>
NSString *StringsFromPList = [[NSBundle mainBundle] bundlePath];
NSString *itemPositionPlistLocation = [StringsFromPList stringByAppendingPathComponent:@"Words.plist"];
_myDictionary= [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation];
NSArray * items = [_myDictionary objectForKey:@"Root"];
NSLog(@"%@", items);
希望它有所帮助:)