Share
Embed
Transcript
43 slides
-
print(self) • ៸໘༑߳ (ezura) • ͖ͳͷ: protocol, enum • τϥΠϑΥʔτۈ
• twitter: @eduraaa -
for element in [1, 2, 3] { /* do something
*/ } [1, 2, 3].forEach { _ in /* do something */ } 1 ... 3 if array.isEmpty { /* do something */ } SequenceType (CollectionType) ForwardIndexType BooleanType Comparable IntegerLiteral Convertible ArrayLiteral Convertible -
for index in 1...5 { print("\(index) times 5 is \(index
* 5)") } let names = ["Anna", "Alex", "Brian", "Jack"] let count = names.count for i in 0..<count { print("Person \(i + 1) is called \(names[i])") } 3BOHF -
Represents a discrete value in a series QSPUPDPM'PSXBSE*OEFY5ZQF 5 6
7 … … Equatable + func successor() -> Self -
enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday } extension Week: ForwardIndexType { // ͜ͷ߹ͷ enum Hashable ʹ҉తʹ४ڌ͠·͢ɻ // protocol Hashable : Equatable ͳͷͰ͢Ͱʹ Equatable ʹ४ڌࡁΈͰ͢ɻ func successor() -> Week { switch self { case .Sunday: return .Monday case .Monday: return .Tuesday case .Tuesday: return .Wednesday case .Wednesday: return .Thursday case .Thursday: return .Friday case .Friday: return .Saturday case .Saturday: return self // ऴ } } } 'PSXBSE*OEFY5ZQF ྫ Sunday Monday Tuesday … Saturday 8FFL -
enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday } extension Week: ForwardIndexType { // ͜ͷ߹ͷ enum Hashable ʹ҉తʹ४ڌ͠·͢ɻ // protocol Hashable : Equatable ͳͷͰ͢Ͱʹ Equatable ʹ४ڌࡁΈͰ͢ɻ func successor() -> Week { switch self { case .Sunday: return .Monday case .Monday: return .Tuesday case .Tuesday: return .Wednesday case .Wednesday: return .Thursday case .Thursday: return .Friday case .Friday: return .Saturday case .Saturday: return self // ऴ } } } Week.Friday == Week.Friday // true (Week.Monday ... .Friday).contains(.Wednesday) // true 'PSXBSE*OEFY5ZQF ྫ -
enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday } extension Week: ForwardIndexType { // ͜ͷ߹ͷ enum Hashable ʹ҉తʹ४ڌ͠·͢ɻ // protocol Hashable : Equatable ͳͷͰ͢Ͱʹ Equatable ʹ४ڌࡁΈͰ͢ɻ func successor() -> Week { switch self { case .Sunday: return .Monday case .Monday: return .Tuesday case .Tuesday: return .Wednesday case .Wednesday: return .Thursday case .Thursday: return .Friday case .Friday: return .Saturday case .Saturday: return .Sunday // ࠷ॳʹΔ } } } (Week.Saturday ... .Monday).contains(.Sunday) // true 'PSXBSE*OEFY5ZQF ྫ Sunday Monday Tuesday … Saturday -
let loopRange: Range<LoopIndex> = ͭ͡ɻɻ ࣗ࡞ͷܕ 5...2 extension LoopIndex :
IntegerLiteralConvertible ॳظԽ࣌ʹ ϦςϥϧΛड͚औΕΔ -
struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {
self.rawValue = rawValue } } // OK let loopIndex = LoopIndex(1) // ௨ৗͰ͖ͳ͍ // error: cannot convert value of type 'Int' to specified type 'LoopIndex' let beforeConform: LoopIndex = 5 -PPQ*OEFY ࣗ࡞ͷܕ -
struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {
self.rawValue = rawValue } } extension LoopIndex: IntegerLiteralConvertible { init(integerLiteral value: IntegerLiteralType) { self.init(value) } } let loopIndex: LoopIndex = 1 -PPQ*OEFY*OUFHFS-JUFSBM$POWFSUJCMF 0, -
struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {
self.rawValue = rawValue } } extension LoopIndex: IntegerLiteralConvertible { init(integerLiteral value: IntegerLiteralType) { self.init(value) } } extension LoopIndex: ForwardIndexType { // ུ } let loopIndex: LoopIndex = 1 let loopRange : Range<LoopIndex> = 5...2 loopRange.contains(1) // true -PPQ*OEFY*OUFHFS-JUFSBM$POWFSUJCMF 0, -
enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday } 8FFL Sunday Monday Tuesday … Saturday 8FFL -
enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday } extension Week: ForwardIndexType { // ͜ͷ߹ͷ enum Hashable ʹ҉తʹ४ڌ͠·͢ɻ // protocol Hashable : Equatable ͳͷͰ͢Ͱʹ Equatable ʹ४ڌࡁΈͰ͢ɻ func successor() -> Week { switch self { case .Sunday: return .Monday case .Monday: return .Tuesday case .Tuesday: return .Wednesday case .Wednesday: return .Thursday case .Thursday: return .Friday case .Friday: return .Saturday case .Saturday: return self // ऴ } } } 8FFL'PSXBSE*OEFY5ZQF Sunday Monday Tuesday … Saturday 8FFL -
struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {
self.rawValue = rawValue } } // OK let loopIndex = LoopIndex(1) // ௨ৗͰ͖ͳ͍ // error: cannot convert value of type 'Int' to specified type 'LoopIndex' let beforeConform: LoopIndex = 5 -PPQ*OEFY ࣗ࡞ͷܕ -
struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {
self.rawValue = rawValue } } extension LoopIndex: IntegerLiteralConvertible { init(integerLiteral value: IntegerLiteralType) { self.init(value) } } let loopIndex: LoopIndex = 1 -PPQ*OEFY*OUFHFS-JUFSBM$POWFSUJCMF -PPQ*OEFY 1 -
struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {
self.rawValue = rawValue } } extension LoopIndex: IntegerLiteralConvertible { init(integerLiteral value: IntegerLiteralType) { self.init(value) } } let loopIndex: LoopIndex = 1 -PPQ*OEFY*OUFHFS-JUFSBM$POWFSUJCMF -PPQ*OEFY 1 -
enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday } extension Week: ForwardIndexType { // ͜ͷ߹ͷ enum Hashable ʹ҉తʹ४ڌ͠·͢ɻ // protocol Hashable : Equatable ͳͷͰ͢Ͱʹ Equatable ʹ४ڌࡁΈͰ͢ɻ func successor() -> Week { switch self { case .Sunday: return .Monday case .Monday: return .Tuesday case .Tuesday: return .Wednesday case .Wednesday: return .Thursday case .Thursday: return .Friday case .Friday: return .Saturday case .Saturday: return self // ऴ } } } 8FFL'PSXBSE*OEFY5ZQF Sunday Monday Tuesday … Saturday 8FFL -
struct LoopIndex { let rawValue: Int init(_ rawValue: Int) {
self.rawValue = rawValue } } extension LoopIndex: IntegerLiteralConvertible { init(integerLiteral value: IntegerLiteralType) { self.init(value) } } let loopIndex: LoopIndex = 1 -PPQ*OEFY*OUFHFS-JUFSBM$POWFSUJCMF -PPQ*OEFY 1 -
ࢀߟ w 4XJGU4UBOEBSE-JCSBSZ3FGFSFODF IUUQTEFWFMPQFSBQQMFDPNMJCSBSZJPT EPDVNFOUBUJPO(FOFSBM3FGFSFODF 4XJGU4UBOEBSE-JCSBSZ3FGFSFODF w 4XJGU%PDPSH IUUQTXJGUEPDPSHWQSPUPDPM'PSXBSE*OEFY5ZQF w
Ϧςϥϧͱܕͷ IUUQXXXTMJEFTIBSFOFUUPNPIJSPLVNBHBJ TXJGU