在项目中,经常需要读取配置文件,现在就来看看读取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];

 

相关文章:

  • 2022-12-23
  • 2021-10-13
  • 2022-01-12
  • 2021-05-31
  • 2022-12-23
  • 2021-09-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-04-10
  • 2022-12-23
  • 2021-12-24
  • 2022-03-10
  • 2022-12-23
相关资源
相似解决方案