際際滷

際際滷Share a Scribd company logo
SWIFT
Protocol (1/2)
Bill Kim(蟾) | ibillkim@gmail.com
覈谿
Protocols
Protocol Syntax
Property Requirements
Method Requirements
Initializer Requirements
Protocols as Types
References
Protocols
襦貊 轟 蠍磯   襯 豌 豌讌
(blueprint).
襦貊 襷譟煙る  襦貊 磯ジ(conform)螻 襷
.
襦貊  蟲 豢螳蟇磯 豢螳 蠍磯レ 蠍 
襦貊 (extend) 蟆 螳ロ.
豢豌: https://noahlogs.tistory.com/13 [語 襦蠏語今]
Protocol Syntax
襦貊  企, 蟲譟一牡, 願碓 炎骸 .
protocol SomeProtocol {
// protocol definition goes here
}
struct SomeStructure: SomeProtocol {
// structure definition goes here
}
class SomeClass: SomeProtocol {
// class definition goes here
}
Property Requirements
襦貊 襦狩郁 ル 襦狩一語 螻磯 襦狩
語 覈讌 給.
讌襷 襦狩一 企螻  蠏碁Μ螻 gettable, settable讌
 覈.  襦狩磯  var襦 誤伎 .
protocol SomeProtocol {
// 襦狩一 企螻  蠏碁Μ螻 gettable, settable讌 覈.
var mustBeSettable: Int { get set }
var doesNotNeedToBeSettable: Int { get }
}
protocol AnotherProtocol {
//  襦狩磯 static れ襯  誤.
static var someTypeProperty: Int { get set }
}
Property Requirements
protocol FullyNamed {
//  襦狩磯ゼ 螳 襦貊 誤.
var fullName: String { get }
}
struct Person: FullyNamed {
var fullName: String
}
let john = Person(fullName: "John Appleseed")
// john.fullName is "John Appleseed"
class Starship: FullyNamed {
var prefix: String?
var name: String
init(name: String, prefix: String? = nil) {
self.name = name
self.prefix = prefix
}
var fullName: String {
return (prefix != nil ? prefix! + " " : "") + name
}
}
// 螻磯 襦狩磯   給.
var ncc1701 = Starship(name: "Enterprise", prefix: "USS")
// ncc1701.fullName is "USS Enterprise"
Method Requirements
旧ろ 伎 譟伎  襦 企殊伎襯 豢螳
  給.
 覦覯朱 貉れろ  企殊伎 朱誤磯ゼ l  
襦 覲蟆渚蟇磯  蟲 讌  豐蠍壱 覲企ゼ 豢螳
 給.
Method Requirements
protocol SomeProtocol {
static func someTypeMethod()
}
protocol RandomNumberGenerator {
func random() -> Double
}
class LinearCongruentialGenerator: RandomNumberGenerator {
var lastRandom = 42.0
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -> Double {
lastRandom = ((lastRandom * a + c).truncatingRemainder(dividingBy:m))
return lastRandom / m
}
}
let generator = LinearCongruentialGenerator()
print("Here's a random number: (generator.random())")
// Prints "Here's a random number: 0.3746499199817101
print("And another one: (generator.random())")
// Prints "And another one: 0.729023776863283"
Method Requirements
mutating れ襯  語ろ伎れ 覲蟆 螳ロる 蟆 
 給.
 mutating れ 螳 襷 .
