swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

55

Upload: tomohiro-kumagai

Post on 19-Mar-2017

755 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 2: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 3: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 4: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 5: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 6: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 7: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct Person { init(name: String, number: Int) { }

}

Page 8: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct Person { var name: String var number: Int init(name: String, number: Int) { self.name = name

} }

Page 9: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 10: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct Person { init(name: String, number: Int) { guard name.isVaidAsPersionName() else { fatalError("Invalid name") }

:

Page 11: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

init? init!

nil

struct Person { init?(name: String, number: Int) { guard name.isVaidAsPersionName() else { return nil }

:

Page 12: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

init … throws

struct Person { init(name: String, number: Int) throws { guard name.isVaidAsPersionName() else { throw InitializeError.invalidName }

:

Page 13: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

class Person { var name: String var number: Int init?(name: String, number: Int) { guard name.isVaidAsPersionName() else { return nil }

Page 14: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 15: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct Person { init(name: String, number: Int) {

}

init(havingName: String, andNumber: Int) {

}

Page 16: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct Number { init( _ string: String, radix: Int) {

}

Page 17: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct WebSite { init(url: String, baseAddress: URL? = nil) {

}

Page 18: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 19: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 20: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct Person { var name: String var number: Int init(name: String) { self.name = name self.number = getNextNumber()

}

Page 21: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct Person { var name: String var number: Int }

// イニシャライザーを定義しなくても使える let someone = Person(name: "Kumagai", number: 2)

Page 22: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct Person { var name: String var number: Int }

extension Person {

init(name: String) { self.init(name: name, number: getNextNumber()) }

Page 23: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 24: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

class Base { init(name: String, number: Int) { } }

Page 25: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

class Base { convenience init(name: String) { self.init(name: name, number: getNextNumber()) } }

Page 26: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

class Base { required init(name: String, number: Int) { } }

Page 27: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

extension Object {

convenience init(a: Int) { … }

init(b: Int) { … }

required nit(c: Int) { … }

Page 28: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 29: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

class View { var textColor = NSColor.black var backgroundColor = NSColor.white init() { }

}

Page 30: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

class Object { var value = Value()

init() { value = Value() } }

Page 31: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct Data { var value = Value()

}

let data = Data(value: Value())

Page 32: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 33: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct Person {

var name: String var number: Int init(name: String) {

self.name = name self.number = getNextNumber() } }

Page 34: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct Person {

var name: String var number: Int }

extension Persion {

init(name: String) {

self.name = name self.number = getNextNumber() }

Page 35: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct RandomGenerator { var seed1: UInt32 = arc4random() var seed2: UInt32 = arc4random() }

let gen1 = RandomGenerator() let gen2 = RandomGenerator(seed1: 0, seed2: 1)

Page 36: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 37: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 38: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

public struct MyData { public var value = MyValue()

public init(value: MyValue) { self.value = value } }

Page 39: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

public struct MyData { public var value = MyValue() }

extension MyData { public init(memberwiseWithValue value: MyValue) { self.init(value: value) } }

Page 40: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

public struct MyData { public var value: MyValue

public init(value: MyValue) { self.value = value } }

Page 41: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 42: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

let speaker = Person("Kumagai", 1234)

Page 43: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

let h = Person.init(name: "Kumagai", number: 1234)

// 関数型変数 f にイニシャライザーを入れて使える let f: (String,Int) -> Person = Person.init(name:number:)

let someone = f("Kumagai", 1234)

Page 44: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

// 取り得る型から推論する

let list = MemberList(persons: .init(name: "A"))

// 関数内で、戻り値の型から推論する

func makePerson(name: String) -> Person { return .init(name: name)

}

Page 45: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 46: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

protocol ExpressibleByName { init(name: String)

}

Page 47: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

// 値型をプロトコルに適合させるとき struct Person : ExpressibleByName { init(name: String) { … } } // 参照型をプロトコルに適合させるとき class Thread : ExpressibleByName { required init(name: String) { … } }

Page 48: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

// 値型はプロトコルに適合できる extension Person : ExpressibleByName { init(name: String) { … } } // 参照型はプロトコルに適合できない extension Thread : ExpressibleByName { required init(name: String) { … } }

Page 49: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 50: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

struct Value { init() { … } }

// メタタイプ型の変数に保存して … let type: Value.Type = Value.self

// そこからインスタンスを生成できる let instance: Value = type.init()

型.self

init

Page 51: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

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)

Page 52: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe

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: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 54: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
Page 55: Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe