IOS 读取项目中的文件方法

在项目中,经常需要读取配置文件,现在就来看看读取plist配置文件的代码

//读取项目下的文件路径
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"fileName" ofType:@"plist"];

//可转换成URL
NSURL *url = [NSURL fileURLWithPath:path];

//如NSXML就需要提供URL地址
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];

//换成NSData
NSData *data = [[NSData alloc]initWithContentsOfFile:path];

 

//可直接转换成对应文件格式
//当plist文件为字典是可用以下方法
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:plistPath];
//当plist文件为数组类型可以使用以下方法
NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
原文地址:https://www.cnblogs.com/oscar1987121/p/5229441.html