17
17
- [ 2.控制流] ( #2 )
18
18
- [ 3.函数] ( #3 )
19
19
- [ 4.类与初始化(Initializers)] ( #4 )
20
-
20
+ - [ 5.枚举与结构体] ( #5 )
21
+ - [ 6.协议(Protocols)] ( #6 )
21
22
22
23
<h2 id =" 1 " >1.变量,常量,属性(property)和实例变量(instance variable)</h2 >
23
24
@@ -671,7 +672,7 @@ if let thisSementTitle = dataSource?.titleFroSegmentAtIndex?(index){
671
672
}
672
673
~~~
673
674
674
- 我们也能够用这个特性来组织我们的代码结构 ,如下面的代码所示,将UITableViewDataSource的实现移到了Extension。 使代码更清晰。
675
+ 我们还能够用这个特性来组织我们的代码结构 ,如下面的代码所示,将UITableViewDataSource的实现移到了Extension。 使代码更清晰。
675
676
676
677
~~~ swift
677
678
// MARK: - UITableViewDataSource
@@ -680,7 +681,39 @@ if let thisSementTitle = dataSource?.titleFroSegmentAtIndex?(index){
680
681
}
681
682
~~~
682
683
684
+ 3 . `Protocol Oriented Programming`
685
+
686
+ 随着Swift2.0的发布,面向协议编程正式也加入到了Swift的编程范式。 Cool.
687
+
688
+ 这种编程方式通过怎样的语法特性支撑的呢?
689
+
690
+ 那就是我们能够对协议进行扩展,也就是我们能够提供协议的默认实现,能够为协议添加新的方法与实现。
691
+
692
+ 用前面的myProtocol为例子,我们在Swift里这样为它提供默认实现。
693
+
694
+ ~~~ swift
695
+ extension myProtocol {
696
+ func hello () -> String {
697
+ return " hello world!"
698
+ }
699
+ }
700
+ ~~~
701
+
702
+ 我们还能对系统原有的protocol 进行扩展,大大增强了我们的想象空间。 Swift2.0的实现也有很多地方用extension protocol 的形式进行了重构。
703
+
704
+ 面向协议编程能够展开说很多,在这里这简单地介绍了语法。 有兴趣的朋友可以参考下面的资料:
705
+
706
+ [Session 408 : Protocol- Oriented Programming in Swift](https :// developer.apple.com/videos/wwdc/2015/?id=408)
707
+
708
+ [IF YOU'RE SUBCLASSING, YOU'RE DOING IT WRONG.](http :// krakendev.io/blog/subclassing-can-suck-and-heres-why)
709
+
710
+
711
+ < h2 id= " 7" > 7 .Swift与Cocoa </ h2>
712
+
713
+
683
714
715
+
716
+
684
717
685
718
686
719
--
0 commit comments