protocol Togglable {
mutating func toggle()
}
enum OnOffSwitch: Togglable {
case off, on
mutating func toggle() {
switch self {
case .off:
self = .on
case .on:
self = .off
}
}
}
var lightSwitch = OnOffSwitch.off
lightSwitch.toggle()
print(lightSwitch) // lightSwitch is now equal to .on
Initializer Requirements
襦貊 襦 蟲伎狩 企殊伎襯 讌  
.
protocol SomeProtocol {
init(someParameter: Int)
}
class SomeClass: SomeProtocol {
// 企れ 襦貊  企殊伎 蟲
// 襦貊 轟 企殊伎螳 り 覈蠍 覓語 蟲
// 企 企殊伎 required れ襯 覿譴 .
// 企  final襦 碁 蟆 required襯 讌  .
// final襦 碁覃 觚企 讌 蠍 覓語.
required init(someParameter: Int) {
// initializer implementation goes here
}
}
Initializer Requirements
protocol SomeProtocol {
init()
}
class SomeSuperClass {
init() {
// initializer implementation goes here
}
}
class SomeSubClass: SomeSuperClass, SomeProtocol {
// 轟 襦貊  企殊伎襯 蟲螻, 狩企れ 企殊伎襯 觚企燕 蟆曙
// 企殊伎  required れ override れ襯 伎.
required override init() {
// initializer implementation goes here
}
}
Protocols as Types
襦貊  朱  螳ロ. 蠏碁蠍 覓語 れ
螻 螳    覈 螻褐 襦貊   
.
- , 覃, 企殊伎 朱誤  轟 襴 
- , 覲, 襦狩一 
- 貉企 覦一,  煙 危
Protocols as Types
protocol RandomNumberGenerator {
func random() -> Double
}
class LinearCongruentialGenerator: RandomNumberGenerator {
var lastRandom = 42.0
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -> Double {
lastRandom = ((lastRandom * a + c).truncatingRemainder(dividingBy:m))
return lastRandom / m
}
}
class Dice {
let sides: Int
let generator: RandomNumberGenerator
init(sides: Int, generator: RandomNumberGenerator) {
self.sides = sides
self.generator = generator
}
func roll() -> Int {
return Int(generator.random() * Double(sides)) + 1
}
}
var d6 = Dice(sides: 6, generator: LinearCongruentialGenerator())
for _ in 1...5 {
rint("Random dice roll is (d6.roll())")
}
// Random dice roll is 3, Random dice roll is 5, Random dice roll is 4
// Random dice roll is 5, Random dice roll is 4
References
[1] [Swift]Protocols 襴 : http://minsone.github.io/mac/
ios/swift-protocols-summary
[2] Protocols : https://docs.swift.org/swift-book/
LanguageGuide/Protocols.html
[3] Swift ) Protocols (4) : https://zeddios.tistory.com/347
[4] Swift Protocol  蠍 : https://
academy.realm.io/kr/posts/understanding-swift-
protocol/
[5] Swift 4.2 Protocol 螻旧 覓語 襴 : https://
medium.com/@kimtaesoo188/swift-4-2-protocol-螻旧-覓語-
襴-f3a97c6f8cc2
References
[6] 襦貊 (Protocols) : https://jusung.gitbook.io/the-
swift-language-guide/language-guide/21-protocols
[7] る Swift  (Protocol) : https://medium.com/
@jgj455/る-swift--protocol-f18c82571dad
[8] [Swift] Protocol [01] : https://baked-
corn.tistory.com/24
[9] [Swift] Protocol [02] : https://baked-
corn.tistory.com/26
[10] Swift  襦貊 讌 襦蠏碁覦 : https://
blog.yagom.net/531/
Thank you!

More Related Content

What's hot (20)

