iOS获取文件路径

文件在工程的存放结构

iOS获取文件路径_第1张图片

 

具体代码如下:

//文件路径
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];

  

 

转载于:https://www.cnblogs.com/hacjy/p/6007226.html

你可能感兴趣的:(iOS获取文件路径)