-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
857 lines (422 loc) · 95.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
<!DOCTYPE html>
<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">
<link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">
<meta name="keywords" content="Hexo, NexT" />
<meta property="og:type" content="website">
<meta property="og:title" content="Jungle blogs">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Jungle blogs">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Jungle blogs">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Pisces',
version: '5.1.4',
sidebar: {"position":"right","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
fancybox: true,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="http://yoursite.com/"/>
<title>Jungle blogs</title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container sidebar-position-right
page-home">
<div class="headband"></div>
<a href="https://github.com/JumpX"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">Jungle blogs</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle"></p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about/" rel="section">
<i class="menu-item-icon fa fa-fw fa-user"></i> <br />
关于
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
标签
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section">
<i class="menu-item-icon fa fa-fw fa-th"></i> <br />
分类
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
归档
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/09/20/YYModel解读/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Jungle">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/jun.jpeg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jungle blogs">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/09/20/YYModel解读/" itemprop="url">YYModel解读</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-20T15:38:56+08:00">
2018-09-20
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><h4 id="YYModel-:一个非常轻量级的-JSON-模型自动转换库,代码风格良好且思路清晰,可以从源码中看到作者对-Runtime-深厚的理解。难能可贵的是-YYModel-在其轻量级的代码下还保留着自动类型转换,类型安全,无侵入等特性,并且具有接近手写解析代码的超高性能。"><a href="#YYModel-:一个非常轻量级的-JSON-模型自动转换库,代码风格良好且思路清晰,可以从源码中看到作者对-Runtime-深厚的理解。难能可贵的是-YYModel-在其轻量级的代码下还保留着自动类型转换,类型安全,无侵入等特性,并且具有接近手写解析代码的超高性能。" class="headerlink" title="YYModel :一个非常轻量级的 JSON 模型自动转换库,代码风格良好且思路清晰,可以从源码中看到作者对 Runtime 深厚的理解。难能可贵的是 YYModel 在其轻量级的代码下还保留着自动类型转换,类型安全,无侵入等特性,并且具有接近手写解析代码的超高性能。"></a><a href="https://github.com/ibireme/YYModel" target="_blank" rel="noopener">YYModel</a> :一个非常轻量级的 JSON 模型自动转换库,代码风格良好且思路清晰,可以从源码中看到作者对 Runtime 深厚的理解。难能可贵的是 YYModel 在其轻量级的代码下还保留着自动类型转换,类型安全,无侵入等特性,并且具有接近手写解析代码的超高性能。</h4><h3 id="一、先从objc4说起"><a href="#一、先从objc4说起" class="headerlink" title="一、先从objc4说起"></a>一、先从<a href="https://opensource.apple.com/tarballs/objc4/" target="_blank" rel="noopener">objc4</a>说起</h3><h4 id="Class"><a href="#Class" class="headerlink" title="Class"></a>Class</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">struct objc_class {</span><br><span class="line"> Class isa OBJC_ISA_AVAILABILITY;</span><br><span class="line"> Class super_class;</span><br><span class="line"> const char *name;</span><br><span class="line"> long version;</span><br><span class="line"> long info;</span><br><span class="line"> long instance_size;</span><br><span class="line"> struct objc_ivar_list *ivars;</span><br><span class="line"> struct objc_method_list **methodLists;</span><br><span class="line"> struct objc_cache *cache;</span><br><span class="line"> struct objc_protocol_list *protocols;</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<h5 id="Method(方法)、Ivar(成员变量)、Property(属性)"><a href="#Method(方法)、Ivar(成员变量)、Property(属性)" class="headerlink" title="Method(方法)、Ivar(成员变量)、Property(属性)"></a>Method(方法)、Ivar(成员变量)、Property(属性)</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">typedef struct method_t *Method;</span><br><span class="line">typedef struct ivar_t *Ivar;</span><br><span class="line">typedef struct property_t *objc_property_t;</span><br></pre></td></tr></table></figure>
<h4 id="Method(方法)"><a href="#Method(方法)" class="headerlink" title="Method(方法)"></a>Method(方法)</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">struct objc_method {</span><br><span class="line"> SEL method_name;</span><br><span class="line"> char *method_types;</span><br><span class="line"> IMP method_imp;</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">struct objc_method_list {</span><br><span class="line"> struct objc_method_list *obsolete;</span><br><span class="line"> int method_count;</span><br><span class="line"> int space;</span><br><span class="line"> struct objc_method method_list[1];</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">struct objc_method_description {</span><br><span class="line"> SEL name; /**< The name of the method */</span><br><span class="line"> char *types; /**< The types of the method arguments */</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">struct objc_method_description_list {</span><br><span class="line"> int count;</span><br><span class="line"> struct objc_method_description list[1];</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">//获取方法列表</span><br><span class="line">Method *class_copyMethodList(Class cls, unsigned int *outCount);</span><br><span class="line"></span><br><span class="line">//获取方法名</span><br><span class="line">SEL method_getName(Method m);</span><br><span class="line"></span><br><span class="line">//获取方法实现</span><br><span class="line">IMP method_getImplementation(Method m);</span><br><span class="line"></span><br><span class="line">//获取参数类型和返回类型</span><br><span class="line">const char *method_getTypeEncoding(Method m);</span><br></pre></td></tr></table></figure>
<h4 id="Ivar(成员变量)"><a href="#Ivar(成员变量)" class="headerlink" title="Ivar(成员变量)"></a>Ivar(成员变量)</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">struct objc_ivar_list {</span><br><span class="line"> int ivar_count;</span><br><span class="line"> int space;</span><br><span class="line"> struct objc_ivar ivar_list[1];</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">struct objc_ivar {</span><br><span class="line"> char *ivar_name;</span><br><span class="line"> char *ivar_type;</span><br><span class="line"> int ivar_offset;</span><br><span class="line"> int space;</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">//基地址偏移字节, 对象地址 + ivar偏移字节 -> 访问ivar</span><br><span class="line">ptrdiff_t ivar_getOffset(Ivar ivar);</span><br><span class="line"></span><br><span class="line">//成员变量名</span><br><span class="line">const char *ivar_getName(Ivar ivar);</span><br><span class="line"></span><br><span class="line">//成员变量类型</span><br><span class="line">const char *ivar_getTypeEncoding(Ivar ivar);</span><br></pre></td></tr></table></figure>
<h4 id="Property(属性)"><a href="#Property(属性)" class="headerlink" title="Property(属性)"></a>Property(属性)</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">struct property_t {</span><br><span class="line"> const char *name;</span><br><span class="line"> const char *attributes;</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">typedef struct {</span><br><span class="line"> const char *name; /**< The name of the attribute */</span><br><span class="line"> const char *value; /**< The value of the attribute (usually empty) */</span><br><span class="line">} objc_property_attribute_t;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">//获取Class的属性列表</span><br><span class="line">objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount);</span><br><span class="line"></span><br><span class="line">//获取单个属性名称</span><br><span class="line">const char *property_getName(objc_property_t prop);</span><br><span class="line"></span><br><span class="line">//获取单个属性详细</span><br><span class="line">const char *property_getAttributes(objc_property_t prop);</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">//复制单个属性的详细:</span><br><span class="line">objc_property_attribute_t *property_copyAttributeList(objc_property_t prop, </span><br><span class="line"> unsigned int *outCount);</span><br></pre></td></tr></table></figure>
<h5 id="举个例子:"><a href="#举个例子:" class="headerlink" title="举个例子:"></a>举个例子:</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">@property (nonatomic, copy, getter=getName, setter=setName:) NSString *name;</span><br></pre></td></tr></table></figure>
<h5 id="获取name的Attributes-lt-name-value-gt"><a href="#获取name的Attributes-lt-name-value-gt" class="headerlink" title="获取name的Attributes<name,value>:"></a>获取name的Attributes<name,value>:</h5><table>
<thead>
<tr>
<th style="text-align:center">index</th>
<th style="text-align:center">name</th>
<th style="text-align:center">value</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">0</td>
<td style="text-align:center">T</td>
<td style="text-align:center">@\”NSString\”</td>
<td style="text-align:center">类型</td>
</tr>
<tr>
<td style="text-align:center">1</td>
<td style="text-align:center">C</td>
<td style="text-align:center">-</td>
<td style="text-align:center">copy</td>
</tr>
<tr>
<td style="text-align:center">2</td>
<td style="text-align:center">N</td>
<td style="text-align:center">-</td>
<td style="text-align:center">nonatomic</td>
</tr>
<tr>
<td style="text-align:center">3</td>
<td style="text-align:center">G</td>
<td style="text-align:center">getName</td>
<td style="text-align:center">getter</td>
</tr>
<tr>
<td style="text-align:center">4</td>
<td style="text-align:center">S</td>
<td style="text-align:center">setName:</td>
<td style="text-align:center">setter</td>
</tr>
<tr>
<td style="text-align:center">5</td>
<td style="text-align:center">V</td>
<td style="text-align:center">_name</td>
<td style="text-align:center">ivarName</td>
</tr>
</tbody>
</table>
<h4 id="还有很多其他苹果规定的type,具体可查看以下链接:"><a href="#还有很多其他苹果规定的type,具体可查看以下链接:" class="headerlink" title="还有很多其他苹果规定的type,具体可查看以下链接:"></a>还有很多其他苹果规定的type,具体可查看以下链接:</h4><h6 id="Type-Encodings"><a href="#Type-Encodings" class="headerlink" title="Type Encodings"></a><a href="https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html" target="_blank" rel="noopener">Type Encodings</a></h6><h6 id="Declared-Properties"><a href="#Declared-Properties" class="headerlink" title="Declared Properties"></a><a href="https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW5" target="_blank" rel="noopener">Declared Properties</a></h6><h4 id="Others"><a href="#Others" class="headerlink" title="Others"></a>Others</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">struct objc_category {</span><br><span class="line"> char *category_name;</span><br><span class="line"> char *class_name;</span><br><span class="line"> struct objc_method_list *instance_methods;</span><br><span class="line"> struct objc_method_list *class_methods;</span><br><span class="line"> struct objc_protocol_list *protocols;</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">struct objc_protocol_list {</span><br><span class="line"> struct objc_protocol_list *next;</span><br><span class="line"> long count;</span><br><span class="line"> __unsafe_unretained Protocol *list[1];</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">struct objc_cache {</span><br><span class="line"> unsigned int mask /* total = mask + 1 */;</span><br><span class="line"> unsigned int occupied;</span><br><span class="line"> Method buckets[1];</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<p>–</p>
<h3 id="二、YYModel文件目录"><a href="#二、YYModel文件目录" class="headerlink" title="二、YYModel文件目录"></a>二、YYModel文件目录</h3><h4 id="3个文件:"><a href="#3个文件:" class="headerlink" title="3个文件:"></a>3个文件:</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">YYModel.h</span><br><span class="line"></span><br><span class="line">YYClassInfo.h</span><br><span class="line">YYClassInfo.m</span><br><span class="line"></span><br><span class="line">NSObject+YYModel.h</span><br><span class="line">NSObject+YYModel.m</span><br></pre></td></tr></table></figure>
<h4 id="YYModel-h"><a href="#YYModel-h" class="headerlink" title="YYModel.h"></a>YYModel.h</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">YYModel.h</span><br><span class="line"></span><br><span class="line">#import <Foundation/Foundation.h></span><br><span class="line"></span><br><span class="line">#if __has_include(<YYModel/YYModel.h>)</span><br><span class="line">FOUNDATION_EXPORT double YYModelVersionNumber;</span><br><span class="line">FOUNDATION_EXPORT const unsigned char YYModelVersionString[];</span><br><span class="line">#import <YYModel/NSObject+YYModel.h></span><br><span class="line">#import <YYModel/YYClassInfo.h></span><br><span class="line">#else</span><br><span class="line">#import "NSObject+YYModel.h"</span><br><span class="line">#import "YYClassInfo.h"</span><br><span class="line">#endif</span><br></pre></td></tr></table></figure>
<h4 id="YYClassInfo:"><a href="#YYClassInfo:" class="headerlink" title="YYClassInfo:"></a>YYClassInfo:</h4><h5 id="管理-4个类,分别是对class、ivar、method、property的封装"><a href="#管理-4个类,分别是对class、ivar、method、property的封装" class="headerlink" title="管理 4个类,分别是对class、ivar、method、property的封装"></a>管理 4个类,分别是对class、ivar、method、property的封装</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">YYClassInfo.h</span><br><span class="line"></span><br><span class="line">@interface YYClassInfo : NSObject</span><br><span class="line">@interface YYClassIvarInfo : NSObject</span><br><span class="line">@interface YYClassMethodInfo : NSObject</span><br><span class="line">@interface YYClassPropertyInfo : NSObject</span><br></pre></td></tr></table></figure>
<h5 id="将-Runtime-层级的一些结构体封装到-NSObject-层级以便调用;"><a href="#将-Runtime-层级的一些结构体封装到-NSObject-层级以便调用;" class="headerlink" title="将 Runtime 层级的一些结构体封装到 NSObject 层级以便调用;"></a>将 Runtime 层级的一些结构体封装到 NSObject 层级以便调用;</h5><table>
<thead>
<tr>
<th style="text-align:left">YYClassInfo(NSObject层级)</th>
<th style="text-align:left">Runtime层级</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">YYClassInfo</td>
<td style="text-align:left">objc_class</td>
</tr>
<tr>
<td style="text-align:left">YYClassIvarInfo</td>
<td style="text-align:left">objc_ivar</td>
</tr>
<tr>
<td style="text-align:left">YYClassMethodInfo</td>
<td style="text-align:left">objc_method</td>
</tr>
<tr>
<td style="text-align:left">YYClassPropertyInfo</td>
<td style="text-align:left">property_t</td>
</tr>
</tbody>
</table>
<h4 id="NSObject-YYModel:"><a href="#NSObject-YYModel:" class="headerlink" title="NSObject+YYModel:"></a>NSObject+YYModel:</h4><h6 id="主要负责提供方便调用的接口、实现具体的模型转换逻辑(借助YYClassInfo-中的封装)"><a href="#主要负责提供方便调用的接口、实现具体的模型转换逻辑(借助YYClassInfo-中的封装)" class="headerlink" title="主要负责提供方便调用的接口、实现具体的模型转换逻辑(借助YYClassInfo 中的封装)"></a>主要负责提供方便调用的接口、实现具体的模型转换逻辑(借助YYClassInfo 中的封装)</h6><ul>
<li>2个私有类(_YYModelMeta、_YYModelPropertyMeta)</li>
<li>1个主类(NSObject (YYModel))</li>
<li>1个协议(@protocol YYModel <nsobject>)</nsobject></li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">NSObject+YYModel.h</span><br><span class="line"></span><br><span class="line">@interface _YYModelMeta : NSObject</span><br><span class="line">@interface _YYModelPropertyMeta : NSObject</span><br><span class="line">@interface NSObject (YYModel)</span><br><span class="line">@protocol YYModel <NSObject></span><br></pre></td></tr></table></figure>
<ul>
<li>_YYModelMeta :表示Model的类信息,包含YYClassInfo;</li>
<li>_YYModelPropertyMeta :表示Model对象的属性信息,包含YYClassPropertyInfo;</li>
</ul>
<h6 id="以下三者关系:"><a href="#以下三者关系:" class="headerlink" title="以下三者关系:"></a>以下三者关系:</h6><ul>
<li>_YYModelMeta、_YYModelPropertyMeta</li>
<li>YYClassInfo(NSObject层级)</li>
<li>Runtime层级</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">NSObject+YYModel(逻辑实现层)</th>
<th style="text-align:left">YYClassInfo(NSObject层级)</th>
<th style="text-align:left">Runtime层级</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">_YYModelMeta</td>
<td style="text-align:left">YYClassInfo</td>
<td style="text-align:left">objc_class</td>
</tr>
<tr>
<td style="text-align:center">-</td>
<td style="text-align:left">YYClassIvarInfo</td>
<td style="text-align:left">objc_ivar</td>
</tr>
<tr>
<td style="text-align:center">-</td>
<td style="text-align:left">YYClassMethodInfo</td>
<td style="text-align:left">objc_method</td>
</tr>
<tr>
<td style="text-align:center">_YYModelPropertyMeta</td>
<td style="text-align:left">YYClassPropertyInfo</td>
<td style="text-align:left">property_t</td>
</tr>
</tbody>
</table>
<h3 id="三、模型转换逻辑实现"><a href="#三、模型转换逻辑实现" class="headerlink" title="三、模型转换逻辑实现"></a>三、模型转换逻辑实现</h3><h4 id="1-JSON转Model分三个步骤:"><a href="#1-JSON转Model分三个步骤:" class="headerlink" title="1.JSON转Model分三个步骤:"></a>1.JSON转Model分三个步骤:</h4><ul>
<li><strong>JSON - to - Data</strong></li>
<li><strong>Data - to - Dictionary</strong></li>
<li><strong>Dictionary - to - Model</strong></li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">//JSON —> Data</span><br><span class="line">NSData *jsonData = [(NSString *)json dataUsingEncoding : NSUTF8StringEncoding];</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">//Data —> Dictionary</span><br><span class="line">NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:NULL];</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">//dictionary —> Model (一个方法就实现了!)</span><br><span class="line"></span><br><span class="line">[self modelSetWithDictionary:dictionary]</span><br></pre></td></tr></table></figure>
<h4 id="2-具体分析-dictionary-转换-Model"><a href="#2-具体分析-dictionary-转换-Model" class="headerlink" title="2.具体分析 dictionary 转换 Model"></a>2.具体分析 dictionary 转换 Model</h4><h6 id="主要实现方法:"><a href="#主要实现方法:" class="headerlink" title="主要实现方法:"></a>主要实现方法:</h6><h5 id="BOOL-modelSetWithDictionary-NSDictionary-dic"><a href="#BOOL-modelSetWithDictionary-NSDictionary-dic" class="headerlink" title="- (BOOL)modelSetWithDictionary:(NSDictionary *)dic;"></a>- (BOOL)modelSetWithDictionary:(NSDictionary *)dic;</h5><h6 id="看这个方法之前,先看看-YYModelMeta、-YYModelPropertyMeta的包:"><a href="#看这个方法之前,先看看-YYModelMeta、-YYModelPropertyMeta的包:" class="headerlink" title="看这个方法之前,先看看_YYModelMeta、_YYModelPropertyMeta的包:"></a>看这个方法之前,先看看_YYModelMeta、_YYModelPropertyMeta的包:</h6><h5 id="YYModelMeta:"><a href="#YYModelMeta:" class="headerlink" title="_YYModelMeta:"></a>_YYModelMeta:</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">@interface _YYModelMeta : NSObject {</span><br><span class="line"> ///用于框架类的实例变量,@private太限制,@protected或@public又太开放,就使用这个package吧</span><br><span class="line"> @package</span><br><span class="line"> </span><br><span class="line"> ///class信息</span><br><span class="line"> YYClassInfo *_classInfo;</span><br><span class="line"> </span><br><span class="line"> /// Key:key 和 keyPath, Value:_YYModelPropertyMeta.</span><br><span class="line"> NSDictionary *_mapper;</span><br><span class="line"> </span><br><span class="line"> /// Array<_YYModelPropertyMeta></span><br><span class="line"> NSArray *_allPropertyMetas;</span><br><span class="line"> </span><br><span class="line"> /// Array<_YYModelPropertyMeta> path.</span><br><span class="line"> NSArray *_keyPathPropertyMetas;</span><br><span class="line"> </span><br><span class="line"> /// Array<_YYModelPropertyMeta> meta 对应多个 key</span><br><span class="line"> NSArray *_multiKeysPropertyMetas;</span><br><span class="line"> </span><br><span class="line"> /// key 和 keyPath 个数</span><br><span class="line"> NSUInteger _keyMappedCount;</span><br><span class="line"> </span><br><span class="line"> /// Model class type.</span><br><span class="line"> YYEncodingNSType _nsType;</span><br><span class="line"> </span><br><span class="line"> /// 用户响应了 "new字典去替换origin字典" 协议方法;json-to-model将要转换;</span><br><span class="line"> BOOL _hasCustomWillTransformFromDictionary;</span><br><span class="line"> </span><br><span class="line"> /// 用户响应了 "new字典去替换origin字典" 协议方法;json-to-model已转换;</span><br><span class="line"> BOOL _hasCustomTransformFromDictionary;</span><br><span class="line"> </span><br><span class="line"> /// model-to-json额外协议方法:-modelCustomTransformToDictionary:</span><br><span class="line"> BOOL _hasCustomTransformToDictionary;</span><br><span class="line"> </span><br><span class="line"> /// json-to-model额外协议方法:+modelCustomClassForDictionary:</span><br><span class="line"> BOOL _hasCustomClassFromDictionary;</span><br><span class="line">}</span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<h5 id="YYModelPropertyMeta"><a href="#YYModelPropertyMeta" class="headerlink" title="_YYModelPropertyMeta"></a>_YYModelPropertyMeta</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">@interface _YYModelPropertyMeta : NSObject {</span><br><span class="line"> @package</span><br><span class="line"> </span><br><span class="line"> ///< 属性名</span><br><span class="line"> NSString *_name; </span><br><span class="line"> </span><br><span class="line"> ///< 属性type,包含三种类型:</span><br><span class="line"> // value(值类型void、UInt8、Block、CArray等)、 // qualifier(限定词,Const、Byref、Oneway等)、</span><br><span class="line"> // property(Readonly、Copy、Setter、Dynamic等)、 </span><br><span class="line"> YYEncodingType _type; </span><br><span class="line"> </span><br><span class="line"> ///< 属性Foundation type,比如NSMutableString、NSNumber等 </span><br><span class="line"> YYEncodingNSType _nsType; </span><br><span class="line"> </span><br><span class="line"> ///< 是否C Number类型 </span><br><span class="line"> BOOL _isCNumber; </span><br><span class="line"> </span><br><span class="line"> ///< 属性对应的Class </span><br><span class="line"> Class _cls; </span><br><span class="line"> </span><br><span class="line"> ///< 数组对应的泛型Class </span><br><span class="line"> Class _genericCls; </span><br><span class="line"> </span><br><span class="line"> SEL _getter; </span><br><span class="line"> SEL _setter;</span><br><span class="line"> </span><br><span class="line"> ///< 是否兼容KVC</span><br><span class="line"> BOOL _isKVCCompatible; </span><br><span class="line"> </span><br><span class="line"> ///< 结构体是否可以根据key来归解档 </span><br><span class="line"> BOOL _isStructAvailableForKeyedArchiver; </span><br><span class="line"> </span><br><span class="line"> ///< class/generic class implements +modelCustomClassForDictionary:</span><br><span class="line"> BOOL _hasCustomClassFromDictionary; </span><br><span class="line"> </span><br><span class="line"> /*</span><br><span class="line"> property->key: _mappedToKey:key _mappedToKeyPath:nil _mappedToKeyArray:nil</span><br><span class="line"> property->keyPath: _mappedToKey:keyPath _mappedToKeyPath:keyPath(array) _mappedToKeyArray:nil</span><br><span class="line"> property->keys: _mappedToKey:keys[0] _mappedToKeyPath:nil/keyPath _mappedToKeyArray:keys(array)</span><br><span class="line"> */</span><br><span class="line"> NSString *_mappedToKey; ///< the key mapped to</span><br><span class="line"> NSArray *_mappedToKeyPath; ///< the key path mapped to (nil if the name is not key path) </span><br><span class="line"> NSArray *_mappedToKeyArray; ///< the key(NSString) or keyPath(NSArray) array (nil if not mapped to multiple keys)</span><br><span class="line"> YYClassPropertyInfo *_info; ///< 属性信息</span><br><span class="line"> _YYModelPropertyMeta *_next; ///< 如果一个key对应多个属性的话,next</span><br><span class="line">}</span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<h5 id="再看两个函数:CFArrayApplyFunction-和-CFDictionaryApplyFunction"><a href="#再看两个函数:CFArrayApplyFunction-和-CFDictionaryApplyFunction" class="headerlink" title="再看两个函数:CFArrayApplyFunction() 和 CFDictionaryApplyFunction()"></a>再看两个函数:CFArrayApplyFunction() 和 CFDictionaryApplyFunction()</h5><h6 id="CF-CoreFoundation-相对NS-Foundation-遍历容器能提升性能"><a href="#CF-CoreFoundation-相对NS-Foundation-遍历容器能提升性能" class="headerlink" title="CF(CoreFoundation)相对NS(Foundation)遍历容器能提升性能"></a>CF(CoreFoundation)相对NS(Foundation)遍历容器能提升性能</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">//遍历字典,将参数context传给applier;需要重写applier方法,接收context;</span><br><span class="line">void CFDictionaryApplyFunction(CFDictionaryRef theDict, CFDictionaryApplierFunction CF_NOESCAPE applier, void *context);</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">//遍历数组,将参数context传给applier;需要重写applier方法,接收context;</span><br><span class="line">void CFArrayApplyFunction(CFArrayRef theArray, CFRange range, CFArrayApplierFunction CF_NOESCAPE applier, void *context);</span><br></pre></td></tr></table></figure>
<h5 id="主要实现方法:-1"><a href="#主要实现方法:-1" class="headerlink" title="主要实现方法:"></a>主要实现方法:</h5><h5 id="BOOL-modelSetWithDictionary-NSDictionary-dic-1"><a href="#BOOL-modelSetWithDictionary-NSDictionary-dic-1" class="headerlink" title="- (BOOL)modelSetWithDictionary:(NSDictionary *)dic;"></a>- (BOOL)modelSetWithDictionary:(NSDictionary *)dic;</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">//dictionary 转换 Model主要实现方法</span><br><span class="line">- (BOOL)modelSetWithDictionary:(NSDictionary *)dic {</span><br><span class="line"></span><br><span class="line"> //1.kCFNull是NSNull的单例,表示空值@{}</span><br><span class="line"> if (!dic || dic == (id)kCFNull) return NO;</span><br><span class="line"> if (![dic isKindOfClass:[NSDictionary class]]) return NO;</span><br><span class="line"> </span><br><span class="line"> //2.创建_YYModelMeta对象,封装并获取class信息(获取class的ivar、method、property等)</span><br><span class="line"> _YYModelMeta *modelMeta = [_YYModelMeta metaWithClass:object_getClass(self)];</span><br><span class="line"> if (modelMeta->_keyMappedCount == 0) return NO;//属性个数为0</span><br><span class="line"> </span><br><span class="line"> //这是YYModel协议中的内容,算是附加功能暂时先忽略</span><br><span class="line"> if (modelMeta->_hasCustomWillTransformFromDictionary) {//json-to-model转换之前</span><br><span class="line"> dic = [((id<YYModel>)self) modelCustomWillTransformFromDictionary:dic];</span><br><span class="line"> if (![dic isKindOfClass:[NSDictionary class]]) return NO;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> //3.创建结构体(class信息、model对象、dic)</span><br><span class="line"> ModelSetContext context = {0};</span><br><span class="line"> context.modelMeta = (__bridge void *)(modelMeta);</span><br><span class="line"> context.model = (__bridge void *)(self);</span><br><span class="line"> context.dictionary = (__bridge void *)(dic);</span><br><span class="line"> </span><br><span class="line"> //4.判断属性个数和字典数量关系</span><br><span class="line"> //通常情况是两者相等</span><br><span class="line"> //存在3种类型的key(keyPath、keys、key),分别处理</span><br><span class="line"> if (modelMeta->_keyMappedCount >= CFDictionaryGetCount((CFDictionaryRef)dic)) {</span><br><span class="line"> </span><br><span class="line"> //字典</span><br><span class="line"> CFDictionaryApplyFunction((CFDictionaryRef)dic, ModelSetWithDictionaryFunction, &context);</span><br><span class="line"> </span><br><span class="line"> // 是否存在映射keyPath属性元</span><br><span class="line"> if (modelMeta->_keyPathPropertyMetas) {</span><br><span class="line"> //数组(keyPath)</span><br><span class="line"> CFArrayApplyFunction((CFArrayRef)modelMeta->_keyPathPropertyMetas,</span><br><span class="line"> CFRangeMake(0, CFArrayGetCount((CFArrayRef)modelMeta->_keyPathPropertyMetas)),</span><br><span class="line"> ModelSetWithPropertyMetaArrayFunction,</span><br><span class="line"> &context);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> //是否存在一个key映射多个属性元</span><br><span class="line"> if (modelMeta->_multiKeysPropertyMetas) {</span><br><span class="line"> </span><br><span class="line"> //数组(keys)</span><br><span class="line"> CFArrayApplyFunction((CFArrayRef)modelMeta->_multiKeysPropertyMetas,</span><br><span class="line"> CFRangeMake(0, CFArrayGetCount((CFArrayRef)modelMeta->_multiKeysPropertyMetas)),</span><br><span class="line"> ModelSetWithPropertyMetaArrayFunction,</span><br><span class="line"> &context);</span><br><span class="line"> }</span><br><span class="line"> } else {</span><br><span class="line"> </span><br><span class="line"> //数组(key)</span><br><span class="line"> CFArrayApplyFunction((CFArrayRef)modelMeta->_allPropertyMetas,</span><br><span class="line"> CFRangeMake(0, modelMeta->_keyMappedCount),</span><br><span class="line"> ModelSetWithPropertyMetaArrayFunction,</span><br><span class="line"> &context);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> // 忽略</span><br><span class="line"> if (modelMeta->_hasCustomTransformFromDictionary) {</span><br><span class="line"> return [((id<YYModel>)self) modelCustomTransformFromDictionary:dic];</span><br><span class="line"> }</span><br><span class="line"> return YES;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h4 id="上述方法主要干了这么几件事:"><a href="#上述方法主要干了这么几件事:" class="headerlink" title="上述方法主要干了这么几件事:"></a>上述方法主要干了这么几件事:</h4><ul>
<li>验证参数(为了程序健壮性一定要这么做)</li>
<li>创建YYModelMeta对象</li>
<li>创建ModelSetContext结构体</li>
<li>将字典用ModelSetWithDictionaryFunction解析(遍历字典去setModel)</li>
<li>根据字典中存在keyPath、keys、key对应的value是数组的情况,都调用ModelSetWithPropertyMetaArrayFunction解析(遍历数组去setModel)</li>
</ul>
<h5 id="遍历数组"><a href="#遍历数组" class="headerlink" title="遍历数组"></a>遍历数组</h5><h6 id="ModelSetWithPropertyMetaArrayFunction"><a href="#ModelSetWithPropertyMetaArrayFunction" class="headerlink" title="ModelSetWithPropertyMetaArrayFunction"></a>ModelSetWithPropertyMetaArrayFunction</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">static void ModelSetWithPropertyMetaArrayFunction(const void *_propertyMeta, void *_context) {</span><br><span class="line"> ModelSetContext *context = _context;</span><br><span class="line"> __unsafe_unretained NSDictionary *dictionary = (__bridge NSDictionary *)(context->dictionary);</span><br><span class="line"> __unsafe_unretained _YYModelPropertyMeta *propertyMeta = (__bridge _YYModelPropertyMeta *)(_propertyMeta);</span><br><span class="line"> if (!propertyMeta->_setter) return;</span><br><span class="line"> id value = nil;</span><br><span class="line"> </span><br><span class="line"> if (propertyMeta->_mappedToKeyArray) {</span><br><span class="line"> value = YYValueForMultiKeys(dictionary, propertyMeta->_mappedToKeyArray);</span><br><span class="line"> } else if (propertyMeta->_mappedToKeyPath) {</span><br><span class="line"> value = YYValueForKeyPath(dictionary, propertyMeta->_mappedToKeyPath);</span><br><span class="line"> } else {</span><br><span class="line"> value = [dictionary objectForKey:propertyMeta->_mappedToKey];</span><br><span class="line"> }</span><br><span class="line"> //必须有value才实现value转property</span><br><span class="line"> if (value) {</span><br><span class="line"> __unsafe_unretained id model = (__bridge id)(context->model);</span><br><span class="line"> //最终每个属性对应的解析方法</span><br><span class="line"> ModelSetValueForProperty(model, value, propertyMeta);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h5 id="遍历字典"><a href="#遍历字典" class="headerlink" title="遍历字典"></a>遍历字典</h5><h6 id="ModelSetWithDictionaryFunction"><a href="#ModelSetWithDictionaryFunction" class="headerlink" title="ModelSetWithDictionaryFunction"></a>ModelSetWithDictionaryFunction</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">static void ModelSetWithDictionaryFunction(const void *_key, const void *_value, void *_context) {</span><br><span class="line"> ModelSetContext *context = _context;</span><br><span class="line"> __unsafe_unretained _YYModelMeta *meta = (__bridge _YYModelMeta *)(context->modelMeta);</span><br><span class="line"> __unsafe_unretained _YYModelPropertyMeta *propertyMeta = [meta->_mapper objectForKey:(__bridge id)(_key)];</span><br><span class="line"> __unsafe_unretained id model = (__bridge id)(context->model);</span><br><span class="line"> while (propertyMeta) {</span><br><span class="line"> </span><br><span class="line"> //必须有setter方法才实现value转property</span><br><span class="line"> if (propertyMeta->_setter) {</span><br><span class="line"> </span><br><span class="line"> //最终每个属性对应的解析方法</span><br><span class="line"> ModelSetValueForProperty(model, (__bridge __unsafe_unretained id)_value, propertyMeta);</span><br><span class="line"> }</span><br><span class="line"> propertyMeta = propertyMeta->_next;</span><br><span class="line"> };</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h5 id="最终的解析方法:遍历字典或数组中每一项属性"><a href="#最终的解析方法:遍历字典或数组中每一项属性" class="headerlink" title="最终的解析方法:遍历字典或数组中每一项属性"></a>最终的解析方法:遍历字典或数组中每一项属性</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">static void ModelSetValueForProperty(__unsafe_unretained id model,</span><br><span class="line"> __unsafe_unretained id value,</span><br><span class="line"> __unsafe_unretained _YYModelPropertyMeta *meta) {</span><br></pre></td></tr></table></figure>
<h6 id="为了看清实现逻辑,精简代码如下:"><a href="#为了看清实现逻辑,精简代码如下:" class="headerlink" title="为了看清实现逻辑,精简代码如下:"></a>为了看清实现逻辑,精简代码如下:</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">static void ModelSetValueForProperty(__unsafe_unretained id model,</span><br><span class="line"> __unsafe_unretained id value,</span><br><span class="line"> __unsafe_unretained _YYModelPropertyMeta *meta) {</span><br><span class="line"> if (meta->_isCNumber) {//是否C Number 类型</span><br><span class="line"> NSNumber *num = YYNSNumberCreateFromID(value);//将id类型转换成NSNumber类型</span><br><span class="line"> ModelSetNumberToProperty(model, num, meta);//值类型</span><br><span class="line"> if (num != nil) [num class]; // hold the number</span><br><span class="line"> } else if (meta->_nsType) {</span><br><span class="line"> if (value == (id)kCFNull) {</span><br><span class="line"> ((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, (id)nil);</span><br><span class="line"> } else {</span><br><span class="line"> switch (meta->_nsType) {</span><br><span class="line"> case YYEncodingTypeNSString:</span><br><span class="line"> case YYEncodingTypeNSMutableString: {</span><br><span class="line"> } break;</span><br><span class="line"> </span><br><span class="line"> case YYEncodingTypeNSValue:</span><br><span class="line"> case YYEncodingTypeNSNumber:</span><br><span class="line"> case YYEncodingTypeNSDecimalNumber: {</span><br><span class="line"> } break;</span><br><span class="line"> </span><br><span class="line"> case YYEncodingTypeNSData:</span><br><span class="line"> case YYEncodingTypeNSMutableData: {</span><br><span class="line"> } break;</span><br><span class="line"> </span><br><span class="line"> case YYEncodingTypeNSDate: {</span><br><span class="line"> } break;</span><br><span class="line"> </span><br><span class="line"> case YYEncodingTypeNSURL: {</span><br><span class="line"> } break;</span><br><span class="line"> </span><br><span class="line"> case YYEncodingTypeNSArray:</span><br><span class="line"> case YYEncodingTypeNSMutableArray: {</span><br><span class="line"> } break;</span><br><span class="line"> </span><br><span class="line"> case YYEncodingTypeNSDictionary:</span><br><span class="line"> case YYEncodingTypeNSMutableDictionary: {</span><br><span class="line"> } break;</span><br><span class="line"> </span><br><span class="line"> case YYEncodingTypeNSSet:</span><br><span class="line"> case YYEncodingTypeNSMutableSet: {</span><br><span class="line"> } // break; commented for code coverage in next line</span><br><span class="line"> </span><br><span class="line"> default: break;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> } else {</span><br><span class="line"> BOOL isNull = (value == (id)kCFNull);</span><br><span class="line"> switch (meta->_type & YYEncodingTypeMask) {</span><br><span class="line"> case YYEncodingTypeObject: {</span><br><span class="line"> } break;</span><br><span class="line"> </span><br><span class="line"> case YYEncodingTypeClass: {</span><br><span class="line"> } break;</span><br><span class="line"> </span><br><span class="line"> case YYEncodingTypeSEL: {</span><br><span class="line"> } break;</span><br><span class="line"> </span><br><span class="line"> case YYEncodingTypeBlock: {</span><br><span class="line"> } break;</span><br><span class="line"> </span><br><span class="line"> case YYEncodingTypeStruct:</span><br><span class="line"> case YYEncodingTypeUnion:</span><br><span class="line"> case YYEncodingTypeCArray: {</span><br><span class="line"> } break;</span><br><span class="line"> </span><br><span class="line"> case YYEncodingTypePointer:</span><br><span class="line"> case YYEncodingTypeCString: {</span><br><span class="line"> } // break; commented for code coverage in next line</span><br><span class="line"> </span><br><span class="line"> default: break;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h6 id="Number类型转成属性"><a href="#Number类型转成属性" class="headerlink" title="Number类型转成属性"></a>Number类型转成属性</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">static force_inline void ModelSetNumberToProperty(__unsafe_unretained id model,</span><br><span class="line"> __unsafe_unretained NSNumber *num,</span><br><span class="line"> __unsafe_unretained _YYModelPropertyMeta *meta) {</span><br><span class="line"> switch (meta->_type & YYEncodingTypeMask) {</span><br><span class="line"> case YYEncodingTypeBool: {</span><br><span class="line"> </span><br><span class="line"> //最终通过objc_msgSend发送消息的形式,实现model的该属性的setter方法</span><br><span class="line"> ((void (*)(id, SEL, bool))(void *) objc_msgSend)((id)model, meta->_setter, num.boolValue);</span><br><span class="line"> } break;</span><br><span class="line"> case ...//基本雷同,看上面一行代码就行</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h5 id="最终通过objc-msgSend发送消息的形式,实现model的该属性的setter方法"><a href="#最终通过objc-msgSend发送消息的形式,实现model的该属性的setter方法" class="headerlink" title="最终通过objc_msgSend发送消息的形式,实现model的该属性的setter方法"></a>最终通过objc_msgSend发送消息的形式,实现model的该属性的setter方法</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">((void (*)(id, SEL, bool))(void *) objc_msgSend)((id)model, meta->_setter, num.boolValue);</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">/** objc_msgSend</span><br><span class="line"> 定义:'为某个类对象发送消息,并且返回一个值' </span><br><span class="line"> 参数1: 消息接收的对象实例 </span><br><span class="line"> 参数2: 要执行的方法 </span><br><span class="line"> ...: 一系列其他参数 */</span><br><span class="line">id objc_msgSend(id self, SEL op, ...)</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">//实现的伪代码...</span><br><span class="line">id objc_msgSend(id self, SEL op, ...) {</span><br><span class="line"> if (!self) return nil;</span><br><span class="line"> //关键代码</span><br><span class="line"> IMP imp = class_getMethodImplementation(self->isa, SEL op);</span><br><span class="line"> imp(self, op, ...); </span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h5 id="以上大概就是json-to-model整个过程"><a href="#以上大概就是json-to-model整个过程" class="headerlink" title="以上大概就是json-to-model整个过程"></a>以上大概就是json-to-model整个过程</h5><h3 id="三、model-to-json过程解析"><a href="#三、model-to-json过程解析" class="headerlink" title="三、model-to-json过程解析"></a>三、model-to-json过程解析</h3><h6 id="最后解析的json要满足苹果规定:"><a href="#最后解析的json要满足苹果规定:" class="headerlink" title="最后解析的json要满足苹果规定:"></a>最后解析的json要满足苹果规定:</h6><ul>
<li>最后解析的json一定是字典或数组</li>
<li>json内每一项都应该是NSString、NSNumber、NSArray、NSDictionary、NSNull的一种</li>
<li>所有字典的key都应该是NSString</li>
<li>值类型不能是非数值的或无穷的</li>
</ul>
<h5 id="id-modelToJSONObject"><a href="#id-modelToJSONObject" class="headerlink" title="- (id)modelToJSONObject;"></a>- (id)modelToJSONObject;</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">- (id)modelToJSONObject {</span><br><span class="line"> /*</span><br><span class="line"> Apple said:</span><br><span class="line"> The top level object is an NSArray or NSDictionary.</span><br><span class="line"> All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.</span><br><span class="line"> All dictionary keys are instances of NSString.</span><br><span class="line"> Numbers are not NaN or infinity.</span><br><span class="line"> */</span><br><span class="line"> // 最终都是调用递归转换方法</span><br><span class="line"> id jsonObject = ModelToJSONObjectRecursive(self);</span><br><span class="line"> if ([jsonObject isKindOfClass:[NSArray class]]) return jsonObject;</span><br><span class="line"> if ([jsonObject isKindOfClass:[NSDictionary class]]) return jsonObject;</span><br><span class="line"> return nil;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h6 id="static-id-ModelToJSONObjectRecursive-NSObject-model"><a href="#static-id-ModelToJSONObjectRecursive-NSObject-model" class="headerlink" title="static id ModelToJSONObjectRecursive(NSObject *model);"></a>static id ModelToJSONObjectRecursive(NSObject *model);</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br></pre></td><td class="code"><pre><span class="line">static id ModelToJSONObjectRecursive(NSObject *model) {</span><br><span class="line"></span><br><span class="line"> //model为nil、空值、string、number直接返回,不用再往下递归解析</span><br><span class="line"> if (!model || model == (id)kCFNull) return model;</span><br><span class="line"> if ([model isKindOfClass:[NSString class]]) return model;</span><br><span class="line"> if ([model isKindOfClass:[NSNumber class]]) return model;</span><br><span class="line"> </span><br><span class="line"> //字典需要遍历每一项<key,value>,同时创建一个新字典,key必须string;</span><br><span class="line"> //value若为nil、空值、string、number则不必再往下解析,直接赋值给新字典;</span><br><span class="line"> //value若为字典、数组等,则继续往下递归解析,最终的值也是赋值给新字典;</span><br><span class="line"> if ([model isKindOfClass:[NSDictionary class]]) {</span><br><span class="line"> if ([NSJSONSerialization isValidJSONObject:model]) return model;</span><br><span class="line"> NSMutableDictionary *newDic = [NSMutableDictionary new];</span><br><span class="line"> [((NSDictionary *)model) enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {</span><br><span class="line"> NSString *stringKey = [key isKindOfClass:[NSString class]] ? key : key.description;</span><br><span class="line"> if (!stringKey) return;</span><br><span class="line"> id jsonObj = ModelToJSONObjectRecursive(obj);</span><br><span class="line"> if (!jsonObj) jsonObj = (id)kCFNull;</span><br><span class="line"> newDic[stringKey] = jsonObj;</span><br><span class="line"> }];</span><br><span class="line"> return newDic;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> //NSSet</span><br><span class="line"> if ([model isKindOfClass:[NSSet class]]) {</span><br><span class="line"> NSArray *array = ((NSSet *)model).allObjects;</span><br><span class="line"> if ([NSJSONSerialization isValidJSONObject:array]) return array;</span><br><span class="line"> NSMutableArray *newArray = [NSMutableArray new];</span><br><span class="line"> for (id obj in array) {</span><br><span class="line"> if ([obj isKindOfClass:[NSString class]] || [obj isKindOfClass:[NSNumber class]]) {</span><br><span class="line"> [newArray addObject:obj];</span><br><span class="line"> } else {</span><br><span class="line"> id jsonObj = ModelToJSONObjectRecursive(obj);</span><br><span class="line"> if (jsonObj && jsonObj != (id)kCFNull) [newArray addObject:jsonObj];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> return newArray;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> //NSArray</span><br><span class="line"> if ([model isKindOfClass:[NSArray class]]) {</span><br><span class="line"> if ([NSJSONSerialization isValidJSONObject:model]) return model;</span><br><span class="line"> NSMutableArray *newArray = [NSMutableArray new];</span><br><span class="line"> for (id obj in (NSArray *)model) {</span><br><span class="line"> if ([obj isKindOfClass:[NSString class]] || [obj isKindOfClass:[NSNumber class]]) {</span><br><span class="line"> [newArray addObject:obj];</span><br><span class="line"> } else {</span><br><span class="line"> id jsonObj = ModelToJSONObjectRecursive(obj);</span><br><span class="line"> if (jsonObj && jsonObj != (id)kCFNull) [newArray addObject:jsonObj];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> return newArray;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> //比较特殊的四种类型:NSURL、NSAttributedString、NSDate、NSData</span><br><span class="line"> //NSURL返回完整的url字符串</span><br><span class="line"> if ([model isKindOfClass:[NSURL class]]) return ((NSURL *)model).absoluteString;</span><br><span class="line"> if ([model isKindOfClass:[NSAttributedString class]]) return ((NSAttributedString *)model).string;</span><br><span class="line"> if ([model isKindOfClass:[NSDate class]]) return [YYISODateFormatter() stringFromDate:(id)model];</span><br><span class="line"> if ([model isKindOfClass:[NSData class]]) return nil;</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> //如果是model是类创建的对象,则需要将model的属性(key、keyPath、keys)方式解析</span><br><span class="line"> _YYModelMeta *modelMeta = [_YYModelMeta metaWithClass:[model class]];</span><br><span class="line"> </span><br><span class="line"> //属性个数为0</span><br><span class="line"> if (!modelMeta || modelMeta->_keyMappedCount == 0) return nil;</span><br><span class="line"> </span><br><span class="line"> //将该model转化成字典</span><br><span class="line"> NSMutableDictionary *result = [[NSMutableDictionary alloc] initWithCapacity:64];</span><br><span class="line"> </span><br><span class="line"> //__unsafe_unretained 的好处就是 编译器不用去管理内存,提高性能</span><br><span class="line"> __unsafe_unretained NSMutableDictionary *dic = result; // avoid retain and release in block</span><br><span class="line"> </span><br><span class="line"> //按照属性key、keys、keyPath遍历</span><br><span class="line"> [modelMeta->_mapper enumerateKeysAndObjectsUsingBlock:^(NSString *propertyMappedKey, _YYModelPropertyMeta *propertyMeta, BOOL *stop) {</span><br><span class="line"> </span><br><span class="line"> //最终是通过objc_sendMsg调用getter获取value</span><br><span class="line"> if (!propertyMeta->_getter) return;</span><br><span class="line"> </span><br><span class="line"> id value = nil;</span><br><span class="line"> if (propertyMeta->_isCNumber) {</span><br><span class="line"> </span><br><span class="line"> //C Number 的方式getter Value</span><br><span class="line"> value = ModelCreateNumberFromProperty(model, propertyMeta);</span><br><span class="line"> </span><br><span class="line"> //NS类型:NSString、NSArray等</span><br><span class="line"> } else if (propertyMeta->_nsType) {</span><br><span class="line"> </span><br><span class="line"> //最底层的objc_msgSend调用,取出的值继续递归解析,其他情况雷同,不一一看了</span><br><span class="line"> id v = ((id (*)(id, SEL))(void *) objc_msgSend)((id)model, propertyMeta->_getter);</span><br><span class="line"> value = ModelToJSONObjectRecursive(v);</span><br><span class="line"> } else {</span><br><span class="line"> switch (propertyMeta->_type & YYEncodingTypeMask) {</span><br><span class="line"> case YYEncodingTypeObject: {</span><br><span class="line"> id v = ((id (*)(id, SEL))(void *) objc_msgSend)((id)model, propertyMeta->_getter);</span><br><span class="line"> value = ModelToJSONObjectRecursive(v);</span><br><span class="line"> if (value == (id)kCFNull) value = nil;</span><br><span class="line"> } break;</span><br><span class="line"> case YYEncodingTypeClass: {</span><br><span class="line"> Class v = ((Class (*)(id, SEL))(void *) objc_msgSend)((id)model, propertyMeta->_getter);</span><br><span class="line"> value = v ? NSStringFromClass(v) : nil;</span><br><span class="line"> } break;</span><br><span class="line"> case YYEncodingTypeSEL: {</span><br><span class="line"> SEL v = ((SEL (*)(id, SEL))(void *) objc_msgSend)((id)model, propertyMeta->_getter);</span><br><span class="line"> value = v ? NSStringFromSelector(v) : nil;</span><br><span class="line"> } break;</span><br><span class="line"> default: break;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> if (!value) return;</span><br><span class="line"> </span><br><span class="line"> if (propertyMeta->_mappedToKeyPath) {</span><br><span class="line"> NSMutableDictionary *superDic = dic;</span><br><span class="line"> NSMutableDictionary *subDic = nil;</span><br><span class="line"> for (NSUInteger i = 0, max = propertyMeta->_mappedToKeyPath.count; i < max; i++) {</span><br><span class="line"> NSString *key = propertyMeta->_mappedToKeyPath[i];</span><br><span class="line"> if (i + 1 == max) { // end</span><br><span class="line"> if (!superDic[key]) superDic[key] = value;</span><br><span class="line"> break;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> subDic = superDic[key];</span><br><span class="line"> if (subDic) {</span><br><span class="line"> if ([subDic isKindOfClass:[NSDictionary class]]) {</span><br><span class="line"> subDic = subDic.mutableCopy;</span><br><span class="line"> superDic[key] = subDic;</span><br><span class="line"> } else {</span><br><span class="line"> break;</span><br><span class="line"> }</span><br><span class="line"> } else {</span><br><span class="line"> subDic = [NSMutableDictionary new];</span><br><span class="line"> superDic[key] = subDic;</span><br><span class="line"> }</span><br><span class="line"> superDic = subDic;</span><br><span class="line"> subDic = nil;</span><br><span class="line"> }</span><br><span class="line"> } else {</span><br><span class="line"> if (!dic[propertyMeta->_mappedToKey]) {</span><br><span class="line"> dic[propertyMeta->_mappedToKey] = value;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }];</span><br><span class="line"> </span><br><span class="line"> if (modelMeta->_hasCustomTransformToDictionary) {</span><br><span class="line"> BOOL suc = [((id<YYModel>)model) modelCustomTransformToDictionary:dic];</span><br><span class="line"> if (!suc) return nil;</span><br><span class="line"> }</span><br><span class="line"> return result;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h5 id="以上大概就是model-to-json的过程"><a href="#以上大概就是model-to-json的过程" class="headerlink" title="以上大概就是model-to-json的过程"></a>以上大概就是model-to-json的过程</h5><p>另外,还有model-to-data、model-to-string等方法,都是先调用model-to-json,然后再利用系统的NSJSONSerialization、initWithData:encoding:进行转换</p>
<p>####### modelToJSONData<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">//先model-to-json,再json-to-data</span><br><span class="line">- (NSData *)modelToJSONData {</span><br><span class="line"> id jsonObject = [self modelToJSONObject];</span><br><span class="line"> if (!jsonObject) return nil;</span><br><span class="line"> return [NSJSONSerialization dataWithJSONObject:jsonObject options:0 error:NULL];</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>####### modelToJSONString<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">//先model-to-data,再data-to-string</span><br><span class="line">- (NSString *)modelToJSONString {</span><br><span class="line"> NSData *jsonData = [self modelToJSONData];</span><br><span class="line"> if (jsonData.length == 0) return nil;</span><br><span class="line"> return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h5 id="以上,解析json-to-model、model-to-json的转换过程解读完毕"><a href="#以上,解析json-to-model、model-to-json的转换过程解读完毕" class="headerlink" title="以上,解析json-to-model、model-to-json的转换过程解读完毕"></a>以上,解析json-to-model、model-to-json的转换过程解读完毕</h5><h3 id="四、一些宏的使用"><a href="#四、一些宏的使用" class="headerlink" title="四、一些宏的使用"></a>四、一些宏的使用</h3><p>1、NS_ASSUME_NONNULL_BEGIN、NS_ASSUME_NONNULL_END<br>我们可以给每个属性或每个方法都去指定 <strong>nullable 和 _</strong>nonnull,当一个类中,有的方法或属性给它指定了 <strong>nullable 和 _</strong>nonnull,其他方法又没有指定,编译器会爆警告。这时候可以全局使用NS_ASSUME_NONNULL_BEGIN、NS_ASSUME_NONNULL_END,然后再单独指定你要指定为__nullable的属性和方法。</p>
<p>2、#define force_inline <strong>inline</strong> <strong>attribute</strong>((always_inline))<br><strong>attribute</strong>((always_inline))的意思是强制内联,<br>所有加了<strong>attribute</strong>((always_inline))的函数再被调用时不会被编译成函数调用而是直接扩展到调用函数体内</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">force_inline void a() {</span><br><span class="line"> print("hello");</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">void b(){</span><br><span class="line"> a();</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>b调用a函数的汇编代码不会是跳转到a执行,而是a函数的代码直接在b内成为b的一部分。<br>用force_inline代替 <strong>inline</strong> <strong>attribute</strong>((always_inline)),是因为比较方便。</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
</section>
<nav class="pagination">
<span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="page-number" href="/page/3/">3</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right"></i></a>
</nav>
</div>
</div>
<div class="sidebar-toggle">
<div class="sidebar-toggle-line-wrap">
<span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
</div>
</div>
<aside id="sidebar" class="sidebar">
<div class="sidebar-inner">
<section class="site-overview-wrap sidebar-panel sidebar-panel-active">
<div class="site-overview">
<div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image"
src="/images/jun.jpeg"
alt="Jungle" />
<p class="site-author-name" itemprop="name">Jungle</p>
<p class="site-description motion-element" itemprop="description"></p>
</div>
<nav class="site-state motion-element">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">3</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
</nav>
</div>
</section>
</div>
</aside>
</div>
</main>
<footer id="footer" class="footer">
<div class="footer-inner">
<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">© <span itemprop="copyrightYear">2018</span>
<span class="with-love">
<i class="fa fa-user"></i>
</span>
<span class="author" itemprop="copyrightHolder">Jungle</span>
</div>
<div class="powered-by"><a class="theme-link" target="_blank" href="https://github.com/JumpX/JumpX.github.io">我们</a></div>
<span class="post-meta-divider">|</span>
<div class="theme-info"><a class="theme-link" target="_blank" href="https://github.com/JumpX/JumpX.github.io">github</a></div>
<span class="post-meta-divider">|</span>
<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
本站访客数:55</span>
</span>
</div>
<!--
<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
本站访客数:<span id="busuanzi_value_site_uv"></span>
</span>
</div>
-->
</div>
</footer>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
</div>
</div>
<script type="text/javascript">
if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
window.Promise = null;
}
</script>
<script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
<script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
<script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>
</body>
</html>
<script type="text/javascript" scr="/js/src/love.js"></script>