襦語ろ磯 E04 js function
襦語ろ磯 E04 js function襦語ろ磯 E04 js function
襦語ろ磯 E04 js function
Young-Beom Rhee
襦語ろ磯 E05 js closure oop
襦語ろ磯 E05 js closure oop襦語ろ磯 E05 js closure oop
襦語ろ磯 E05 js closure oop
Young-Beom Rhee
ろ襯危ろ磯 E04 Javascript 螳豌伎, 襦蠏碁覦
ろ襯危ろ磯 E04 Javascript 螳豌伎,  襦蠏碁覦ろ襯危ろ磯 E04 Javascript 螳豌伎,  襦蠏碁覦
ろ襯危ろ磯 E04 Javascript 螳豌伎, 襦蠏碁覦
Young-Beom Rhee
願 覦 Chap.11 蠍磯蓋 API 企(java)(KOR)
願 覦 Chap.11 蠍磯蓋 API 企(java)(KOR)願 覦 Chap.11 蠍磯蓋 API 企(java)(KOR)
願 覦 Chap.11 蠍磯蓋 API 企(java)(KOR)
MIN SEOK KOO
Javascript ろ 螳ロ 貊(Executable Code) ろ 貊ろ(Execution Context), Lexical En...
Javascript ろ 螳ロ 貊(Executable Code) ろ 貊ろ(Execution Context), Lexical En...Javascript ろ 螳ロ 貊(Executable Code) ろ 貊ろ(Execution Context), Lexical En...
Javascript ろ 螳ロ 貊(Executable Code) ろ 貊ろ(Execution Context), Lexical En...
Young-Beom Rhee
Javascript - Function
Javascript - FunctionJavascript - Function
Javascript - Function
wonmin lee
Lambda 覓伎瑚
Lambda  覓伎瑚Lambda  覓伎瑚
Lambda 覓伎瑚
Vong Sik Kong
襦語ろ磯 E03 - Javascript intro.
襦語ろ磯 E03 - Javascript intro.襦語ろ磯 E03 - Javascript intro.
襦語ろ磯 E03 - Javascript intro.
Young-Beom Rhee
願 覦 Chap. 6 企(CLASS)(KOR)
願 覦 Chap. 6 企(CLASS)(KOR)願 覦 Chap. 6 企(CLASS)(KOR)
願 覦 Chap. 6 企(CLASS)(KOR)
MIN SEOK KOO
Startup JavaScript 6 - , れ, 企
Startup JavaScript 6 - , れ, 企Startup JavaScript 6 - , れ, 企
Startup JavaScript 6 - , れ, 企
Circulus
語伎ろ, 企, IIFE
語伎ろ, 企, IIFE語伎ろ, 企, IIFE
語伎ろ, 企, IIFE
ChangHyeon Bae
Startup JavaScript 3 - 譟郁唄覓, 覦覲給, 語襴
Startup JavaScript 3 - 譟郁唄覓, 覦覲給, 語襴Startup JavaScript 3 - 譟郁唄覓, 覦覲給, 語襴
Startup JavaScript 3 - 譟郁唄覓, 覦覲給, 語襴
Circulus
覦ろ襴渚 蠍一覓碁~蠍一
覦ろ襴渚 蠍一覓碁~蠍一覦ろ襴渚 蠍一覓碁~蠍一
覦ろ襴渚 蠍一覓碁~蠍一
危磯 C++ 螻給
危磯 C++ 螻給危磯 C++ 螻給
危磯 C++ 螻給
quxn6
Start IoT with JavaScript - 5.螳豌2
Start IoT with JavaScript - 5.螳豌2Start IoT with JavaScript - 5.螳豌2
Start IoT with JavaScript - 5.螳豌2
Park Jonggun
C++ Concurrency in Action 9-2 Interrupting threads
C++ Concurrency in Action 9-2 Interrupting threadsC++ Concurrency in Action 9-2 Interrupting threads
C++ Concurrency in Action 9-2 Interrupting threads
Seok-joon Yun
[Swift] Functions
[Swift] Functions[Swift] Functions
[Swift] Functions
Bill Kim
javascript01
javascript01javascript01
javascript01
ChangHyeon Bae
覦ろ襴渚
覦ろ襴渚 覦ろ襴渚
覦ろ襴渚
讌 覲
Javascript 蟲′襭 pdf
Javascript 蟲′襭 pdfJavascript 蟲′襭 pdf
Javascript 蟲′襭 pdf
Hyosang Hong
襦語ろ磯 E04 js function
襦語ろ磯 E04 js function襦語ろ磯 E04 js function
襦語ろ磯 E04 js function
Young-Beom Rhee
襦語ろ磯 E05 js closure oop
襦語ろ磯 E05 js closure oop襦語ろ磯 E05 js closure oop
襦語ろ磯 E05 js closure oop
Young-Beom Rhee
ろ襯危ろ磯 E04 Javascript 螳豌伎, 襦蠏碁覦
ろ襯危ろ磯 E04 Javascript 螳豌伎,  襦蠏碁覦ろ襯危ろ磯 E04 Javascript 螳豌伎,  襦蠏碁覦
ろ襯危ろ磯 E04 Javascript 螳豌伎, 襦蠏碁覦
Young-Beom Rhee
願 覦 Chap.11 蠍磯蓋 API 企(java)(KOR)
願 覦 Chap.11 蠍磯蓋 API 企(java)(KOR)願 覦 Chap.11 蠍磯蓋 API 企(java)(KOR)
願 覦 Chap.11 蠍磯蓋 API 企(java)(KOR)
MIN SEOK KOO
Javascript ろ 螳ロ 貊(Executable Code) ろ 貊ろ(Execution Context), Lexical En...
Javascript ろ 螳ロ 貊(Executable Code) ろ 貊ろ(Execution Context), Lexical En...Javascript ろ 螳ロ 貊(Executable Code) ろ 貊ろ(Execution Context), Lexical En...
Javascript ろ 螳ロ 貊(Executable Code) ろ 貊ろ(Execution Context), Lexical En...
Young-Beom Rhee
Javascript - Function
Javascript - FunctionJavascript - Function
Javascript - Function
wonmin lee
襦語ろ磯 E03 - Javascript intro.
襦語ろ磯 E03 - Javascript intro.襦語ろ磯 E03 - Javascript intro.
襦語ろ磯 E03 - Javascript intro.
Young-Beom Rhee
願 覦 Chap. 6 企(CLASS)(KOR)
願 覦 Chap. 6 企(CLASS)(KOR)願 覦 Chap. 6 企(CLASS)(KOR)
願 覦 Chap. 6 企(CLASS)(KOR)
MIN SEOK KOO
Startup JavaScript 6 - , れ, 企
Startup JavaScript 6 - , れ, 企Startup JavaScript 6 - , れ, 企
Startup JavaScript 6 - , れ, 企
Circulus
語伎ろ, 企, IIFE
語伎ろ, 企, IIFE語伎ろ, 企, IIFE
語伎ろ, 企, IIFE
ChangHyeon Bae
Startup JavaScript 3 - 譟郁唄覓, 覦覲給, 語襴
Startup JavaScript 3 - 譟郁唄覓, 覦覲給, 語襴Startup JavaScript 3 - 譟郁唄覓, 覦覲給, 語襴
Startup JavaScript 3 - 譟郁唄覓, 覦覲給, 語襴
Circulus
覦ろ襴渚 蠍一覓碁~蠍一
覦ろ襴渚 蠍一覓碁~蠍一覦ろ襴渚 蠍一覓碁~蠍一
覦ろ襴渚 蠍一覓碁~蠍一
危磯 C++ 螻給
危磯 C++ 螻給危磯 C++ 螻給
危磯 C++ 螻給
quxn6
Start IoT with JavaScript - 5.螳豌2
Start IoT with JavaScript - 5.螳豌2Start IoT with JavaScript - 5.螳豌2
Start IoT with JavaScript - 5.螳豌2
Park Jonggun
C++ Concurrency in Action 9-2 Interrupting threads
C++ Concurrency in Action 9-2 Interrupting threadsC++ Concurrency in Action 9-2 Interrupting threads
C++ Concurrency in Action 9-2 Interrupting threads
Seok-joon Yun
[Swift] Functions
[Swift] Functions[Swift] Functions
[Swift] Functions
Bill Kim
覦ろ襴渚
覦ろ襴渚 覦ろ襴渚
覦ろ襴渚
讌 覲
Javascript 蟲′襭 pdf
Javascript 蟲′襭 pdfJavascript 蟲′襭 pdf
Javascript 蟲′襭 pdf
Hyosang Hong

