iOS*项目--第七天,初次读取json数据、KVC转模型技巧、运行时字典转模型以及初步对显示网页的操作并且跟踪标签

一、初次读取json数据

二、KVC转模型技巧,这里的技巧主要解决的是字典中的key 与 模型中有的属性对应不起来的时候 的解决办法

    

  <方法1>

<方法2>运行时字典转模型,运行时自己一直很晕。不过还是整理下来,方便以后用。 这里直接创建了一个分类。

  • 头文件代码
     1 //
     2 //  NSObject+Model.h
     3 //  Chaos_G
     4 //
     5 
     6 #import <Foundation/Foundation.h>
     7 
     8 @interface NSObject (Model)
     9 
    10 // 快速进行字典转模型
    11 // mapDict:模型中的哪个属性名跟字典里面的key对应
    12 + (instancetype)objcWithDict:(NSDictionary *)dict mapDict:(NSDictionary *)mapDict;
    13 
    14 @end
  • .m文件代码
     1 //
     2 //  NSObject+Model.m
     3 //  Chaos_G
     4 //
     5 
     6 #import "NSObject+Model.h"
     7 
     8 #import <objc/runtime.h>
     9 
    10 @implementation NSObject (Model)
    11 
    12 + (instancetype)objcWithDict:(NSDictionary *)dict mapDict:(NSDictionary *)mapDict
    13 {
    14     id objc = [[self alloc] init];
    15     
    16     
    17     // 遍历模型中属性
    18     unsigned int count = 0;
    19     Ivar *ivars = class_copyIvarList(self, &count);
    20     
    21     for (int i = 0 ; i < count; i++) {
    22         Ivar ivar = ivars[i];
    23         
    24         // 属性名称
    25         NSString *ivarName = @(ivar_getName(ivar));
    26         
    27         
    28         ivarName = [ivarName substringFromIndex:1];
    29         
    30         id value = dict[ivarName];
    31         // 需要由外界通知内部,模型中属性名对应字典里面的哪个key
    32         // ID -> id
    33         if (value == nil) {
    34             if (mapDict) {
    35             NSString *keyName = mapDict[ivarName];
    36                 
    37             value = dict[keyName];
    38             }
    39         }
    40 
    41             
    42             [objc setValue:value forKeyPath:ivarName];
    43         
    44         
    45     }
    46     
    47     
    48     return objc;
    49 }
    50 
    51 @end

三、利用UIWebView显示网页,显示网页过程中通过路径加载网页的URL时,路径中包含汉字的处理方法

  <方法1>

  <方法2>

  • UIWebView显示网页的具体代码如下
 1 #import "ChaosHtmlViewController.h"
 2 #import "ChaosHtml.h"
 3 
 4 @interface ChaosHtmlViewController () <UIWebViewDelegate>
 5 
 6 @end
 7 
 8 @implementation ChaosHtmlViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     
13     self.navigationItem.title = _htmlItem.title;
14     
15     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(dismiss)];
16     
17     // 取出webView 加载数据
18     UIWebView *web = (UIWebView *)self.view;
19     
20     web.delegate = self; // 设置web的代理,实现在web加载完成后,跳转到相应的网页标签
21     
22     // 直接通过URLForResource:这个方法  获取的url就是将汉字处理过的URL
23     NSURL *url = [[NSBundle mainBundle] URLForResource:_htmlItem.html withExtension:nil];
24     
25     NSURLRequest *request = [NSURLRequest requestWithURL:url];
26     
27     // web 加载请求,一步一步往上走,缺什么补什么
28     [web loadRequest:request];
29 }
30 
31 - (void)dismiss
32 {
33     [self dismissViewControllerAnimated:YES completion:nil];
34 }
35 
36 // 在加载view的方法里面,将控制器的view改成UIWebView
37 - (void)loadView
38 {
39     UIWebView *web = [[UIWebView alloc] initWithFrame:ChaosScreenBounds];
40     
41     self.view = web;
42 }
43 #pragma mark - webView的代理方法
44 // 执行JavaScript  必须在web加载完成的时候执行
45 - (void)webViewDidFinishLoad:(UIWebView *)webView
46 {
47     
48     NSString *javaStr = [NSString stringWithFormat:@"window.location.href = '#%@';",_htmlItem.ID];
49     
50     [webView stringByEvaluatingJavaScriptFromString:javaStr];
51 }
52 
53 @end
原文地址:https://www.cnblogs.com/gchlcc/p/5393423.html