Swift の表現力を楽しもう!

Swift の表現力を楽しもう!

標準ライブラリ遊び
「5 ... 1」を紐解きながら標準ライブラリを理解していきましょう。

6a55a7fb19c1fa78e86ddca7a69db088?s=128

Yuka Ezura

April 27, 2016
Tweet

Transcript

  1. 3.
  2. 7.

    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
  3. 11.

    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
  4. 14.

    Represents a discrete value in a series QSPUPDPM'PSXBSE*OEFY5ZQF 5 6

    7 … … Equatable
 +
 func successor() -> Self
  5. 16.

    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
  6. 17.

    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 ྫ
  7. 19.

    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
  8. 23.

    let loopRange: Range<LoopIndex> = ͭ͡͸ɻɻ ࣗ࡞ͷܕ 5...2 extension LoopIndex :

    IntegerLiteralConvertible ॳظԽ࣌ʹ
 ੔਺ϦςϥϧΛड͚औΕΔ
  9. 25.

    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 ࣗ࡞ͷܕ
  10. 26.

    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,
  11. 27.

    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,
  12. 32.

    enum Week { case Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,

    Saturday } 8FFL Sunday Monday Tuesday … Saturday 8FFL
  13. 33.

    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
  14. 34.

    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 ࣗ࡞ͷܕ
  15. 35.

    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
  16. 36.

    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
  17. 40.

    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
  18. 41.

    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