Similar to [Swift] Protocol (1/2) (20)

[Swift] Protocol (2/2)
[Swift] Protocol (2/2)[Swift] Protocol (2/2)
[Swift] Protocol (2/2)
Bill Kim
Javascript 譟郁 蠍
Javascript 譟郁   蠍Javascript 譟郁   蠍
Javascript 譟郁 蠍
jongho jeong
6 function
6 function6 function
6 function
覦 8
覦 8覦 8
覦 8
穴襴 覦ろ襴渚 襴
穴襴 覦ろ襴渚 襴穴襴 覦ろ襴渚 襴
穴襴 覦ろ襴渚 襴
TR 069 企殊伎誤 蟆襭 3ク
TR 069 企殊伎誤 蟆襭 3クTR 069 企殊伎誤 蟆襭 3ク
TR 069 企殊伎誤 蟆襭 3ク
ymtech
PowerVR Low Level GLSL Optimisation
PowerVR Low Level GLSL Optimisation PowerVR Low Level GLSL Optimisation
PowerVR Low Level GLSL Optimisation
覩殊
襦蠏碁覦
 襦蠏碁覦 襦蠏碁覦
襦蠏碁覦
Min-su Kim
[蠖] The C++ Programming Language 13 襴
[蠖] The C++ Programming Language 13 襴[蠖] The C++ Programming Language 13 襴
[蠖] The C++ Programming Language 13 襴
矧願
GKAC 2015 Apr. - Battery, 襦企襯 API 語
GKAC 2015 Apr. - Battery, 襦企襯    API 語GKAC 2015 Apr. - Battery, 襦企襯    API 語
GKAC 2015 Apr. - Battery, 襦企襯 API 語
GDG Korea
企襴 蠍磯 DApp 螳覦螻 る 螻 れ (ERC20,ERC721,ERC1155,ERC1400)
企襴 蠍磯 DApp 螳覦螻 る 螻 れ (ERC20,ERC721,ERC1155,ERC1400)企襴 蠍磯 DApp 螳覦螻 る 螻 れ (ERC20,ERC721,ERC1155,ERC1400)
企襴 蠍磯 DApp 螳覦螻 る 螻 れ (ERC20,ERC721,ERC1155,ERC1400)
wonyong hwang
20150212 c++11 features used in crow
20150212 c++11 features used in crow20150212 c++11 features used in crow
20150212 c++11 features used in crow
Jaeseung Ha
危襴貅伎 焔 豕 蠍磯
危襴貅伎 焔 豕 蠍磯危襴貅伎 焔 豕 蠍磯
危襴貅伎 焔 豕 蠍磯
Daniel Kim
襦蠏碁覦 : C++11 伎手鍵
襦蠏碁覦 : C++11 伎手鍵襦蠏碁覦 : C++11 伎手鍵
襦蠏碁覦 : C++11 伎手鍵
Jongwook Choi
Refelction 螳螻 RTTR 殊企襴
Refelction 螳螻 RTTR 殊企襴Refelction 螳螻 RTTR 殊企襴
Refelction 螳螻 RTTR 殊企襴
ssuser7c5a40
Programming Cascading
Programming CascadingProgramming Cascading
Programming Cascading
Taewook Eom
Let's Go (golang)
Let's Go (golang)Let's Go (golang)
Let's Go (golang)
[143] Modern C++ 覓伎^蟇 ?
[143] Modern C++ 覓伎^蟇  ?[143] Modern C++ 覓伎^蟇  ?
[143] Modern C++ 覓伎^蟇 ?
NAVER D2
[NDC2015] C++11 螻蠍 蠍磯 - Crow 蠍磯 譴朱
[NDC2015] C++11 螻蠍 蠍磯 - Crow  蠍磯 譴朱[NDC2015] C++11 螻蠍 蠍磯 - Crow  蠍磯 譴朱
[NDC2015] C++11 螻蠍 蠍磯 - Crow 蠍磯 譴朱
Jaeseung Ha
20201121 貊 朱讌螻
20201121 貊 朱讌螻20201121 貊 朱讌螻
20201121 貊 朱讌螻
Chiwon Song
[Swift] Protocol (2/2)
[Swift] Protocol (2/2)[Swift] Protocol (2/2)
[Swift] Protocol (2/2)
Bill Kim
Javascript 譟郁 蠍
Javascript 譟郁   蠍Javascript 譟郁   蠍
Javascript 譟郁 蠍
jongho jeong
6 function
6 function6 function
6 function
覦 8
覦 8覦 8
覦 8
穴襴 覦ろ襴渚 襴
穴襴 覦ろ襴渚 襴穴襴 覦ろ襴渚 襴
穴襴 覦ろ襴渚 襴
TR 069 企殊伎誤 蟆襭 3ク
TR 069 企殊伎誤 蟆襭 3クTR 069 企殊伎誤 蟆襭 3ク
TR 069 企殊伎誤 蟆襭 3ク
ymtech
PowerVR Low Level GLSL Optimisation
PowerVR Low Level GLSL Optimisation PowerVR Low Level GLSL Optimisation
PowerVR Low Level GLSL Optimisation
覩殊
襦蠏碁覦
 襦蠏碁覦 襦蠏碁覦
