文件在工程的存放结构
具体代码如下:
//文件路径
NSBundle *bundle = [NSBundle bundleForClass:[PopQuestionUIView class]];
NSURL *url = [bundle URLForResource:@"Resources" withExtension:@"bundle"];
NSBundle *resBundle = [NSBundle bundleWithURL:url];
NSString *filePath = [resBundle pathForResource:@"popQuestionLocalData" ofType:@"txt"]; // NSData类方法读取数据
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"NSData类方法读取的内容是:%@", jsonString); NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];