Skip to content

Commit 7fca5d6

Browse files
committed
添加 protocol oriented programming
1 parent 906bfba commit 7fca5d6

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

Swift学习:Swift与Objective-C/Swift学习:Swift与Objective-C.md

+35-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
- [2.控制流](#2)
1818
- [3.函数](#3)
1919
- [4.类与初始化(Initializers)](#4)
20-
20+
- [5.枚举与结构体](#5)
21+
- [6.协议(Protocols)](#6)
2122

2223
<h2 id="1">1.变量,常量,属性(property)和实例变量(instance variable)</h2>
2324

@@ -671,7 +672,7 @@ if let thisSementTitle = dataSource?.titleFroSegmentAtIndex?(index){
671672
}
672673
~~~
673674

674-
我们也能够用这个特性来组织我们的代码结构,如下面的代码所示,将UITableViewDataSource的实现移到了Extension使代码更清晰
675+
我们还能够用这个特性来组织我们的代码结构,如下面的代码所示,将UITableViewDataSource的实现移到了Extension使代码更清晰
675676

676677
~~~swift
677678
// MARK: - UITableViewDataSource
@@ -680,7 +681,39 @@ if let thisSementTitle = dataSource?.titleFroSegmentAtIndex?(index){
680681
}
681682
~~~
682683

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+
683714

715+
716+
684717

685718

686719
--

0 commit comments

Comments
 (0)