开发者

Reading from and writing to plists in xcode 4.0.2 - issue retrieving from dictionary

I have been following this Tutorial on how to read from a plist. However i cannot read the string with key name. i tried putting NSLog("%@", [temp objectForKey:@"Name"]); but it returns null. i had a similar issue before which involved opening the xml file and manually changing dict to array. How can i fix this?

the hierarchy in the xml file is what confuses me. the dictionary i created named root with string John Doe is part of a dictionary itself? Clarification appreciated!

From the tutorial linked above:

// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
    // if not in documents, get property list from main bundle
    plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}

// read property list into memory as an NSData object
NSData *plistXML = [[NSFile开发者_运维问答Manager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property liost into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp)
{
    NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}

NSLog(@"%@", [temp objectForKey:@"Name"];


Most likely this is because there is no object with key @"Name" (remember that Xcode is case sensitive!) in the (presumably) NSDictionary named temp.

For a more specific answer, you'll need to post the code where you define temp.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