-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
3302 lines (1491 loc) · 145 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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
<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=2">
<meta name="theme-color" content="#222">
<link rel="stylesheet" href="/lib/fancybox/source/jquery.fancybox.css">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">
<link rel="stylesheet" href="/css/main.css?v=7.1.1">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.1">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.1.1">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.1.1">
<link rel="mask-icon" href="/images/logo.svg?v=7.1.1" color="#222">
<script id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Gemini',
version: '7.1.1',
sidebar: {"position":"right","width":300,"display":"post","offset":12,"onmobile":false,"dimmer":false},
back2top: true,
back2top_sidebar: false,
fancybox: true,
fastclick: false,
lazyload: false,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
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>
<meta name="description" content="平时学习的小笔记。">
<meta name="keywords" content="Object-C 、Swift">
<meta property="og:type" content="website">
<meta property="og:title" content="钟晴">
<meta property="og:url" content="http://zhongqing05.top/index.html">
<meta property="og:site_name" content="钟晴">
<meta property="og:description" content="平时学习的小笔记。">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="钟晴">
<meta name="twitter:description" content="平时学习的小笔记。">
<link rel="canonical" href="http://zhongqing05.top/">
<script id="page.configurations">
CONFIG.page = {
sidebar: "",
};
</script>
<title>钟晴</title>
<script async src="//www.googletagmanager.com/gtag/js?id=UA-142699670-1"></script>
<script>
var host = window.location.hostname;
if (host !== "localhost" || !true) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-142699670-1');
}
</script>
<noscript>
<style>
.use-motion .motion-element,
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-title { opacity: initial; }
.use-motion .logo,
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container sidebar-position-right
page-home">
<div class="headband"></div>
<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">钟晴</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle">当你的实力撑不起你的梦想,就更应该脚踏实地!</p>
</div>
<div class="site-nav-toggle">
<button aria-label="Toggle navigation bar">
<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 menu-item-active">
<a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>Home</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>Tags</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>Categories</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>Archives</a>
</li>
<li class="menu-item menu-item-读书">
<a href="/books" rel="section"><i class="menu-item-icon fa fa-fw fa-book"></i> <br>读书</a>
</li>
<li class="menu-item menu-item-电影">
<a href="/movies" rel="section"><i class="menu-item-icon fa fa-fw fa-film"></i> <br>电影</a>
</li>
<li class="menu-item menu-item-search">
<a href="javascript:;" class="popup-trigger">
<i class="menu-item-icon fa fa-search fa-fw"></i> <br>Search</a>
</li>
</ul>
<div class="site-search">
<div class="popup search-popup local-search-popup">
<div class="local-search-header clearfix">
<span class="search-icon">
<i class="fa fa-search"></i>
</span>
<span class="popup-btn-close">
<i class="fa fa-times-circle"></i>
</span>
<div class="local-search-input-wrapper">
<input autocomplete="off" placeholder="Searching..." spellcheck="false" type="text" id="local-search-input">
</div>
</div>
<div id="local-search-result"></div>
</div>
</div>
</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://zhongqing05.top/2020/05/15/iOS代码规范/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="ZQing">
<meta itemprop="description" content="平时学习的小笔记。">
<meta itemprop="image" content="/images/avatar.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="钟晴">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2020/05/15/iOS代码规范/" class="post-title-link" itemprop="url">iOS代码规范</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">Posted on</span>
<time title="Created: 2020-05-15 13:46:07 / Modified: 13:47:35" itemprop="dateCreated datePublished" datetime="2020-05-15T13:46:07+08:00">2020-05-15</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Objective-C/" itemprop="url" rel="index"><span itemprop="name">Objective-C</span></a></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h4 id="编程核心原则:"><a href="#编程核心原则:" class="headerlink" title="编程核心原则:"></a>编程核心原则:</h4><p><strong>原则一:代码应该简洁易懂,逻辑清晰</strong></p>
<p>因为软件是需要人来维护的。这个人在未来很可能不是你。所以首先是为人编写程序,其次才是计算机:</p>
<ul>
<li>不要过分追求技巧,降低程序的可读性。</li>
<li>简洁的代码可以让bug无处藏身 </li>
</ul>
<p><strong>原则二:面向变化编程,而不是面向需求编程。</strong></p>
<p>需求是暂时的,只有变化才是永恒的。 本次迭代不能仅仅为了当前的需求,写出扩展性强,易修改的程序才是负责任的做法,对自己负责,对公司负责。</p>
<p><strong>原则三:先保证程序的正确性,防止过度工程</strong></p>
<p>过度工程(over-engineering):在正确可用的代码写出之前就过度地考虑扩展,重用的问题,使得工程过度复杂。</p>
<blockquote>
<ol>
<li>先把眼前的问题解决掉,解决好,再考虑将来的扩展问题。</li>
<li>先写出可用的代码,反复推敲,再考虑是否需要重用的问题。</li>
<li>先写出可用,简单,明显没有bug的代码,再考虑测试的问题。</li>
</ol>
</blockquote>
<h4 id="Objective-C-编程规范"><a href="#Objective-C-编程规范" class="headerlink" title="Objective-C 编程规范"></a>Objective-C 编程规范</h4><p><strong>if 语句不要使用过多分支,分支比较多的情况下要善于使用return来提前返回</strong></p>
<p>推荐:</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></pre></td><td class="code"><pre><span class="line">- (void)someMethod { </span><br><span class="line"> if (!goodCondition) {</span><br><span class="line"> return;</span><br><span class="line"> }</span><br><span class="line"> //Do something</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>不推荐这样写:</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></pre></td><td class="code"><pre><span class="line">- (void)someMethod { </span><br><span class="line"> if (goodCondition) {</span><br><span class="line"> //Do something</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>举个典型的例子</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><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></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">-(id)initWithDictionary:(NSDictionary*)dict error:(NSError)err{</span><br><span class="line"> //方法1. 参数为nil</span><br><span class="line"> if (!dict) {</span><br><span class="line"> if (err) *err = [JSONModelError errorInputIsNil];</span><br><span class="line"> return nil;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> //方法2. 参数不是nil,但也不是字典</span><br><span class="line"> if (![dict isKindOfClass:[NSDictionary class]]) {</span><br><span class="line"> if (err) *err = [JSONModelError errorInvalidDataWithMessage:@"Attempt to initialize JSONModel object using initWithDictionary:error: but the dictionary parameter was not an 'NSDictionary'."];</span><br><span class="line"> return nil;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> //方法3. 初始化</span><br><span class="line"> self = [self init];</span><br><span class="line"> if (!self) {</span><br><span class="line"> //初始化失败</span><br><span class="line"> if (err) *err = [JSONModelError errorModelIsInvalid];</span><br><span class="line"> return nil;</span><br><span class="line"> }</span><br><span class="line"> //方法4. 检查用户定义的模型里的属性集合是否大于传入的字典里的key集合(如果大于,则返回NO)</span><br><span class="line"> if (![self __doesDictionary:dict matchModelWithKeyMapper:self.__keyMapper error:err]) {</span><br><span class="line"> return nil;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> //方法5. 核心方法:字典的key与模型的属性的映射</span><br><span class="line"> if (![self __importDictionary:dict withKeyMapper:self.__keyMapper validation:YES error:err]) {</span><br><span class="line"> return nil;</span><br><span class="line"> }</span><br><span class="line"> //方法6. 可以重写[self validate:err]方法并返回NO,让用户自定义错误并阻拦model的返回</span><br><span class="line"> if (![self validate:err]) {</span><br><span class="line"> return nil;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> //方法7. 终于通过了!成功返回model</span><br><span class="line"> return self;</span><br></pre></td></tr></table></figure>
<p><strong>条件表达式如果很长(超过2个表达式),则需要将他们提取出来赋给一个BOOL值</strong></p>
<p>推荐</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><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">Bool isContains = sessionName.hasPrefix("Swift")</span><br><span class="line">Bool isCurrentYear = sessionDateCompontents.year == 2014</span><br><span class="line"></span><br><span class="line">let isSwiftSession = nameContainsSwift && isCurrentYear && user.isLogin</span><br><span class="line">if (isSwiftSession) { </span><br><span class="line"> //Do something</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>不推荐</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></pre></td><td class="code"><pre><span class="line">if (sessionName.hasPrefix("Swift") && sessionDateCompontents.year == 2014 && user.isLogin ){</span><br><span class="line"> //Do something</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>一个函数的长度必须限制在50行以内</strong></p>
<p>通常来说,在阅读一个函数的时候,如果视需要跨过很长的垂直距离会非常影响代码的阅读体验。如果需要来回滚动眼球或代码才能看全一个方法,就会很影响思维的连贯性,对阅读代码的速度造成比较大的影响。最好的情况是在不滚动眼球或代码的情况下一眼就能将该方法的全部代码映入眼帘。</p>
<p><strong>一个函数只做一件事(单一原则)</strong></p>
<p>每个函数的职责都应该划分的很明确(就像类一样)。</p>
<p>推荐:</p>
<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">dataConfiguration()</span><br><span class="line">viewConfiguration()</span><br></pre></td></tr></table></figure>
<p>不推荐:</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></pre></td><td class="code"><pre><span class="line">void dataConfiguration()</span><br><span class="line">{ </span><br><span class="line"> ...</span><br><span class="line"> viewConfiguration()</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>函数命名要清晰</strong></p>
<p>命名应该尽可能的清晰和简洁,但在 Objective-C 中,清晰比简洁更重要。由于 Xcode 强大的自动补全功能,我们不必担心名称过长的问题</p>
<p>推荐</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></pre></td><td class="code"><pre><span class="line">- (void) insertObject:(UInteger)atIndex;</span><br><span class="line"></span><br><span class="line">- (void) removeObjectAtIndex:(UInteger)atIndex;</span><br></pre></td></tr></table></figure>
<p>不推荐</p>
<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">- (void) insert:(int)at;</span><br><span class="line">- (void) remove:(int)at;</span><br></pre></td></tr></table></figure>
<p><strong>在 init 和 dealloc 中不要用存取方法访问实例变量</strong><br>当 <code>init</code>、<code>dealloc</code> 方法被执行时,类的运行时环境不是处于正常状态的,使用存取方法访问变量可能会导致不可预料的结果,因此应当在这两个方法内直接访问实例变量。</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><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></pre></td><td class="code"><pre><span class="line">//正确,直接访问实例变量</span><br><span class="line">- (instancetype)init {</span><br><span class="line"> self = [super init];</span><br><span class="line"> if (self) {</span><br><span class="line"> _bar = [[NSMutableString alloc] init];</span><br><span class="line"> }</span><br><span class="line"> return self;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">- (void)dealloc {</span><br><span class="line"> [_bar release];</span><br><span class="line"> [super dealloc];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">// 错误,不要通过存取方法访问</span><br><span class="line">- (instancetype)init {</span><br><span class="line"> self = [super init];</span><br><span class="line"> if (self) {</span><br><span class="line"> self.bar = [NSMutableString string];</span><br><span class="line"> }</span><br><span class="line"> return self;</span><br><span class="line">}</span><br><span class="line">- (void)dealloc {</span><br><span class="line"> self.bar = nil;</span><br><span class="line"> [super dealloc];</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>不要用点分语法来调用方法,只用来访问属性。这样是为了防止代码可读性问题。</strong></p>
<p>推荐</p>
<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">view.backgroundColor = [UIColor orangeColor];</span><br></pre></td></tr></table></figure>
<p>不推荐</p>
<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">[view setBackgroundColor:[UIColor orangeColor]];</span><br></pre></td></tr></table></figure>
<p><strong>注释</strong></p>
<ol>
<li>公共接口(注释要告诉阅读代码的人,当前类能实现什么功能)。</li>
<li>涉及到比较深层专业知识的代码(注释要体现出实现原理和思想)。 </li>
<li>容易产生歧义的代码</li>
</ol>
<p>设计以上三种情况下。都必须写好注释。在xcode中自带快捷注释方法。选好要注释的方法或类。<code>option + command + /</code></p>
<p><strong>系统常用类作实例变量声明时加入后缀</strong></p>
<table>
<thead>
<tr>
<th align="left">类型</th>
<th align="right">后缀</th>
</tr>
</thead>
<tbody><tr>
<td align="left">UIView</td>
<td align="right">View</td>
</tr>
<tr>
<td align="left">UILabel</td>
<td align="right">Lbl</td>
</tr>
<tr>
<td align="left">UIButton</td>
<td align="right">Btn</td>
</tr>
<tr>
<td align="left">UIImage</td>
<td align="right">Img</td>
</tr>
<tr>
<td align="left">UIImageView</td>
<td align="right">ImgView</td>
</tr>
<tr>
<td align="left">NSArray</td>
<td align="right">Ary</td>
</tr>
<tr>
<td align="left">NSMutableArray</td>
<td align="right">Mary</td>
</tr>
<tr>
<td align="left">NSDictionary</td>
<td align="right">Dict</td>
</tr>
<tr>
<td align="left">NSMutableDictionary</td>
<td align="right">Mdict</td>
</tr>
<tr>
<td align="left">NSString</td>
<td align="right">Str</td>
</tr>
<tr>
<td align="left">NSMutableString</td>
<td align="right">Mstr</td>
</tr>
<tr>
<td align="left">NSSet</td>
<td align="right">Set</td>
</tr>
<tr>
<td align="left">NSMutableSet</td>
<td align="right">Mset</td>
</tr>
</tbody></table>
<p><strong>CGRect函数</strong></p>
<p>其实iOS内部已经提供了相应的获取CGRect各个部分的函数了,它们的可读性比较高,而且简短,推荐使用</p>
<p>推荐</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></pre></td><td class="code"><pre><span class="line">CGRect frame = self.view.frame; </span><br><span class="line">CGFloat x = CGRectGetMinX(frame); </span><br><span class="line">CGFloat y = CGRectGetMinY(frame); </span><br><span class="line">CGFloat width = CGRectGetWidth(frame); </span><br><span class="line">CGFloat height = CGRectGetHeight(frame); </span><br><span class="line">CGRect frame = CGRectMake(0.0, 0.0, width, height);</span><br></pre></td></tr></table></figure>
<p>不推荐</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></pre></td><td class="code"><pre><span class="line">CGRect frame = self.view.frame; </span><br><span class="line">CGFloat x = frame.origin.x; </span><br><span class="line">CGFloat y = frame.origin.y; </span><br><span class="line">CGFloat width = frame.size.width; </span><br><span class="line">CGFloat height = frame.size.height; </span><br><span class="line">CGRect frame = (CGRect){ .origin = CGPointZero, .size = frame.size };</span><br></pre></td></tr></table></figure>
<p><strong>范型</strong></p>
<p>如果容器元素唯一时,建议在定义NSArray、NSDictionary、NSSet,指定容器的元素</p>
<p>推荐</p>
<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">@property (nonatimic, readwrite, copy) NSArray <DSRUser*> *userAry;</span><br><span class="line">@property (nonatimic, readwrite, copy) NSDictionary <NSString *,DSRUser*> *userDict</span><br></pre></td></tr></table></figure>
<p>不推荐</p>
<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">@property (nonatimic, readwrite, copy) NSArray *userAry;</span><br><span class="line">@property (nonatimic, readwrite, copy) NSDictionary * userDict</span><br></pre></td></tr></table></figure>
<p><strong>尽量使用字面量值来创建 NSString , NSDictionary , NSArray , NSNumber 这些不可变对象:</strong></p>
<p>推荐</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></pre></td><td class="code"><pre><span class="line">NSArray *nameAry = @[@"zhangsan", @"lishi", @"wangwu"];</span><br><span class="line">NSDictionary *productDict = @{@"iPhone" : @"Kate",</span><br><span class="line"> @"iPad" : @"Kamal",</span><br><span class="line"> @"Mobile Web" : @"Bill"};</span><br></pre></td></tr></table></figure>
<p>不推荐</p>
<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">NSArray * nameAry = [NSArray arrayWithObjects:@"zhangsan", @"lishi", @"wangwu", nil];</span><br><span class="line">NSDictionary *productManagers = [NSDictionary dictionaryWithObjectsAndKeys:@"Kate", @"iPhone", @"Kamal", @"iPad", @"Bill"];</span><br></pre></td></tr></table></figure>
<p><strong>属性的命名使用小驼峰</strong></p>
<p>推荐:</p>
<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, readwrite, copy) NSString * tipStr;</span><br></pre></td></tr></table></figure>
<p><strong>属性的关键字推荐按照 原子性,读写,内存管理的顺序排列</strong><br>推荐:</p>
<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">@property (nonatomic, readwrite, copy) NSString *nameStr;</span><br><span class="line">@property (nonatomic, readonly, assign) NSUInterger old;</span><br></pre></td></tr></table></figure>
<p><strong>Block属性应该使用copy关键字</strong></p>
<p>推荐:</p>
<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">typedef void (^ErrorCodeBlock) (id errorCode,NSString *message);</span><br><span class="line">@property (nonatomic, readwrite, copy) ErrorCodeBlock errorBlock;</span><br></pre></td></tr></table></figure>
<p><strong>形容词性的BOOL属性的getter应该加上is前缀</strong></p>
<p>推荐</p>
<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, getter=isEditable, assign) BOOL editable;</span><br></pre></td></tr></table></figure>
<p><strong>建议尽量把对外公布出来的属性设置为只读,在实现文件内部设为读写,避免多处地方修改属性值,方便管理代码</strong></p>
<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">在头文件中,设置对象属性为 `readonly`</span><br><span class="line">在实现文件中设置为 `readwrite`</span><br></pre></td></tr></table></figure>
<p><strong>方法名中不应使用and</strong></p>
<p>推荐</p>
<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">- (instancetype)initWithWidth:(CGFloat)width height:(CGFloat)height;</span><br></pre></td></tr></table></figure>
<p>不推荐</p>
<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">- (instancetype)initWithWidth:(CGFloat)width andHeight:(CGFloat)height;</span><br></pre></td></tr></table></figure>
<p><strong>方法实现时,如果参数过长(参数超过3个),则令每个参数占用一行,以冒号对齐</strong></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></pre></td><td class="code"><pre><span class="line">- (void)doSomethingWith:(NSString *)theFoo</span><br><span class="line"> rect:(CGRect)theRect</span><br><span class="line"> interval:(CGFloat)theInterval</span><br><span class="line">{</span><br><span class="line"> //Implementation</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>方法名前缀</strong></p>
<ul>
<li>刷新视图的方法名要以refresh为首。</li>
<li>更新数据的方法名要以update为首</li>
<li>点击事件的方法实现要以click为首</li>
</ul>
<p>推荐</p>
<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">- (void)refreshHeaderViewWithCount:(NSUInteger)count;</span><br><span class="line">- (void)updateDataSourceWithViewModel:(ViewModel*)viewModel;</span><br></pre></td></tr></table></figure>
<p><strong>类遵循代理过多的时候(超过3个协议),换行对齐显示</strong></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">@interface ShopViewController () <UIGestureRecognizerDelegate,</span><br><span class="line"> HXSClickEventDelegate,</span><br><span class="line"> UITableViewDelegate,</span><br><span class="line"> UITableViewDataSource>{</span><br><span class="line"> ...</span><br><span class="line"> </span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>代理的方法需要明确必须执行和可不执行</strong></p>
<p>代理方法在默认情况下都是必须执行的,然而在设计一组代理方法的时候,有些方法可以不是必须执行(是因为存在默认配置),这些方法就需要使用@optional关键字来修饰:</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></pre></td><td class="code"><pre><span class="line">@protocol HomeCellViewDeleagte <NSObject></span><br><span class="line"></span><br><span class="line">@optional</span><br><span class="line">- (void)switchValueChanged:(id)sender;</span><br><span class="line">- (void)clickButtonToDetail:(id)sender;</span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<p><strong>使用定时器时。要实现startTimer 和 stopTimer 两个函数;实现函数尽量不要加入其他的业务逻辑</strong></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><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">- (void) startTimer</span><br><span class="line">{</span><br><span class="line"> if (_timer){</span><br><span class="line"> [self stopTimer];</span><br><span class="line"> }</span><br><span class="line"> _timer = ...</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">- (void) stopTimer{</span><br><span class="line"> [_timer invalidate];</span><br><span class="line"> _timer = nil;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>方法与参数名,一般都以小写字母开头;对于缩写字符串,可以大写字符开头</strong></p>
<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">+ (NSURL *)URLWithString:(NSString *)URLString;</span><br></pre></td></tr></table></figure>
<h4 id="参考文献"><a href="#参考文献" class="headerlink" title="参考文献"></a>参考文献</h4><p><a href="https://juejin.im/post/5940c8befe88c2006a468ea6#heading-29" target="_blank" rel="noopener">https://juejin.im/post/5940c8befe88c2006a468ea6#heading-29</a></p>
<p><a href="https://github.com/QianKaiLu/Objective-C-Coding-Guidelines-In-Chinese" target="_blank" rel="noopener">https://github.com/QianKaiLu/Objective-C-Coding-Guidelines-In-Chinese</a></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://zhongqing05.top/2020/04/21/iOS面试题Swift部分/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="ZQing">
<meta itemprop="description" content="平时学习的小笔记。">
<meta itemprop="image" content="/images/avatar.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="钟晴">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2020/04/21/iOS面试题Swift部分/" class="post-title-link" itemprop="url">iOS面试题Swift部分</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">Posted on</span>
<time title="Created: 2020-04-21 15:03:36 / Modified: 15:07:52" itemprop="dateCreated datePublished" datetime="2020-04-21T15:03:36+08:00">2020-04-21</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Swift/" itemprop="url" rel="index"><span itemprop="name">Swift</span></a></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="Swift基础面试题"><a href="#Swift基础面试题" class="headerlink" title="Swift基础面试题"></a>Swift基础面试题</h3><p>1、Class和Struct 的区别</p>
<p>类是引用类型,结构体是值类型。<br>结构体不可以继承<br>值类型被赋予给一个变量、常量或者被传递给一个函数时。其值会倍拷贝。<br>引用类型在被赋予到一个变量、常量或者被传递到一个函数时、其值不会被拷贝。</p>
<p>2、defer的用法。</p>
<p>使用defer代码块来表示在函数返回前,函数中最后执行的代码。无论函数是否会抛出异常,这段代码都会被执行。</p>
<p>defer语句块中的代码,会在当前作用域结束前掉用。每当一个作用域结束就进行该作用域defer执行。</p>
<p>3、什么是高阶函数。</p>
<p>一个函数如果可以一某一个个函数作为参数,或者是返回值。那么这个函数就称之为高阶函数。</p>
<p>4、什么是copy on write时候</p>
<p>写时复制,指的是swift中的值类型,并不会一开始赋值的时候就去复制,只有再需要修改的时候,才去复制。</p>
<p>5、String 与 NSString 的关系与区别</p>
<p>String 是结构体,值类型,NSStirng 是类,引用类型<br>NSString 与 String之间可以随意转换</p>
<p>6、associatedtype 的作用</p>
<p>简单来说就是protocol 使用的泛型</p>
<p>实现协议的时候,可以使用typealias 指定特定的类型,也可以自动推断</p>
<p>7、final关键字</p>
<p>final用于限制继承和重写,如果只是需要在某一个属性前加一个final。如果需要限制整个类无法被继承,那么可用用类名之前加一个final</p>
<p>8、public 和 open 的区别</p>
<p>这两个都用于模块声明需要对外界暴露的函数,区别在于,public 修饰的类,在模块外无法被继承,而open则可以任意继承,公开度来说,open>public</p>
<p>9、定义静态方法时关键字 static 和 class 有什么区别</p>
<p>static 定义的方法不可以被子类继承,class则可以</p>
<p>10、throws 和 rethrows 的用法与作用</p>
<p>throws 用在函数上,表示这个函数会抛出异常</p>
<p>rethrows 与 throws 类似,不过只适用于参数中有函数,且函数会抛出异常的情况下,rethrows 可以用throws替换,反过来不行.如</p>
<p>11、mutating关键字</p>
<p>结构体和枚举类型中修改 self 或其属性的方法必须将该实例方法标注为 mutating,否则无法在方法里改变自己的变量。</p>
<blockquote>
<p>实现协议中的 mutating 方法时,若是类类型,则不用写 mutating 关键字。而对于结构体和枚举,则必须写 mutating 关键字。</p>
</blockquote>
<p>12、必包是引用类型吗</p>
<p>必包是引用类型。如果一个必包被分配给一个变量。这个变量复制给另一个变量,那么他们引用的是同一个必包,他们的捕捉列表也会被复制。</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://zhongqing05.top/2020/04/21/iOS面试题Object-C部分/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="ZQing">
<meta itemprop="description" content="平时学习的小笔记。">
<meta itemprop="image" content="/images/avatar.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="钟晴">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2020/04/21/iOS面试题Object-C部分/" class="post-title-link" itemprop="url">iOS面试题Object-C部分</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>