首页 > 解决方案 > Objective-c 访问 plist 中的值

问题描述

我正在修改其他人的代码并努力访问 plist 中的值,我可以使用以下代码访问 Info.plist 等:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Info.plist"];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:finalPath];

NSLog(@"ret=%@", plistData);

但是,当尝试访问 Account.plist 时,它返回 null,我假设这是路径问题?

任何帮助/指针表示赞赏。

在此处输入图像描述

标签: iosobjective-cxcode

解决方案


以下是如何从 plist 中获取 Dictionary 或 Array 的示例。

//*******************************************
- (NSArray *)loadArrayFromPlist:(NSString *)plistName {
    NSString *plistPath = [[NSBundle mainBundle] pathForResource: plistName ofType: @"plist"];
    return [NSArray arrayWithContentsOfFile: plistPath];
}

//*******************************************
- (NSDictionary *)loadDictionaryFromPlist:(NSString *)plistName {
    NSString *plistPath = [[NSBundle mainBundle] pathForResource: plistName ofType: @"plist"];
    return [NSDictionary dictionaryWithContentsOfFile: plistPath];
}

推荐阅读