襦蠏碁覦
Min-su Kim
[蠖] The C++ Programming Language 13 襴
[蠖] The C++ Programming Language 13 襴[蠖] The C++ Programming Language 13 襴
[蠖] The C++ Programming Language 13 襴
矧願
GKAC 2015 Apr. - Battery, 襦企襯 API 語
GKAC 2015 Apr. - Battery, 襦企襯    API 語GKAC 2015 Apr. - Battery, 襦企襯    API 語
GKAC 2015 Apr. - Battery, 襦企襯 API 語
GDG Korea
企襴 蠍磯 DApp 螳覦螻 る 螻 れ (ERC20,ERC721,ERC1155,ERC1400)
企襴 蠍磯 DApp 螳覦螻 る 螻 れ (ERC20,ERC721,ERC1155,ERC1400)企襴 蠍磯 DApp 螳覦螻 る 螻 れ (ERC20,ERC721,ERC1155,ERC1400)
企襴 蠍磯 DApp 螳覦螻 る 螻 れ (ERC20,ERC721,ERC1155,ERC1400)
wonyong hwang
20150212 c++11 features used in crow
20150212 c++11 features used in crow20150212 c++11 features used in crow
20150212 c++11 features used in crow
Jaeseung Ha
危襴貅伎 焔 豕 蠍磯
危襴貅伎 焔 豕 蠍磯危襴貅伎 焔 豕 蠍磯
危襴貅伎 焔 豕 蠍磯
Daniel Kim
襦蠏碁覦 : C++11 伎手鍵
襦蠏碁覦 : C++11 伎手鍵襦蠏碁覦 : C++11 伎手鍵
襦蠏碁覦 : C++11 伎手鍵
Jongwook Choi
Refelction 螳螻 RTTR 殊企襴
Refelction 螳螻 RTTR 殊企襴Refelction 螳螻 RTTR 殊企襴
Refelction 螳螻 RTTR 殊企襴
ssuser7c5a40
Programming Cascading
Programming CascadingProgramming Cascading
Programming Cascading
Taewook Eom
Let's Go (golang)
Let's Go (golang)Let's Go (golang)
Let's Go (golang)
[143] Modern C++ 覓伎^蟇 ?
[143] Modern C++ 覓伎^蟇  ?[143] Modern C++ 覓伎^蟇  ?
[143] Modern C++ 覓伎^蟇 ?
NAVER D2
[NDC2015] C++11 螻蠍 蠍磯 - Crow 蠍磯 譴朱
[NDC2015] C++11 螻蠍 蠍磯 - Crow  蠍磯 譴朱[NDC2015] C++11 螻蠍 蠍磯 - Crow  蠍磯 譴朱
[NDC2015] C++11 螻蠍 蠍磯 - Crow 蠍磯 譴朱
Jaeseung Ha
20201121 貊 朱讌螻
20201121 貊 朱讌螻20201121 貊 朱讌螻
20201121 貊 朱讌螻
Chiwon Song

