我正在尝试读取~/Library/Preferences/com.apple.mail.plist (在Snow Leopard上)以获取电子邮件地址和其他信息,以便进入about对话框。我使用了下面的代码,这显然是错误的:
NSBundle* bundle;
bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"~/Library/Preferences/com.apple.mail.plist" ofType:@"plist"];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSString *item = [plistData valueForKeyPath:@"MailAccounts.Item 2.AccountName"];
NSLog(@"Result = %@", item);此外,我需要读取的值是MailAcounts -> Item 2 -> AccountName,并且我不确定我这样做是否正确(由于Item 2密钥中的空格)。
我试着阅读苹果的plist文件的开发者指南,但没有帮助。
如何读取plist并将值提取为NSString?
谢谢。
发布于 2011-12-29 06:45:36
第一层是一个数组,因此需要使用"MailAccounts.AccountName"并将其视为NSArray*
NSString *plistPath = [@"~/Library/Preferences/com.apple.mail.plist" stringByExpandingTildeInPath];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSArray *item = [plistData valueForKeyPath:@"MailAccounts.AccountName"];
NSLog(@"Account: %@", [item objectAtIndex:2]);或者,您可以按键从"MailAccounts"中提取数组,首先使用valueForKey: (这将生成NSArray*),然后使用objectAtIndex:来获取该特定帐户的字典(如果您需要的不只是名称,则非常有用)。
发布于 2011-12-29 03:40:26
两件事:
NSBundle来获取文件的路径。该文件位于应用程序捆绑包之外。所以你应该干脆NSString *plistPath =plistPath必须在用户目录的路径中展开波浪号。NSString为此提供了一种方法。使用像这样的东西
NSString *plistPath =@“~/库/首选项/com.apple.mail.plist”plistPath
https://stackoverflow.com/questions/8660343
复制相似问题