iOS开发:从本地文件读取字符串:stringWithContentsOfFile&initWithContentsOfFile

2018-09-10 11:18:37 浏览数 (1)

1. 定义的区分

1. stringWithContentsOfFile
  • 用法

Returns a string created by reading data from the file at a given path interpreted using a given encoding.

  • 声明
代码语言:javascript复制
  (instancetype)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
2. initWithContentsOfFile
  • 用法

Returns an NSString object initialized by reading data from the file at a given path using a given encoding.

  • 声明
代码语言:javascript复制
- (instancetype)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;

2. 用法示例

1. 加载本地.json文件
  • stringWithContentsOfFile
代码语言:javascript复制
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"sectionMdl23" ofType:@"json"];
NSString *JSONString = [NSString stringWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"sssssssss%@",JSONString);
  • initWithContentsOfFile
代码语言:javascript复制
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"sectionMdl23" ofType:@"json"];
NSString *JSONString = [[NSString alloc] initWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:nil]];
NSLog(@"sssssssss%@",JSONString);
2. 加载本地.html文件
  • stringWithContentsOfFile
代码语言:javascript复制
NSString *path = [[NSBundle mainBundle] pathForResource:@"html/start" ofType:@"html"];
NSString *htmlString1 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
NSString *urlStr = [NSString stringWithFormat:@"%@/html",[[NSBundle mainBundle] bundlePath]];
NSURL *url = [NSURL fileURLWithPath:urlStr isDirectory:YES];
[self.webView loadHTMLString:htmlString1 baseURL:url];
  • initWithContentsOfFile
代码语言:javascript复制
NSString *path = [[NSBundle mainBundle] pathForResource:@"html/start" ofType:@"html"];
NSString *htmlString1 =[[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL]];
NSString *urlStr = [NSString stringWithFormat:@"%@/html",[[NSBundle mainBundle] bundlePath]];
NSURL *url = [NSURL fileURLWithPath:urlStr isDirectory:YES];
[self.webView loadHTMLString:htmlString1 baseURL:url];

3. 为了什么要读取本地.json和.html数据?

1. 场景:让webview加载本地html文件

可以利用loadHTMLString渲染html字符串的方式加载网页。

代码语言:javascript复制
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.titleStr;
    [self.webView loadHTMLString:[self getTheHtmlString] baseURL:nil];
}

#pragma mark - 处理html字符串
- (NSString *)getTheHtmlString{
    
    //1.配置模版信息
    NSString *path = [[NSBundle mainBundle] pathForResource:self.htmlName ofType:@"html"];
    NSString *htmlStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    //2.替换关键字: {电话}
    NSString *mobileno = self.getCustInfoModel.mobileno;
    if (nil == mobileno) {
         mobileno = @"";
    }
    strUrl = [htmlStr stringByReplacingOccurrencesOfString:@"{电话}" withString:mobileno];
    return strUrl;
}
2. 场景:由本地json文件决定VC的数据源

例如,下面的代码是为了从JSON读取设计好的数据,来决定一个VC的数据源。而这个VC是一个已经被封装好的类,其显示内容高度依赖于按照设定规则写好的JSON。

代码语言:javascript复制
//本地json
- (void)initData {
    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"sectionMdl23" ofType:@"json"];
    NSString *jsonStr = [[NSString alloc] initWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:nil];
    if (jsonStr != nil) {
    
        NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
        NSError *err;
        NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
        
        @try{
            SupplementModel *sectionArrModel = [SupplementModel mj_objectWithKeyValues:jsonDict];
            self.sectionMdlArr = sectionArrModel.data;
            
            //增加数据
            [self.sectionMdlArr enumerateObjectsUsingBlock:^(SectionModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj.sectionType == 2) {
                    obj.cellExampleModelArr = [obj.cellModelArr mutableCopy];
                }
            }];
            
        }@catch(NSException *exception) {
            [Toast showBottomWithText:kServerErrorMsg];
        }
        [self.tableView reloadData];
    }
}

0 人点赞