Slide 1

Slide 1 text

Struct ͱ Class ͷ࢖͍෼͚ํ ࠓߋฉ͚ͳ͍ʁ

Slide 2

Slide 2 text

Who am I? MAGES. Inc. Game Div. (5pb.Games) iOS Developer @loveeʢ੕໺ܙྤʣ ͷΞγελϯτP←!!!!

Slide 3

Slide 3 text

class struct vs

Slide 4

Slide 4 text

struct class ܕ ஋ܕ ࢀরܕ ܧঝ ෆՄ Մೳ deinit ͳ͠ ͋Γ

Slide 5

Slide 5 text

struct class ܕ ஋ܕ ࢀরܕ ܧঝ ෆՄ Մೳ deinit ͳ͠ ͋Γ ๏ ੜ੒ίετ௿͍ ๏ ॥؀ࢀর໰୊͕ͳ͍ ๏ εϨουηʔϑ ๏ ॲཧ͕୯७Ͱ෼͔Γ΍͍͢ ๏ etc…

Slide 6

Slide 6 text

΋͏શ෦ struct ࢖͓͏ͥʂ ʢUIView ͱ͔͔Βܧঝ͢Δ΋ͷҎ֎ʣ

Slide 7

Slide 7 text

struct Player { let name: String // ໊લɺมߋෆՄ var level = 1 // ͦͷଞॾʑ init(name: String) { self.name = name } mutating func levelUp() { level += 1 } } var player = Player(name: "ྋ෩੨༿") { didSet { // ηʔϒσʔλ } } player.levelUp() // didSet ͕ݺ͹ΕͯϢʔβσʔλ͕ηʔϒ͞ΕΔ

Slide 8

Slide 8 text

struct Player { let name: String // ໊લɺมߋෆՄ var level = 1 // ͦͷଞॾʑ init(name: String) { self.name = name } mutating func levelUp() { level += 1 } } var player = Player(name: "ྋ෩੨༿") { didSet { // ηʔϒσʔλ } } player.levelUp() // didSet ͕ݺ͹ΕͯϢʔβσʔλ͕ηʔϒ͞ΕΔ

Slide 9

Slide 9 text

struct Player { let name: String // ໊લɺมߋෆՄ var level = 1 // ͦͷଞॾʑ init(name: String) { self.name = name } mutating func levelUp() { level += 1 } } var player = Player(name: "ྋ෩੨༿") { didSet { // ηʔϒσʔλ } } player.levelUp() // didSet ͕ݺ͹ΕͯϢʔβσʔλ͕ηʔϒ͞ΕΔ
 player = Player(name: "ࡩͶͶ") // ϩδοΫΤϥʔɿఆ਺Ͱ͋Δ player.name ͕มΘͬͯ͠·͏

Slide 10

Slide 10 text

struct Player { let name: String // ໊લɺมߋෆՄ var level = 1 // ͦͷଞॾʑ init(name: String) { self.name = name } mutating func levelUp() { level += 1 } } var player = Player(name: "ྋ෩੨༿") { didSet { // ηʔϒσʔλ } } player.levelUp() // didSet ͕ݺ͹ΕͯϢʔβσʔλ͕ηʔϒ͞ΕΔ
 player = Player(name: "ࡩͶͶ") // ϩδοΫΤϥʔɿఆ਺Ͱ͋Δ player.name ͕มΘͬͯ͠·͏ 㨍㩆㏔⹻׳׻⹪⑉㌈ר挵㓱؃ש؆ק׿׻Ŏ

Slide 11

Slide 11 text

΍͸Γ struct ͱ class ͸ ͪΌΜͱ࢖͍෼͚·͠ΐ͏

Slide 12

Slide 12 text

switch instance { case is Data: useStruct() case is Object: useClass() case _: fatalError("ͪΌΜͱߟ͑Ζʂ") } 嗒嵿䥥鰛㏰❔鰘鰟 &CVC鰙1DLGEV鰑鰹
 鱣鱈鱻痓鰉鰴鰛鯵鰹

Slide 13

Slide 13 text

true false ୅ೖ࣌ʹσʔλͷίϐʔͱͯ͠ ୅ೖ͞ΕΔͷ͕͓͔͍͠ class struct ಉҰൺֱʢ===ʣʹҙຯ͕͋Δ ʮϥΠϑαΠΫϧʯ͕ߟ͑ΒΕ Δ ந৅తͳʮຊମʯ͕ߟ͑ΒΕΔ

Slide 14

Slide 14 text

protocol LoverBecomable { } struct Human { var name: String var item: String? var lover: LoverBecomable? init(name: String) { self.name = name } } extension Human: LoverBecomable { } var ktanaka = Human(name: "ాதݡ࣏") var maki = Human(name: "੢໦໺ਅඣ") ktanaka.lover = maki maki.item = "Bikini" (ktanaka.lover as? Human)?.item // nil ktanaka.lover === maki // Error: Binary operator '===' cannot be applied

Slide 15

Slide 15 text

protocol LoverBecomable { } struct Human { var name: String var item: String? var lover: LoverBecomable? init(name: String) { self.name = name } } extension Human: LoverBecomable { } var ktanaka = Human(name: "ాதݡ࣏") var maki = Human(name: "੢໦໺ਅඣ") ktanaka.lover = maki maki.item = "Bikini" (ktanaka.lover as? Human)?.item // nil ktanaka.lover === maki // Error: Binary operator '===' cannot be applied ㌬↛؄؋

Slide 16

Slide 16 text

protocol LoverBecomable { } struct Human { var name: String var item: String? var lover: LoverBecomable? init(name: String) { self.name = name } } extension Human: LoverBecomable { } var ktanaka = Human(name: "ాதݡ࣏") var maki = Human(name: "੢໦໺ਅඣ") ktanaka.lover = maki maki.item = "Bikini" (ktanaka.lover as? Human)?.item // nil ktanaka.lover === maki // Error: Binary operator '===' cannot be applied

Slide 17

Slide 17 text

true false ୅ೖ࣌ʹσʔλͷίϐʔͱͯ͠ ୅ೖ͞ΕΔͷ͕͓͔͍͠ class struct ಉҰൺֱʢ===ʣʹҙຯ͕͋Δ ʮϥΠϑαΠΫϧʯ͕ߟ͑ΒΕ Δ ந৅తͳʮຊମʯ͕ߟ͑ΒΕΔ " " " "

Slide 18

Slide 18 text

protocol LoverBecomable { } struct Human { var name: String var item: String? var lover: LoverBecomable? init(name: String) { self.name = name } } extension Human: LoverBecomable { } var ktanaka = Human(name: "ాதݡ࣏") var maki = Human(name: "੢໦໺ਅඣ") ktanaka.lover = maki maki.item = "Bikini" (ktanaka.lover as? Human)?.item // nil ktanaka.lover === maki // Error: Binary operator '===' cannot be applied

Slide 19

Slide 19 text

protocol LoverBecomable: class { } class Human { var name: String var item: String? weak var lover: LoverBecomable? init(name: String) { self.name = name } } extension Human: LoverBecomable { } let ktanaka = Human(name: "ాதݡ࣏") let maki = Human(name: "੢໦໺ਅඣ") ktanaka.lover = maki maki.item = "Bikini" (ktanaka.lover as? Human)?.item // Bikini ktanaka.lover === maki // true

Slide 20

Slide 20 text

class Human { // … var bankAccounts: [BankAccount] // … } protocol BankAccountDelegate: class { func account(_ account: BankAccount, changePasswordTo newPassword: String) throws -> Int func account(_ account: BankAccount, transfer amount: Int, to receiver: Int) throws } class BankAccount { let id: Int private(set) var hashedPassword: Int private(set) weak var delegate: BankAccountDelegate? // init(id: Int, encryptedPassword: Int, delegate: BankAccountDelegate) { … } func changePassword(to newPassword: String) { do { guard let delegate = self.delegate else { throw NSError() } let newHashedPassword = try delegate.account(self, changePasswordTo: newPassword) self.hashedPassword = newHashedPassword print("ύεϫʔυมߋ੒ޭ") } catch { print("ύεϫʔυมߋࣦഊ") } } func transfer(_ amount: Int, to receiver: Int) { do { guard let delegate = self.delegate else { throw NSError() } try delegate.account(self, transfer: amount, to: receiver) print("ૹۚ੒ޭ") } catch { print("ૹࣦۚഊ") } } }

Slide 21

Slide 21 text

class Human { // … var bankAccounts: [BankAccount] // … } protocol BankAccountDelegate: class { func account(_ account: BankAccount, changePasswordTo newPassword: String) throws -> Int func account(_ account: BankAccount, transfer amount: Int, to receiver: Int) throws } class BankAccount { // … func changePassword(to newPassword: String) { // … } func transfer(_ amount: Int, to receiver: Int) { do { guard let delegate = self.delegate else { throw NSError() } try delegate.account(self, transfor: amount, to: receiver) print("ૹۚ੒ޭ") } catch { print("ૹࣦۚഊ") } } } let tanakaAccount = someBank.issueAccount(password: "1234") ktanaka.bankAccounts.append(tanakaAccount) let amazonAccountID = someBank.issueAccount(password: "J7BJuGy8ks2T").id ktanaka.bankAccounts[0].transfer(1000, to: amazonAccountID) // ૹۚ੒ޭ maki.bankAccounts.append(ktanaka.bankAccounts[0]) maki.bankAccounts[0].transfer(100_000, to: amazonAccountID) // ૹۚ੒ޭ ktanaka.bankAccounts[0].changePassword(to: “5678") maki.bankAccounts[0].transfer(500_000, to: amazonAccountID) // ૹۚ੒ޭʂʁ

Slide 22

Slide 22 text

class Human { // … var bankAccounts: [BankAccount] // … } protocol BankAccountDelegate: class { func account(_ account: BankAccount, changePasswordTo newPassword: String) throws -> Int func account(_ account: BankAccount, transfer amount: Int, to receiver: Int) throws } class BankAccount { // … func changePassword(to newPassword: String) { // … } func transfer(_ amount: Int, to receiver: Int) { do { guard let delegate = self.delegate else { throw NSError() } try delegate.account(self, transfor: amount, to: receiver) print("ૹۚ੒ޭ") } catch { print("ૹࣦۚഊ") } } } let tanakaAccount = someBank.issueAccount(password: "1234") ktanaka.bankAccounts.append(tanakaAccount) let amazonAccountID = someBank.issueAccount(password: "J7BJuGy8ks2T").id ktanaka.bankAccounts[0].transfer(1000, to: amazonAccountID) // ૹۚ੒ޭ maki.bankAccounts.append(ktanaka.bankAccounts[0]) maki.bankAccounts[0].transfer(100_000, to: amazonAccountID) // ૹۚ੒ޭ ktanaka.bankAccounts[0].changePassword(to: “5678") maki.bankAccounts[0].transfer(500_000, to: amazonAccountID) // ૹۚ੒ޭʂʁ ٤ٌڂڋٜ⯪㧕؊㏰❔؄؋

Slide 23

Slide 23 text

class Human { // … var bankAccounts: [BankAccount] // … } protocol BankAccountDelegate: class { func account(_ account: BankAccount, changePasswordTo newPassword: String) throws -> Int func account(_ account: BankAccount, transfer amount: Int, to receiver: Int) throws } class BankAccount { // … func changePassword(to newPassword: String) { // … } func transfer(_ amount: Int, to receiver: Int) { do { guard let delegate = self.delegate else { throw NSError() } try delegate.account(self, transfor: amount, to: receiver) print("ૹۚ੒ޭ") } catch { print("ૹࣦۚഊ") } } } let tanakaAccount = someBank.issueAccount(password: "1234") ktanaka.bankAccounts.append(tanakaAccount) let amazonAccountID = someBank.issueAccount(password: "J7BJuGy8ks2T").id ktanaka.bankAccounts[0].transfer(1000, to: amazonAccountID) // ૹۚ੒ޭ maki.bankAccounts.append(ktanaka.bankAccounts[0]) maki.bankAccounts[0].transfer(100_000, to: amazonAccountID) // ૹۚ੒ޭ ktanaka.bankAccounts[0].changePassword(to: “5678") maki.bankAccounts[0].transfer(500_000, to: amazonAccountID) // ૹۚ੒ޭʂʁ

Slide 24

Slide 24 text

true false ୅ೖ࣌ʹσʔλͷίϐʔͱͯ͠ ୅ೖ͞ΕΔͷ͕͓͔͍͠ class struct ಉҰൺֱʢ===ʣʹҙຯ͕͋Δ ʮϥΠϑαΠΫϧʯ͕ߟ͑ΒΕ Δ ந৅తͳʮຊମʯ͕ߟ͑ΒΕΔ # # # #

Slide 25

Slide 25 text

class Human { // … var bankAccounts: [BankAccount] // … } protocol BankAccountDelegate: class { func account(_ account: BankAccount, changePasswordTo newPassword: String) throws -> Int func account(_ account: BankAccount, transfer amount: Int, to receiver: Int) throws } class BankAccount { // … func changePassword(to newPassword: String) { // … } func transfer(_ amount: Int, to receiver: Int) { do { guard let delegate = self.delegate else { throw NSError() } try delegate.account(self, transfor: amount, to: receiver) print("ૹۚ੒ޭ") } catch { print("ૹࣦۚഊ") } } } let tanakaAccount = someBank.issueAccount(password: "1234") ktanaka.bankAccounts.append(tanakaAccount) let amazonAccountID = someBank.issueAccount(password: "J7BJuGy8ks2T").id ktanaka.bankAccounts[0].transfer(1000, to: amazonAccountID) // ૹۚ੒ޭ maki.bankAccounts.append(ktanaka.bankAccounts[0]) maki.bankAccounts[0].transfer(100_000, to: amazonAccountID) // ૹۚ੒ޭ ktanaka.bankAccounts[0].changePassword(to: “5678") maki.bankAccounts[0].transfer(500_000, to: amazonAccountID) // ૹۚ੒ޭʂʁ

Slide 26

Slide 26 text

class Human { // … var bankAccounts: [BankAccount] // … } protocol BankAccountDelegate: class { func account(_ account: BankAccount, changePasswordTo newPassword: String) throws -> Int func account(_ account: BankAccount, transfer amount: Int, to receiver: Int) throws } struct BankAccount { // … func changePassword(to newPassword: String) { // … } func transfer(_ amount: Int, to receiver: Int) { do { guard let delegate = self.delegate else { throw NSError() } try delegate.account(self, transfor: amount, to: receiver) print("ૹۚ੒ޭ") } catch { print("ૹࣦۚഊ") } } } let tanakaAccount = someBank.issueAccount(password: "1234") ktanaka.bankAccounts.append(tanakaAccount) let amazonAccountID = someBank.issueAccount(password: "J7BJuGy8ks2T").id ktanaka.bankAccounts[0].transfer(1000, to: amazonAccountID) // ૹۚ੒ޭ maki.bankAccounts.append(ktanaka.bankAccounts[0]) maki.bankAccounts[0].transfer(100_000, to: amazonAccountID) // ૹۚ੒ޭ ktanaka.bankAccounts[0].changePassword(to: “5678") maki.bankAccounts[0].transfer(500_000, to: amazonAccountID) // ૹࣦۚഊʂ

Slide 27

Slide 27 text

struct Player { let name: String // ໊લɺมߋෆՄ var level = 1 // ͦͷଞॾʑ init(name: String) { self.name = name } mutating func levelUp() { level += 1 } } var player = Player(name: "ྋ෩੨༿") { didSet { // ηʔϒσʔλ } } player.levelUp() // didSet ͕ݺ͹ΕͯϢʔβσʔλ͕ηʔϒ͞ΕΔ
 player = Player(name: "ࡩͶͶ") // ϩδοΫΤϥʔɿఆ਺Ͱ͋Δ player.name ͕มΘͬͯ͠·͏

Slide 28

Slide 28 text

class Player { struct Status { var level = 1 // ͦͷଞॾʑ mutating func levelUp() { level += 1 } } let name: String // ໊લɺมߋෆՄ var status = Status() { didSet { // ηʔϒσʔλ } } init(name: String) { self.name = name } } let player = Player(name: "ྋ෩੨༿") player.status.levelUp() // status ͷ didSet ͕ݺ͹ΕͯϢʔβσʔλ͕ηʔϒ͞ΕΔ player = Player(name: "ࡩͶͶ") // Τϥʔɿఆ਺Ͱ͋Δ player ͕࠶୅ೖͰ͖ͳ͍ʢ૝ఆ௨Γಈ࡞ʣ

Slide 29

Slide 29 text

㛀źŧ4XJGU8JGFƵĆ -