More from Bill Kim (20)

[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison
Bill Kim
[Algorithm] Big O Notation
[Algorithm] Big O Notation[Algorithm] Big O Notation
[Algorithm] Big O Notation
Bill Kim
[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell Sort
Bill Kim
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix Sort
Bill Kim
[Algorithm] Quick Sort
[Algorithm] Quick Sort[Algorithm] Quick Sort
[Algorithm] Quick Sort
Bill Kim
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap Sort
Bill Kim
[Algorithm] Counting Sort
[Algorithm] Counting Sort[Algorithm] Counting Sort
[Algorithm] Counting Sort
Bill Kim
[Algorithm] Selection Sort
[Algorithm] Selection Sort[Algorithm] Selection Sort
[Algorithm] Selection Sort
Bill Kim
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge Sort
Bill Kim
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
Bill Kim
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
Bill Kim
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary Search
Bill Kim
[Algorithm] Recursive(蠏)
[Algorithm] Recursive(蠏)[Algorithm] Recursive(蠏)
[Algorithm] Recursive(蠏)
Bill Kim
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL[Swift] Data Structure - AVL
[Swift] Data Structure - AVL
Bill Kim
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree
Bill Kim
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)
Bill Kim
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)
Bill Kim
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree
Bill Kim
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree[Swift] Data Structure - Tree
[Swift] Data Structure - Tree
Bill Kim
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph[Swift] Data Structure - Graph
[Swift] Data Structure - Graph
Bill Kim
[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison
Bill Kim
[Algorithm] Big O Notation
[Algorithm] Big O Notation[Algorithm] Big O Notation
[Algorithm] Big O Notation
Bill Kim
[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell Sort
Bill Kim
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix Sort
Bill Kim
[Algorithm] Quick Sort
[Algorithm] Quick Sort[Algorithm] Quick Sort
[Algorithm] Quick Sort
Bill Kim
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap Sort
Bill Kim
[Algorithm] Counting Sort
[Algorithm] Counting Sort[Algorithm] Counting Sort
[Algorithm] Counting Sort
Bill Kim
[Algorithm] Selection Sort
[Algorithm] Selection Sort[Algorithm] Selection Sort
[Algorithm] Selection Sort
Bill Kim
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge Sort
Bill Kim
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
Bill Kim
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
Bill Kim
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary Search
Bill Kim
[Algorithm] Recursive(蠏)
[Algorithm] Recursive(蠏)[Algorithm] Recursive(蠏)
[Algorithm] Recursive(蠏)
Bill Kim
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL[Swift] Data Structure - AVL
[Swift] Data Structure - AVL
Bill Kim
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree
Bill Kim
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)
Bill Kim
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)
Bill Kim
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree
Bill Kim
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree[Swift] Data Structure - Tree
[Swift] Data Structure - Tree
Bill Kim
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph[Swift] Data Structure - Graph
[Swift] Data Structure - Graph
Bill Kim

