---
title: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
tags:  #akiba.swift #swift愛好会 #swift #イニシャライザー  
author: [熊谷友宏](https://docswell.com/user/es_kumagai)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/27V3GNWPJQ.jpg?width=480
description: Swift のイニシャライザーの基礎的なところについて 30 分くらいで復習するのに使った資料です。2017/03/06 の『AKIBA.swift × Swift愛好会 vol2』で発表しました。  ※ Docswell での公開に移行する直前の Slideshare での閲覧数は 2,197 でした。
published: March 06, 17
canonical: https://docswell.com/s/es_kumagai/Z6V7L7-loveswift-170306130917
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/27V3GNWPJQ.jpg)

ΠχγϟϥΠβʔ
෮शձ
!&quot;,*#&quot;TXJGUº4XJGUѪ޷ձWPM
‫۽‬୩༑޺
IUUQF[OFUKQ
4XJGU TXJGUQN


# Page. 2

![Page Image](https://bcdn.docswell.com/page/5JG38KDQEL.jpg)

‫۽‬୩༑޺
5PNPIJSP,VNBHBJ
⾣
4XJGU‫͕ޠݴ‬௒େ޷͖Ͱ͢ʂ
⾣
ΈΜͳͰָ͠Ήษ‫ڧ‬ձ͕େ޷͖Ͱ͢ʂ
⾣
ϓϩάϥϛϯάͷָ͠͞Λ఻͍͖͍͑ͯͨɻ
!FT@LVNBHBJ
UPNPIJSPLVNBHBJ
$PEF1JFDFGPSNBD04
IUUQF[OFUKQ
9DPEFపఈղઆ


# Page. 3

![Page Image](https://bcdn.docswell.com/page/47Q59N1WEP.jpg)

ษ‫ڧ‬ձΛ։࠵͍ͯ͠·͢ɻ
Θ͍Θ͍ɺָ͘͠ɺ
ΈΜͳͰ‫ޠ‬Β͑Δ৔ॴΛ໨ࢦͯ͠
ԣ඿J1IPOF։ൃऀษ‫ڧ‬ձ
ୈճ໨Λ݄೔ʹ։࠵ʢ໨ඪʣ
ΧδϡΞϧ4XJGUษ‫ڧ‬ձ
ZJEFW ˏԣ඿ɾഅंಓ
IUUQTBUOEPSHHSPVQTZJEFW
ୈճ໨͸‫ݕ‬౼த
DTXJGU !ԣ඿ɾ੨༿୆
IUUQTBUOEPSHHSPVQTDTXJGU
ΈΜͳͰ4XJGU ෮शձ
NJOOB@EF@TXJGU ˏौ୩
ୈճ໨Λ݄೔ʹ։࠵༧ఆ
IUUQDTXJGUDPOOQBTTDPN


# Page. 4

![Page Image](https://bcdn.docswell.com/page/KE49XGR1J1.jpg)

NPPLNPPLSBEJP
ϜοΫϜοΫϥδΦ
‫۽‬୩ͱ៸໘͕ϓϩάϥϛϯάίʔυͷ಺͔Β
ௌ͑ͯ͘͜Δ੠ʹࣖΛ܏ָ͚ͯ͠ΉϥδΦ
ຖ݄ୈ̎ɾୈ̐ ݄༵೔ ʹ഑৴
IUUQNPPLNPPLSBEJPDPNB
⾣
ୈ࿩ 
ʰNFUIPEEJTQBUDIʱ
⾣
ୈ࿩ 
ʰάϩʔόϧม਺ʱ
⾣
ୈ࿩ 
ʰHFUUFSͱTFUUFSʱ
⾣
ୈ࿩ 
ʰ‫ܕ‬ΤΠϦΞεʹ૝͍Λ஘ͤΔʱ
⾣
ୈ ̕ ࿩ 
ʰVOTBGF#JU$BTUΛ‫ͭݟ‬ΊͯΈ͍ͨʱ
⾣
ୈ ̔ ࿩ 
ʰ໊લۭؒͷ੠͕ௌ͖͍ͨʱ


# Page. 5

![Page Image](https://bcdn.docswell.com/page/L71KNDQ5EG.jpg)

ΠχγϟϥΠβʔ


# Page. 6

![Page Image](https://bcdn.docswell.com/page/G7WD4YMWE2.jpg)

Πϯελϯεͷੜ੒
 ‫ج‬ຊॻࣜ
 ࣮૷ͷ‫ج‬ຊ


# Page. 7

![Page Image](https://bcdn.docswell.com/page/4JZG2XQ273.jpg)

Πϯελϯεͷੜ੒
‫ج‬ຊॻࣜ
⾣
‫ʹܕ‬JOJUͰఆٛ͢Δ
⾣
Ҿ਺ϦετΛఴ͑ɺ໭Γ஋͸ࢦఆ͠ͳ͍
struct Person {
init(name: String, number: Int) {
}
}


# Page. 8

![Page Image](https://bcdn.docswell.com/page/YE6YV4N6JV.jpg)

Πϯελϯεͷੜ੒
࣮૷ͷ‫ج‬ຊ
⾣
શͯͷอଘ‫ܕ‬ϓϩύςΟʔΛॳ‫ظ‬Խ͢Δ
⾣
ͻͱͭͰ΋ॳ‫ظ‬Խ͠ͳ͍ͱίϯύΠϧΤϥʔ
struct Person {
var name: String
var number: Int
init(name: String, number: Int) {
self.name = name
}
}
SFUVSOGSPNJOJUJBMJ[FSXJUIPVUJOJUJBMJ[JOHBMMTUPSFEQSPQFSUJFT


# Page. 9

![Page Image](https://bcdn.docswell.com/page/GE5GDQ55E4.jpg)

ੜ੒Ͱ͖ͳ͍͜ͱΛߟྀ
 ࣦഊͨ͠ΒͲ͏͠Α͏΋ͳ͍ͱ͖
 ࣦഊ͢Δ͔΋͠Εͳ͍ͱ͖
 ࣦഊͷৄࡉΛ௨஌͍ͨ͠ͱ͖


# Page. 10

![Page Image](https://bcdn.docswell.com/page/972Y8PNG7R.jpg)

ੜ੒Ͱ͖ͳ͍͜ͱΛߟྀ
ࣦഊͨ͠ΒͲ͏͠Α͏΋ͳ͍ͱ͖
⾣
௨ৗ௨ΓJOJUͰ࣮૷͢Δ
⾣
ࣦഊ͸GBUBM&amp;SSPSͰ௨஌͢Δ
struct Person {
init(name: String, number: Int) {
guard name.isVaidAsPersionName() else {
fatalError(&quot;Invalid name&quot;)
}
:


# Page. 11

![Page Image](https://bcdn.docswell.com/page/DJYG65PGJM.jpg)

ੜ੒Ͱ͖ͳ͍͜ͱΛߟྀ
ࣦഊ͢Δ͔΋͠Εͳ͍ͱ͖
⾣
init?·ͨ͸init!Ͱ࣮૷͢Δ
⾣
ࣦഊͨ͠ΒnilΛฦ͢
struct Person {
init?(name: String, number: Int) {
guard name.isVaidAsPersionName() else {
return nil
}
:


# Page. 12

![Page Image](https://bcdn.docswell.com/page/V7NGZN58E8.jpg)

ੜ੒Ͱ͖ͳ͍͜ͱΛߟྀ
ࣦഊͷৄࡉΛ௨஌͍ͨ͠ͱ͖
⾣
init … throwsͰ࣮૷͢Δ
⾣
ࣦഊͨ͠Β&amp;SSPSΛฦ͢
struct Person {
init(name: String, number: Int) throws {
guard name.isVaidAsPersionName() else {
throw InitializeError.invalidName
}
:


# Page. 13

![Page Image](https://bcdn.docswell.com/page/YJ9WZRYXJ3.jpg)

ੜ੒Ͱ͖ͳ͍͜ͱΛߟྀ
ະॳ‫ظ‬ԽͰ΋ࣦഊͰ͖Δ
⾣
ϓϩύςΟʔΛॳ‫ظ‬Խͯ͠ͳͯ͘΋ࣦഊͰ͖Δ
⾣
Ҏલ͸ॳ‫ظ‬ԽΛࡁ·ͤͳ͍ͱ͍͚ͳ͍৔߹͕͋ͬͨ
class Person {
var name: String
var number: Int
init?(name: String, number: Int) {
guard name.isVaidAsPersionName() else {
return nil
}
อଘ‫ܕ‬ϓϩύςΟʔͷॳ‫ظ‬ԽલͰ΋ࣦഊՄೳ


# Page. 14

![Page Image](https://bcdn.docswell.com/page/GJ8N1W69ED.jpg)

ΠχγϟϥΠβʔͷ໋໊‫ن‬ଇ
 ϥϕϧ෇͚ͷ‫ج‬ຊ
 ஋อଘͷ‫ܕ‬ม‫Ͱ׵‬ͷ໋໊‫ن‬ଇ
7BMVF1SFTFSWJOH5ZQF$POWFSTJPO
 ղऍΛ൐͏‫ܕ‬ม‫Ͱ׵‬ͷ໋໊‫ن‬ଇ
/BSSPXJOH5ZQF$POWFSTJPO


# Page. 15

![Page Image](https://bcdn.docswell.com/page/LJLPQN9MER.jpg)

ΠχγϟϥΠβʔͷ໋໊‫ن‬ଇ
ϥϕϧ෇͚ͷ‫ج‬ຊ
⾣
Ҿ਺ຖʹ୶ʑͱϥϕϧΛ͚ͭΔ
⾣
ӳจͱͯ͠ͷྲྀΕ͸ҙࣝ͠ͳͯ͘ྑ͍
struct Person {
init(name: String, number: Int) {
}
ӳจͷྲྀΕ͸ҙࣝ͠ͳͯ͘ྑ͍
init(havingName: String, andNumber: Int) {
}
͜Μͳ෩ʹ‫ؤ‬ுΒͳͯ͘ྑ͍


# Page. 16

![Page Image](https://bcdn.docswell.com/page/47M41XZ6EW.jpg)

ΠχγϟϥΠβʔͷ໋໊‫ن‬ଇ
஋อଘͷ‫ܕ‬ม‫Ͱ׵‬ͷ໋໊‫ن‬ଇ
⾣
4XJGUͰ͸ΠχγϟϥΠβʔͰ‫ܕ‬ม‫׵‬Λߦ͏
⾣
‫׳‬शͱͯ͠ɺ࠷ॳͷҾ਺ʹϥϕϧΛ͚ͭͳ͍
struct Number {
init( _ string: String, radix: Int) {
*OU͔Β/VNCFS΁ͷ7BMVF1SFTFSWJOH5ZQF$POWFSTJPOΛද‫ݱ‬
}


# Page. 17

![Page Image](https://bcdn.docswell.com/page/P7RWLN6NE9.jpg)

ΠχγϟϥΠβʔͷ໋໊‫ن‬ଇ
ղऍΛ൐͏‫ܕ‬ม‫Ͱ׵‬ͷ໋໊‫ن‬ଇ
⾣
͞·͟·ͳղऍΛ൐͏‫ܕ‬ม‫׵‬
⾣
ม‫ํ׵‬๏Λ࠷ॳͷϥϕϧͰઆ໌͢Δ
struct WebSite {
init(url: String, baseAddress: URL? = nil) {
63-͔Βॳ‫ظ‬Խ͢Δ/BSSPXJOH5ZQF$POWFSTJPOΛද‫ݱ‬
}


# Page. 18

![Page Image](https://bcdn.docswell.com/page/PJXPWNM8EX.jpg)

‫ʹܕ‬ΑͬͯมΘΔੑ֨
 ஋‫ܕ‬ͷΠχγϟϥΠβʔ
 ࢀর‫ܕ‬ͷΠχγϟϥΠβʔ


# Page. 19

![Page Image](https://bcdn.docswell.com/page/3JKDVNGL7D.jpg)

‫ʹܕ‬ΑͬͯมΘΔੑ֨
஋‫ܕ‬ͷΠχγϟϥΠβʔ
⾣
஋‫ܕ‬͸σʔλߏ଄Λද‫͢ݱ‬Δ
⾣
σʔλྖҬΛॳ‫ظ‬Խ͢Δͷ͕໨త

ΠχγϟϥΠβʔ
*OJUJBMJ[FS

ϝϯόʔϫΠζΠχγϟϥΠβʔ
.FNCFSXJTF*OJUJBMJ[FS


# Page. 20

![Page Image](https://bcdn.docswell.com/page/LE3RMVN675.jpg)

‫ʹܕ‬ΑͬͯมΘΔੑ֨ʢ஋‫ܕ‬ʣ
ΠχγϟϥΠβʔ
⾣
σʔλͷॳ‫ظ‬஋Λܾఆ͢ΔΠχγϟϥΠβʔ
⾣
อଘ‫ܕ‬ϓϩύςΟʔશͯʹ஋Λઃఆ͢Δ
struct Person {
var name: String
var number: Int
init(name: String) {
self.name = name
self.number = getNextNumber()
}


# Page. 21

![Page Image](https://bcdn.docswell.com/page/8EDY68WMEG.jpg)

‫ʹܕ‬ΑͬͯมΘΔੑ֨ʢ஋‫ܕ‬ʣ
.FNCFSXJTFΠχγϟϥΠβʔ
⾣
શͯͷอଘ‫ܕ‬ϓϩύςΟʔΛड͚औͬͯॳ‫ظ‬Խ
⾣
‫ܕ‬ຊମʹJOJU࣮૷Λলུ͢Δͱࣗಈ࣮૷͞ΕΔ
struct Person {
var name: String
var number: Int
}
// イニシャライザーを定義しなくても使える
let someone = Person(name: &quot;Kumagai&quot;, number: 2)


# Page. 22

![Page Image](https://bcdn.docswell.com/page/V7P6Y89QJ8.jpg)

‫ʹܕ‬ΑͬͯมΘΔੑ֨ʢ஋‫ܕ‬ʣ
஋‫Ͱܕ‬ΠχγϟϥΠβʔΛ֦ு
⾣
FYUFOTJPOͰΠχγϟϥΠβʔΛ֦ுͰ͖Δ
⾣
஋Λॳ‫ظ‬Խͨ͠ΓɺผͷJOJUΛ‫ݺ‬ΜͩΓͰ͖Δ
struct Person {
var name: String
var number: Int
}
extension Person {
init(name: String) {
self.init(name: name, number: getNextNumber())
}


# Page. 23

![Page Image](https://bcdn.docswell.com/page/2JV3GNMPEQ.jpg)

‫ʹܕ‬ΑͬͯมΘΔੑ֨
ࢀর‫ܕ‬ͷΠχγϟϥΠβʔ
⾣
ࢀর‫ܕ‬͸ɺঢ়ଶΛ‫ڞ‬༗؅ཧ͢ΔΦϒδΣΫτ
⾣
JOJUͷ໨తʹɺΦϒδΣΫτ੍‫ޚ‬ͷ‫఺؍‬΋ՃΘΔ

ࢦఆΠχγϟϥΠβʔ
%FTJHOBUFE*OJUJBMJ[FS

ศٓΠχγϟϥΠβʔ
$POWFOJFODF*OJUJBMJ[FS

ඞਢΠχγϟϥΠβʔ
3FRVJSFE*OJUJBMJ[FS


# Page. 24

![Page Image](https://bcdn.docswell.com/page/5EG38KGQ7L.jpg)

‫ʹܕ‬ΑͬͯมΘΔੑ֨ʢࢀর‫ܕ‬ʣ
ࢦఆΠχγϟϥΠβʔ
⾣
ॳ‫ظ‬Խͷॏཁͳ໾ׂΛ୲͏ΠχγϟϥΠβʔ
⾣
‫ܧ‬ঝઌͰΦʔόʔϥΠυͰ͖Δ
⾣
‫ܧ‬ঝઌͰ৽ͨʹ࣮૷͢Δͱɺ਌ͷJOJUΛӅṭ͢Δ
w
ΦʔόʔϥΠυ͚ͩͳΒ਌ͷଞJOJU͸Ӆṭ͠ͳ͍
class Base {
init(name: String, number: Int) {
}
}


# Page. 25

![Page Image](https://bcdn.docswell.com/page/4JQ59NXW7P.jpg)

‫ʹܕ‬ΑͬͯมΘΔੑ֨ʢࢀর‫ܕ‬ʣ
ศٓΠχγϟϥΠβʔ
⾣
࠷‫ޙ‬͸ଞͷࢦఆΠχγϟϥΠβʔ‫ͼݺ‬ग़͢ඞཁ͕͋Δ
⾣
‫ܧ‬ঝઌͰΦʔόʔϥΠυͰ͖ͳ͍
⾣
‫ܧ‬ঝઌͰ৽ͨʹ࣮૷ͯ͠΋ɺ਌ͷJOJUΛӅṭ͠ͳ͍
class Base {
convenience init(name: String) {
self.init(name: name, number: getNextNumber())
}
}


# Page. 26

![Page Image](https://bcdn.docswell.com/page/K749XG61E1.jpg)

‫ʹܕ‬ΑͬͯมΘΔੑ֨ʢࢀর‫ܕ‬ʣ
ඞਢΠχγϟϥΠβʔ
⾣
ࢦఆΠχγϟϥΠβʔͱಉ౳ͷ໾ׂ
⾣
‫ܧ‬ঝઌͰΦʔόʔϥΠυ͠ͳ͚Ε͹͍͚ͳ͍
⾣
Ӆṭ͞Εͳ͚Ε͹ɺΦʔόʔϥΠυ͸লུՄೳ
class Base {
required init(name: String, number: Int) {
}
}


# Page. 27

![Page Image](https://bcdn.docswell.com/page/LJ1KNDV57G.jpg)

‫ʹܕ‬ΑͬͯมΘΔੑ֨ʢࢀর‫ܕ‬ʣ
ࢀর‫ܕ‬ͷ֦ுͰ࢖͑Δ΋ͷ
⾣
ศٓΠχγϟϥΠβʔΛ֦ுͰ͖Δ
⾣
ࢦఆJOJU΍ඞਢJOJU͸֦ுͰ͖ͳ͍
extension Object {
convenience init(a: Int) { … }
0,
init(b: Int) { … }
EFTJHOBUFEJOJUJBMJ[FSNVTUCFEFDMBSFEEJSFDUMZJO
required nit(c: Int) { … }
SFRVJSFEJOJUJBMJ[FSNVTUCFEFDMBSFEEJSFDUMZJODMBTT


# Page. 28

![Page Image](https://bcdn.docswell.com/page/GJWD4YLW72.jpg)

JOJUͷ֎ଆ͔Βͷॳ‫ظ‬Խ
 ϓϩύςΟʔʹ‫ط‬ఆ஋Λઃఆ
 ‫ط‬ఆ஋Ͱॳ‫ظ‬Խ͢Δͱ͖ͷ஫ҙ
 .FNCFSXJTFJOJUͱ‫ط‬ఆ஋


# Page. 29

![Page Image](https://bcdn.docswell.com/page/4EZG2XM2J3.jpg)

JOJUͷ֎͔Β΋ॳ‫ظ‬ԽͰ͖Δ
ϓϩύςΟʔʹ‫ط‬ఆ஋Λઃఆ
⾣
อଘ‫ܕ‬ϓϩύςΟʔʹॳ‫ظ‬஋ΛࢦఆͰ͖Δ
⾣
JOJU͕‫ݺ‬͹ΕΔલʹઃఆ͞ΕΔ
class View {
var textColor = NSColor.black
var backgroundColor = NSColor.white
init() {
}
}
JOJU಺Ͱॳ‫ظ‬Խ͠ͳͯ͘΋0,


# Page. 30

![Page Image](https://bcdn.docswell.com/page/Y76YV456EV.jpg)

JOJUͷ֎͔Β΋ॳ‫ظ‬ԽͰ͖Δ
‫ط‬ఆ஋Ͱॳ‫ظ‬Խ͢Δͱ͖஫ҙ
⾣
‫ݪ‬ଇɺͲΜͳ৔߹Ͱ΋ॳ‫ظ‬Խ͞ΕΔ
⾣
JOJU಺Ͱॳ‫ظ‬Խ͢Δͱɺ̎ճॳ‫ظ‬Խ͞ΕΔ
class Object {
var value = Value()
JOJUલʹॳ‫ظ‬Խ͞ΕΔ
init() {
value = Value()
}
}
JOJU಺Ͱॳ‫ظ‬Խ͢Δͱɺॳ‫ظ‬஋Λ্ॻ


# Page. 31

![Page Image](https://bcdn.docswell.com/page/G75GDQY574.jpg)

JOJUͷ֎͔Β΋ॳ‫ظ‬ԽͰ͖Δ
.FNCFSXJTFJOJUͱ‫ط‬ఆ஋
⾣
.FNCFSXJTFJOJU͸‫ط‬ఆ஋Λແࢹ͢Δ
⾣
ࣗ෼Ͱಉ౳ͷJOJUΛ࡞Δͱɺॳ‫ظ‬Խ͕ॏෳ
struct Data {
var value = Value()
}
‫ط‬ఆ஋͕ઃఆ͞Ε͍ͯͯ΋ʜ
let data = Data(value: Value())
ϝϯόʔϫΠζΠχγϟϥΠβʔͳΒ‫ط‬ఆ஋͸࢖ΘΕͳ͍


# Page. 32

![Page Image](https://bcdn.docswell.com/page/9J2Y8PGGJR.jpg)

.FNCFSXJTFΠχγϟϥΠβʔ
 ࣗಈ࣮૷͞Εͳ͍ͱ͖
 ࣗಈ࣮૷͞ΕΔͱ͖
 ϓϩύςΟʔ͕‫ط‬ఆ஋Λ࣋ͭͱ͖
 QVCMJDʹఏ‫͖ͱ͍ͨ͠ڙ‬͸ʜ


# Page. 33

![Page Image](https://bcdn.docswell.com/page/DEYG65VGEM.jpg)

.FNCFSXJTFΠχγϟϥΠβʔ
ࣗಈ࣮૷͞Εͳ͍ͱ͖
⾣
‫ܕ‬ຊମʹಠࣗʹJOJUΛ࣮૷ͨ͠ͱ͖
⾣
ಠࣗʹ࣮૷ͨ͠΋ͷ͚͕ͩར༻Ͱ͖Δ
struct Person {
var name: String
var number: Int
init(name: String) {
self.name = name
self.number = getNextNumber()
}
}


# Page. 34

![Page Image](https://bcdn.docswell.com/page/VJNGZNM878.jpg)

.FNCFSXJTFΠχγϟϥΠβʔ
ࣗಈ࣮૷͞ΕΔͱ͖
⾣
‫ܕ‬ຊମʹJOJUΛ࣮૷͠ͳ͍ͱ͖
⾣
‫֦ܕ‬ுͰ࣮૷ͨ͠JOJUʹ͸Ө‫͞ڹ‬Εͳ͍
struct Person {
var name: String
var number: Int
}
extension Persion {
init(name: String) {
self.name = name
self.number = getNextNumber()
}


# Page. 35

![Page Image](https://bcdn.docswell.com/page/YE9WZRNXE3.jpg)

.FNCFSXJTFΠχγϟϥΠβʔ
ϓϩύςΟʔ͕‫ط‬ఆ஋Λ࣋ͭͱ͖
⾣
શͯΛઃఆ͢Δ.FNCFSXJTFJOJU͕ఏ‫͞ڙ‬ΕΔ
⾣
શͯΛলུͨ͠৔߹ͷJOJU΋ఏ‫͞ڙ‬ΕΔ
struct RandomGenerator {
var seed1: UInt32 = arc4random()
var seed2: UInt32 = arc4random()
}
let gen1 = RandomGenerator()
let gen2 = RandomGenerator(seed1: 0, seed2: 1)


# Page. 36

![Page Image](https://bcdn.docswell.com/page/GE8N1WK97D.jpg)

.FNCFSXJTFΠχγϟϥΠβʔ
.FNCFSXJTFJOJUͷΞΫηεൣғ
⾣
‫ط‬ఆͰ͸JOUFSOBMΞΫηε
⾣
JOUFSOBMະຬͷϓϩύςΟʔΛ࣋ͭ৔߹ɺ
ΞΫηεൣғ͸͍ͪ͹Μ௿͍΋ͷʹἧ͑ΒΕΔ
⾣
ϓϩύςΟʔ͕શͯQVCMJDͰ΋ɺ
.FNCFSXJTFΠχγϟϥΠβʔ͸JOUFSOBM


# Page. 37

![Page Image](https://bcdn.docswell.com/page/LELPQNZM7R.jpg)

.FNCFSXJTFΠχγϟϥΠβʔ
QVCMJDʹఏ‫͖ͱ͍ͨ͠ڙ‬͸ʜ
 ಉ౳ͷJOJUΛಠࣗఆٛ͢ΔҊ
 ‫֦ܕ‬ுͰத‫͢ܧ‬ΔҊ


# Page. 38

![Page Image](https://bcdn.docswell.com/page/4JM41XV67W.jpg)

.FNCFSXJTFΠχγϟϥΠβʔ
ಉ౳ͷJOJUͰQVCMJDఏ‫͢ڙ‬ΔҊ
⾣
ඪ४ͷ.FNCFSXJTFJOJUͱಉ౳ʹར༻Ͱ͖Δ
⾣
‫ط‬ఆ஋෇͖ͷϓϩύςΟʔ͸ॏෳॳ‫ظ‬Խ͞ΕΔͷͰ஫ҙ
public struct MyData {
public var value = MyValue()
public init(value: MyValue) {
self.value = value
}
}


# Page. 39

![Page Image](https://bcdn.docswell.com/page/PJRWLN2N79.jpg)

.FNCFSXJTFΠχγϟϥΠβʔ
֦ுJOJUͰQVCMJDఏ‫͢ڙ‬ΔҊ
⾣
ඪ४ͷ.FNCFSXJTFJOJUΛͦͷ··࢖͏
⾣
ॏෳॳ‫ظ‬ԽΛճආͰ͖Δ͕ɺϥϕϧ͸িಥճආ͕ඞཁ
public struct MyData {
public var value = MyValue()
}
extension MyData {
public init(memberwiseWithValue value: MyValue) {
self.init(value: value)
}
}


# Page. 40

![Page Image](https://bcdn.docswell.com/page/PEXPWND87X.jpg)

.FNCFSXJTFΠχγϟϥΠβʔ
Φεεϝ͍ͨ͠ଥ‫ڠ‬Ҋ
⾣
ϓϩύςΟʔͷ௚઀ॳ‫ظ‬ԽΛ΍ΊΔ
⾣
.FNCFSXJTFJOJUಉ౳ͷJOJUΛಠ࣮ࣗ૷͢Δ
public struct MyData {
public var value: MyValue
public init(value: MyValue) {
self.value = value
}
}


# Page. 41

![Page Image](https://bcdn.docswell.com/page/3EKDVNXLJD.jpg)

ΠχγϟϥΠβʔͷ‫ͼݺ‬ग़͠
 ‫ʹ໊ܕ‬Ҿ਺Λ౉͢ํ๏
 ‫ܕ‬JOJUΛ࢖͏ํ๏


# Page. 42

![Page Image](https://bcdn.docswell.com/page/L73RMV56J5.jpg)

ΠχγϟϥΠβʔͷ‫ͼݺ‬ग़͠
‫ʹ໊ܕ‬Ҿ਺Λ౉͢ํ๏
⾣
‫ʹ໊ܕ‬Ҿ਺ϦετΛఴ͑ͯ‫ͼݺ‬ग़͢
⾣
֘౰͢ΔJOJU͕‫ͼݺ‬ग़͞ΕΔ
let speaker = Person(&quot;Kumagai&quot;, 1234)


# Page. 43

![Page Image](https://bcdn.docswell.com/page/87DY68DM7G.jpg)

ΠχγϟϥΠβʔͷ‫ͼݺ‬ग़͠
‫ܕ‬JOJUΛ࢖͏ํ๏ 
⾣
‫ʹ໊ܕ‬JOJUΛ໌‫ͼݺͯ͠ه‬ग़͢
⾣
ؔ਺‫ܕ‬ม਺ʹ֨ೲͯ͠࢖͑Δ
let h = Person.init(name: &quot;Kumagai&quot;, number: 1234)
// 関数型変数 f にイニシャライザーを入れて使える
let f: (String,Int) -&gt; Person
= Person.init(name:number:)
let someone = f(&quot;Kumagai&quot;, 1234)


# Page. 44

![Page Image](https://bcdn.docswell.com/page/VJP6Y8XQE8.jpg)

ΠχγϟϥΠβʔͷ‫ͼݺ‬ग़͠
‫ܕ‬JOJUΛ࢖͏ํ๏ 
⾣
‫ܕ‬ਪ࿦Ͱ‫͖Ͱ༻׆‬Δ
⾣
લ‫͔ޙ‬Βਪ࿦ͯ͠JOJUΛ‫͚ͯͭ͘ݟ‬ΕΔ
// 取り得る型から推論する
let list = MemberList(persons: .init(name: &quot;A&quot;))
// 関数内で、戻り値の型から推論する
func makePerson(name: String) -&gt; Person {
return .init(name: name)
}


# Page. 45

![Page Image](https://bcdn.docswell.com/page/2EV3GNLP7Q.jpg)

ϓϩτίϧͰJOJUΛ‫ن‬ఆ
 ϓϩτίϧͰ࣮૷Λٛ຿෇͚Δ


# Page. 46

![Page Image](https://bcdn.docswell.com/page/57G38KXQJL.jpg)

ϓϩτίϧͰJOJUΛ‫ن‬ఆ
ϓϩτίϧͰ࣮૷Λٛ຿෇͚Δ 
⾣
ϓϩτίϧʹJOJUΛ‫ن‬ఆͰ͖Δ
⾣
‫ن‬ఆͨ͠JOJU͸ɺඞਢΠχγϟϥΠβʔ૬౰
protocol ExpressibleByName {
init(name: String)
}


# Page. 47

![Page Image](https://bcdn.docswell.com/page/4EQ59N8WJP.jpg)

ϓϩτίϧͰJOJUΛ‫ن‬ఆ
ϓϩτίϧͰ࣮૷Λٛ຿෇͚Δ 
⾣
஋‫ܕ‬͸ɺී௨ʹJOJUͰద߹͢Δ
⾣
ࢀর‫ܕ‬͸ɺSFRVJSFEJOJUͰద߹͢Δ
// 値型をプロトコルに適合させるとき
struct Person : ExpressibleByName {
init(name: String) { … }
}
// 参照型をプロトコルに適合させるとき
class Thread : ExpressibleByName {
required init(name: String) { … }
}


# Page. 48

![Page Image](https://bcdn.docswell.com/page/KJ49XG2171.jpg)

ϓϩτίϧͰJOJUΛ‫ن‬ఆ
Ϋϥε֦ுͰ͸ద߹Ͱ͖ͳ͍
⾣
ϓϩτίϧ͕‫ٻ‬ΊΔJOJU͸SFRVJSFE૬౰
⾣
ࢀর‫ܕ‬͸ɺ‫֦ܕ‬ுͰϓϩτίϧʹద߹Ͱ͖ͳ͍
// 値型はプロトコルに適合できる
extension Person : ExpressibleByName {
init(name: String) { … }
}
// 参照型はプロトコルに適合できない
extension Thread : ExpressibleByName {
required init(name: String) { … }
}
SFRVJSFEJOJUJBMJ[FSNVTUCFEFDMBSFEEJSFDUMZJODMBTT


# Page. 49

![Page Image](https://bcdn.docswell.com/page/LE1KNDM5JG.jpg)

ϝλλΠϓͰॳ‫ظ‬Խ͢Δ
 ஋‫ܕ‬ͷΠχγϟϥΠβʔΛϝλλΠϓ͔Β࢖͏
 ࢀর‫ܕ‬ͷΠχγϟϥΠβʔΛϝλλΠϓ͔Β࢖͏


# Page. 50

![Page Image](https://bcdn.docswell.com/page/GEWD4Y3WJ2.jpg)

ϝλλΠϓͰॳ‫ظ‬Խ͢Δ
஋‫ܕ‬ͷJOJUΛϝλλΠϓ͔Β࢖͏
⾣
‫ܕ‬ͷϝλλΠϓ͸型.selfͰऔΓग़ͤΔ
⾣
ϝλλΠϓ͔ΒinitΛ‫ͼݺ‬ग़ͤΔ
struct Value {
init() { … }
}
// メタタイプ型の変数に保存して …
let type: Value.Type = Value.self
// そこからインスタンスを生成できる
let instance: Value = type.init()


# Page. 51

![Page Image](https://bcdn.docswell.com/page/47ZG2XV2E3.jpg)

ϝλλΠϓͰॳ‫ظ‬Խ͢Δ
ࢀর‫ܕ‬ͷJOJUΛϝλλΠϓ͔Β࢖͏
⾣
ࢀর‫ܕ‬͸SFRVJSFEJOJU͚͕ͩ‫ͼݺ‬ग़ͤΔ
⾣
ͦ͏Ͱͳ͍ͱɺ‫ܧ‬ঝઌͷJOJUͷଘࡏΛอূͰ͖ͳ͍
class Object {
required init(a: Int) {}
init(b: Int) {}
}
let type: Object.Type = Object.self
let a = type.init(a: 0)
let b = type.init(b: 0)
0,
DPOTUSVDUJOHBOPCKFDUPGDMBTTUZQF0CKFDUXJUIBNFUBUZQF
WBMVFNVTUVTFBSFRVJSFEJOJUJBMJ[FS


# Page. 52

![Page Image](https://bcdn.docswell.com/page/YJ6YV4Y67V.jpg)

ϝλλΠϓͰॳ‫ظ‬Խ͢Δ
ϙϦϞʔϑΟζϜ΋ߟྀ͞ΕΔ
⾣
‫ج‬ఈͷϝλλΠϓʹɺ೿ੜઌͷΛೖΕΒΕΔ
⾣
‫ܧ‬ঝؔ܎Λߟྀͯ͠JOJU͕‫ݺ‬͹ΕΔ
class Base {
required init() { … }
}
class Sub : Base {
required init() { … }
}
let types = [Base.self, Sub.self] as [Base.Type]
let instances = types.map { $0.init() }


# Page. 53

![Page Image](https://bcdn.docswell.com/page/GJ5GDQG5J4.jpg)

࠷‫ʹޙ‬એ఻


# Page. 54

![Page Image](https://bcdn.docswell.com/page/LE3RMVR575.jpg)

!ौ୩DBGF
&quot;QQMF5IF4XJGU1SPHSBNNJOH-BOHVBHFΛࢀߟʹ
4XJGUͷখ͞ͳࣄฑΛ޿͍ࢹ఺ͰோΊͯΈΔձ
ࠓճͷൃදΈ͍ͨͳ࿩Λ࣌ؒ͘Β͍͔͚ͯ
ΈΜͳͱΘ͍Θ͍ஊসͯ͠·͢ʂ
IUUQTDTXJGUDPOOQBTTDPN
NJOOB@EF@TXJGU


# Page. 55

![Page Image](https://bcdn.docswell.com/page/8EDY68YYEG.jpg)

&amp;OKPZ4XJGU
5IBOLZPV
ΠχγϟϥΠβʔ෮शձ
‫۽‬୩༑޺
!FT@LVNBHBJ
⾣
֓ཁ
⾣
໋໊‫ن‬ଇ
⾣
‫ʹܕ‬ΑͬͯมΘΔੑ֨
⾣
‫ͼݺ‬ग़͠
⾣
JOJU֎͔Βͷॳ‫ظ‬Խ
⾣
.FNCFSXJTFΠχγϟϥΠβʔ
⾣
ϓϩτίϧͰJOJUΛ‫ن‬ఆ
⾣
ϝλλΠϓ͔Βॳ‫ظ‬Խ͢Δ


