前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS开发:从本地文件读取字符串:stringWithContentsOfFile&initWithContentsOfFile

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

作者头像
陈满iOS
发布2018-09-10 11:18:37
3K0
发布2018-09-10 11:18:37
举报
文章被收录于专栏:陈满iOS陈满iOS

1. 定义的区分

1. stringWithContentsOfFile
  • 用法

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

  • 声明
+ (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.

  • 声明
- (instancetype)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;

2. 用法示例

1. 加载本地.json文件
  • stringWithContentsOfFile
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"sectionMdl23" ofType:@"json"];
NSString *JSONString = [NSString stringWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"sssssssss%@",JSONString);
  • initWithContentsOfFile
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"sectionMdl23" ofType:@"json"];
NSString *JSONString = [[NSString alloc] initWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:nil]];
NSLog(@"sssssssss%@",JSONString);
2. 加载本地.html文件
  • stringWithContentsOfFile
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
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字符串的方式加载网页。

- (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。

//本地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];
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.03.27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 定义的区分
  • 2. 用法示例
  • 3. 为了什么要读取本地.json和.html数据?
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档