[Swift] Protocol (1/2)

  • 1. SWIFT Protocol (1/2) Bill Kim(蟾) | ibillkim@gmail.com
  • 2. 覈谿 Protocols Protocol Syntax Property Requirements Method Requirements Initializer Requirements Protocols as Types References
  • 3. Protocols 襦貊 轟 蠍磯 襯 豌 豌讌 (blueprint). 襦貊 襷譟煙る 襦貊 磯ジ(conform)螻 襷 . 襦貊 蟲 豢螳蟇磯 豢螳 蠍磯レ 蠍 襦貊 (extend) 蟆 螳ロ. 豢豌: https://noahlogs.tistory.com/13 [語 襦蠏語今]
  • 4. Protocol Syntax 襦貊 企, 蟲譟一牡, 願碓 炎骸 . protocol SomeProtocol { // protocol definition goes here } struct SomeStructure: SomeProtocol { // structure definition goes here } class SomeClass: SomeProtocol { // class definition goes here }
  • 5. Property Requirements 襦貊 襦狩郁 ル 襦狩一語 螻磯 襦狩 語 覈讌 給. 讌襷 襦狩一 企螻 蠏碁Μ螻 gettable, settable讌 覈. 襦狩磯 var襦 誤伎 . protocol SomeProtocol { // 襦狩一 企螻 蠏碁Μ螻 gettable, settable讌 覈. var mustBeSettable: Int { get set } var doesNotNeedToBeSettable: Int { get } } protocol AnotherProtocol { // 襦狩磯 static れ襯 誤. static var someTypeProperty: Int { get set } }
  • 6. Property Requirements protocol FullyNamed { // 襦狩磯ゼ 螳 襦貊 誤. var fullName: String { get } } struct Person: FullyNamed { var fullName: String } let john = Person(fullName: "John Appleseed") // john.fullName is "John Appleseed" class Starship: FullyNamed { var prefix: String? var name: String init(name: String, prefix: String? = nil) { self.name = name self.prefix = prefix } var fullName: String { return (prefix != nil ? prefix! + " " : "") + name } } // 螻磯 襦狩磯 給. var ncc1701 = Starship(name: "Enterprise", prefix: "USS") // ncc1701.fullName is "USS Enterprise"
  • 7. Method Requirements 旧ろ 伎 譟伎 襦 企殊伎襯 豢螳 給. 覦覯朱 貉れろ 企殊伎 朱誤磯ゼ l 襦 覲蟆渚蟇磯 蟲 讌 豐蠍壱 覲企ゼ 豢螳 給.
  • 8. Method Requirements protocol SomeProtocol { static func someTypeMethod() } protocol RandomNumberGenerator { func random() -> Double } class LinearCongruentialGenerator: RandomNumberGenerator { var lastRandom = 42.0 let m = 139968.0 let a = 3877.0 let c = 29573.0 func random() -> Double { lastRandom = ((lastRandom * a + c).truncatingRemainder(dividingBy:m)) return lastRandom / m } } let generator = LinearCongruentialGenerator() print("Here's a random number: (generator.random())") // Prints "Here's a random number: 0.3746499199817101 print("And another one: (generator.random())") // Prints "And another one: 0.729023776863283"
  • 9. Method Requirements mutating れ襯 語ろ伎れ 覲蟆 螳ロる 蟆 給. mutating れ 螳 襷 . protocol Togglable { mutating func toggle() } enum OnOffSwitch: Togglable { case off, on mutating func toggle() { switch self { case .off: self = .on case .on: self = .off } } } var lightSwitch = OnOffSwitch.off lightSwitch.toggle() print(lightSwitch) // lightSwitch is now equal to .on
  • 10. Initializer Requirements 襦貊 襦 蟲伎狩 企殊伎襯 讌 . protocol SomeProtocol { init(someParameter: Int) } class SomeClass: SomeProtocol { // 企れ 襦貊 企殊伎 蟲 // 襦貊 轟 企殊伎螳 り 覈蠍 覓語 蟲 // 企 企殊伎 required れ襯 覿譴 . // 企 final襦 碁 蟆 required襯 讌 . // final襦 碁覃 觚企 讌 蠍 覓語. required init(someParameter: Int) { // initializer implementation goes here } }
  • 11. Initializer Requirements protocol SomeProtocol { init() } class SomeSuperClass { init() { // initializer implementation goes here } } class SomeSubClass: SomeSuperClass, SomeProtocol { // 轟 襦貊 企殊伎襯 蟲螻, 狩企れ 企殊伎襯 觚企燕 蟆曙 // 企殊伎 required れ override れ襯 伎. required override init() { // initializer implementation goes here } }
  • 12. Protocols as Types 襦貊 朱 螳ロ. 蠏碁蠍 覓語 れ 螻 螳 覈 螻褐 襦貊 . - , 覃, 企殊伎 朱誤 轟 襴 - , 覲, 襦狩一 - 貉企 覦一, 煙 危
  • 13. Protocols as Types protocol RandomNumberGenerator { func random() -> Double } class LinearCongruentialGenerator: RandomNumberGenerator { var lastRandom = 42.0 let m = 139968.0 let a = 3877.0 let c = 29573.0 func random() -> Double { lastRandom = ((lastRandom * a + c).truncatingRemainder(dividingBy:m)) return lastRandom / m } } class Dice { let sides: Int let generator: RandomNumberGenerator init(sides: Int, generator: RandomNumberGenerator) { self.sides = sides self.generator = generator } func roll() -> Int { return Int(generator.random() * Double(sides)) + 1 } } var d6 = Dice(sides: 6, generator: LinearCongruentialGenerator()) for _ in 1...5 { rint("Random dice roll is (d6.roll())") } // Random dice roll is 3, Random dice roll is 5, Random dice roll is 4 // Random dice roll is 5, Random dice roll is 4
  • 14. References [1] [Swift]Protocols 襴 : http://minsone.github.io/mac/ ios/swift-protocols-summary [2] Protocols : https://docs.swift.org/swift-book/ LanguageGuide/Protocols.html [3] Swift ) Protocols (4) : https://zeddios.tistory.com/347 [4] Swift Protocol 蠍 : https:// academy.realm.io/kr/posts/understanding-swift- protocol/ [5] Swift 4.2 Protocol 螻旧 覓語 襴 : https:// medium.com/@kimtaesoo188/swift-4-2-protocol-螻旧-覓語- 襴-f3a97c6f8cc2
  • 15. References [6] 襦貊 (Protocols) : https://jusung.gitbook.io/the- swift-language-guide/language-guide/21-protocols [7] る Swift (Protocol) : https://medium.com/ @jgj455/る-swift--protocol-f18c82571dad [8] [Swift] Protocol [01] : https://baked- corn.tistory.com/24 [9] [Swift] Protocol [02] : https://baked- corn.tistory.com/26 [10] Swift 襦貊 讌 襦蠏碁覦 : https:// blog.yagom.net/531/