Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift3.0の破壊的メジャーアップデートの内容をみてみよう

 Swift3.0の破壊的メジャーアップデートの内容をみてみよう

【朝活】iOS開発会議62回目in 株式会社トライフォート
https://www.facebook.com/events/269576500054423/
の発表内容

Swift3.0のアップデート内容を共有しました。

Sato Takeshi

May 19, 2016
Tweet

More Decks by Sato Takeshi

Other Decks in Technology

Transcript

  1. Өڹ౓େͳมߋ5બ • SE-0005 • SE-0044 • SE-0055 • SE-0057 •

    SE-0072 Swiftϓϩάϥϛϯάษڧձ 2016/5/19 19
  2. SE-0005 Objective-C APIΛSwiftʹม׵͢Δ ΑΓྑ͍ํ๏ • Objective-CͷAPIΛSwiftʹΠϯϙʔτ͢Δ͍͞ͷ໋໊نଇΛมߋ͢ΔϚο νϯάํ๏Λهࡌ • Objecitve-CϑϨʔϜϫʔΫΛ࢖༻ͨ͋͠Γͱ͋ΒΏΔطଘͷSwiftίʔυʹ ӨڹΛ༩͑Δɻ

    • ͪͳΈʹFoundation͕Objective-CΛݺΜͰ͍ΔͷͰطଘίʔυશ͕ͯର ৅ʂ • Swift2ͷίʔυ͔ΒSwift3΁ม׵͢ΔϚΠάϨʔγϣϯπʔϧ͕ඞཁ Swiftϓϩάϥϛϯάษڧձ 2016/5/19 20
  3. ྫ͑͹ɺSwift2.2·Ͱ͸͜Μͳίʔυ class UIBezierPath : NSObject, NSCopying, NSCoding { convenience init(ovalInRect:

    CGRect) func moveToPoint(_: CGPoint) func addLineToPoint(_: CGPoint) func addCurveToPoint(_: CGPoint, controlPoint1: CGPoint, controlPoint2: CGPoint) func addQuadCurveToPoint(_: CGPoint, controlPoint: CGPoint) func appendPath(_: UIBezierPath) func bezierPathByReversingPath() -> UIBezierPath func applyTransform(_: CGAffineTransform) var empty: Bool { get } func containsPoint(_: CGPoint) -> Bool func fillWithBlendMode(_: CGBlendMode, alpha: CGFloat) func strokeWithBlendMode(_: CGBlendMode, alpha: CGFloat) func copyWithZone(_: NSZone) -> AnyObject func encodeWithCoder(_: NSCoder) } Swiftϓϩάϥϛϯάษڧձ 2016/5/19 21
  4. Swift3.0͸͜ΜͳίʔυʹͳΔ class UIBezierPath : NSObject, NSCopying, NSCoding { convenience init(ovalIn

    rect: CGRect) func move(to point: CGPoint) func addLine(to point: CGPoint) func addCurve(to endPoint: CGPoint, controlPoint1 controlPoint1: CGPoint, controlPoint2 controlPoint2: CGPoint) func addQuadCurve(to endPoint: CGPoint, controlPoint controlPoint: CGPoint) func append(_ bezierPath: UIBezierPath) func reversing() -> UIBezierPath func apply(_ transform: CGAffineTransform) var isEmpty: Bool { get } func contains(_ point: CGPoint) -> Bool func fill(_ blendMode: CGBlendMode, alpha alpha: CGFloat) func stroke(_ blendMode: CGBlendMode, alpha alpha: CGFloat) func copy(with zone: NSZone = nil) -> AnyObject func encode(with aCoder: NSCoder) } Swiftϓϩάϥϛϯάษڧձ 2016/5/19 22
  5. ྫ͑͹Core Graphics C API: Swift2.2·Ͱ override func drawRect(rect: CGRect) {

    let context: CGContext = UIGraphicsGetCurrentContext()! let toCenter = CGPoint(x: bounds.width/2.0, y: bounds.height/2.0) let angle = CGFloat(M_PI / 16) var transform = CGAffineTransformIdentity for _ in 0..<32 { triangulateRect(bounds, inputTransform: transform, context: context) transform = CGAffineTransformTranslate(transform, toCenter.x, toCenter.y) transform = CGAffineTransformRotate(transform, angle) transform = CGAffineTransformTranslate(transform, -toCenter.x, -toCenter.y) } CGContextSetLineWidth(context, bounds.size.width / 100) CGContextSetGrayStrokeColor(context, 0.5, 1.0) CGContextDrawPath(context, .Stroke) } Swiftϓϩάϥϛϯάษڧձ 2016/5/19 24
  6. ྫ͑͹Core Graphics C API: Swift3.0͔Β override func drawRect(rect: CGRect) {

    let context: CGContext = UIGraphicsGetCurrentContext()! let toCenter = CGPoint(x: bounds.width/2.0, y: bounds.height/2.0) let angle = CGFloat(M_PI / 16) var transform = CGAffineTransform.identity for _ in 0..<32 { triangulateRect(bounds, inputTransform: transform, context: context) transform = transform.translate(toX: toCenter.x, toY: toCenter.y) .rotate(angle: angle) .translate(toX: -toCenter.x, toY: -toCenter.y) } context.lineWidth = bounds.size.width / 100 context.strokeColor = CGColor(gray: 0.5, alpha: 1.0) context.drawPath(mode: .Stroke) } Swiftϓϩάϥϛϯάษڧձ 2016/5/19 25
  7. SE-0057 Objecitive-CʹܰྔδΣωϦΫεΛΠ ϯϙʔτ͢Δ • Objective-CͷܰྔδΣωϦΫεػೳΛಋೖͯ͠ɺίϨΫγϣϯܕΛѻ͏࣌ ͷObjevecive-CͷΫϥεΛSwiftʹΠϯϙʔτͨ͠ࡍʹͦͷ··ͷܕͰΠϯ ϙʔτͰ͖ΔΑ͏ʹ͢Δ • ݱঢ়͸AnyObjectʹͳ͍ͬͯͨɻ •

    NSArray<NSString *> * ΛΠϯϙʔτͨ͠ࡍʹ[String]ʹͳΔΑ͏ʹͳΔɻ • Ҿ਺ͳ͠ͰObjective-CͷίϨΫγϣϯܕΛఆ͍ٛͯ͠Δίʔυ͸ӨڹΛड ͚Δɻ Swiftϓϩάϥϛϯάษڧձ 2016/5/19 27
  8. ӨڹΛड͚Δίʔυ let array: NSArray = ["hello", "world"] // okay, infer

    NSArray<NSString> // old var mutArray = NSMutableArray() // error: need type arguments for NSMutableArray Swiftϓϩάϥϛϯάษڧձ 2016/5/19 28
  9. SE-0002: ؔ਺ఆٛͰͷΧϦʔԽจ๏ͷ࡟আ Θ͔Γʹ͍͘ΜͰ࡟আ // Before: func curried(x: Int)(y: String) ->

    Float { return Float(x) + Float(y)! } // After: func curried(x: Int) -> (String) -> Float { return {(y: String) -> Float in return Float(x) + Float(y)! } } Swiftϓϩάϥϛϯάษڧձ 2016/5/19 31
  10. SE-0003: ؔ਺ͷҾ਺ͰͷvarΛ࡟আ ؔ਺Ҿ਺ͰvarΛఆٛͯ͠Ҿ਺มߋ͕Ͱ͖ͳ͘ͳΔ //for swift2.2 func foo(var i: Int) {

    i += 1 // OK, but the caller cannot observe this mutation. } //for swift3.0 func foo(i: Int) { i += 1 // illegal } Swiftϓϩάϥϛϯάษڧձ 2016/5/19 32
  11. SE-0053: ؔ਺ͷҾ਺Ͱͷ໌ࣔతletΛ ࡟আ ؔ਺ͷҾ਺͸มߋͰ͖ͳ͘ͳΔ func foo(let x: Int) { ...

    }// not work! func foo(x: Int) { ... }// fine! Swiftϓϩάϥϛϯάษڧձ 2016/5/19 33
  12. SE-0007: CݴޠελΠϧͷforจͷ࡟ আ Cݴޠͷforจ[ྫʀfor(var i = 1; i < 10;

    i++ʣ]͸Swiftͬ Ά͘ͳ͍ͷͰ࡟আ for-inจΛ࢖͓͏ Swiftϓϩάϥϛϯάษڧձ 2016/5/19 35
  13. SE-0043: caseจʹ͓͚Δෳ਺ύλʔϯΛಉ࣌ ʹຬͨ͢ม਺Λఆٛ enum MyEnum { case Case1(Int,Float) case Case2(Float,Int)

    } switch value { case let .Case1(x, 2), let .Case2(2, x): print(x) case .Case1, .Case2: break } Swiftϓϩάϥϛϯάษڧձ 2016/5/19 38
  14. SE-0025: ΞΫηε੍ݶͷείʔϓ ͍··Ͱprivate͕ϑΝΠϧ୯ҐͷΞΫηε੍ݶͩͬͨͷΛείʔϓ୯Ґʹมߋ͠· ͢ʂ ΞΫηε੍ݶͷम০ࢠ͸ҎԼͷ̐ͭʹͳΓ·͢ • public: ݱࡏͷϞδϡʔϧͷ֎Ͱ΋ݟ͑Δम০ࢠ • internal:

    ݱࡏͷϞδϡʔϧͷதͰݟ͑Δम০ࢠ • fileprivate: ݱࡏͷϑΝΠϧ಺Ͱݟ͑Δम০ࢠ • private: ݱࡏͷείʔϓ಺ͷΈʹݟ͑Δम০ࢠ private͕ϑΝΠϧ୯ҐͰ͸ͳ͘είʔϓ୯ҐʹͳΔʂ Swiftϓϩάϥϛϯάษڧձ 2016/5/19 40
  15. ίʔυྫ class A { private var counter = 0 //

    internalͷύϒϦοΫAPI func incrementCount() { ++counter } // ͜ͷείʔϓ͔Β֎͔Βݟ͑ͳ͍API private func advanceCount(dx: Int) { counter += dx } // incrementTwice() is not visible here } extension A { // counterϓϩύςΟ͸͔͜͜Β͸ݟ͑ͳ͍ɻ // advanceCount() ϝιου΋͔͜͜Β͸ݟ͑ͳ͍ // extension಺Ͱଞ͔Β͸ݟ͑ͳ͍ϝιουΛ࣮૷͢Δͷ͸༗ӹͳ৔߹͕͋Δɻ // incrementTwice()ϝιου͸͜ͷextensionΑΓ֎Ͱ͸ίʔυิ׬͕͞Εͳ͍ private func incrementTwice() { incrementCount() incrementCount() } } Swiftϓϩάϥϛϯάษڧձ 2016/5/19 41
  16. ྫ͑͹HealthKit identifiers͸Objecitve-CͰ ͸͜Μͳײ͡ʹఆٛͯ͠ typedef NSString * HKQuantityTypeIdentifier __attribute__((swift_wrapper(enum)); HK_EXTERN HKQuantityTypeIdentifier

    const HKQuantityTypeIdentifierBodyMassIndex; HK_EXTERN HKQuantityTypeIdentifier const HKQuantityTypeIdentifierBodyFatPercentage; HK_EXTERN HKQuantityTypeIdentifier const HKQuantityTypeIdentifierHeight; HK_EXTERN HKQuantityTypeIdentifier const HKQuantityTypeIdentifierBodyMass; HK_EXTERN HKQuantityTypeIdentifier const HKQuantityTypeIdentifierLeanBodyMass; Swiftϓϩάϥϛϯάษڧձ 2016/5/19 45
  17. Swiftଆ͸͜Μͳײ͡ʹenumͰม׵͞ΕΔ enum HKQuantityTypeIdentifier : String { case BodyMassIndex case BodyFatPercentage

    case Height case BodyMass case LeanBodyMass } Swiftϓϩάϥϛϯάษڧձ 2016/5/19 46
  18. NSError͸͜Μͳײ͡ʹObjective-Cʹఆٛ͞Ε ͍ͯΔͱ͖ typedef NSString * NSErrorDomain __attribute__((swift_wrapper(struct))); FOUNDATION_EXPORT NSErrorDomain const

    NSCocoaErrorDomain; FOUNDATION_EXPORT NSErrorDomain const NSPOSIXErrorDomain; FOUNDATION_EXPORT NSErrorDomain const NSOSStatusErrorDomain; FOUNDATION_EXPORT NSErrorDomain const NSMachErrorDomain; Swiftϓϩάϥϛϯάษڧձ 2016/5/19 47
  19. Swiftଆ͸͜Μͳײ͡ʹstructʹม׵͞ΕΔ struct NSErrorDomain : RawRepresentable { typealias RawValue = String

    init(rawValue: RawValue) var rawValue: RawValue { get } static var Cocoa: NSErrorDomain { get } static var POSIX: NSErrorDomain { get } static var OSStatus: NSErrorDomain { get } static var Mach: NSErrorDomain { get } } Swiftϓϩάϥϛϯάษڧձ 2016/5/19 48
  20. SE-0064: Objective-Cͷήολʔηολʔϓϩ ύςΟΛηϨΫλʔͰࢀর͢Δ #selectorͰObjective-Cͷgetterɺsetter͕ࢀরͰ͖ΔΑ͏ʹ͢Δ class Person: NSObject { dynamic var

    firstName: String dynamic let lastName: String dynamic var fullName: String { return "\(firstName) \(lastName)" } init(firstName: String, lastName: String) { self.firstName = firstName self.lastName = lastName } } let firstNameGetter = #selector(getter: Person.firstName) let firstNameSetter = #selector(setter: Person.firstName) Swiftϓϩάϥϛϯάษڧձ 2016/5/19 49
  21. SE-0070: Make Optional Requirements Objective-C-only • Objevictive-Cͷϓϩτίϧ͸࣮૷ͯ͠΋͠ͳͯ͘΋Α͍ optionalम০ࢠ͕͚ͭΒΕΔɻ • Ͱ΋Swiftͷϓϩτίϧʹoptional͸͚ͭͨ͘ͳ͍ɻ

    • ͳͷͰɺOptionalम০ࢠΛObjective-CͷΈʹݶఆ͠·͢ɻ • optionalम০ࢠͷલʹ͸ඞͣ@objcम০ࢠΛ͚ͭΔΑ͏ʹ͠· ͢ɻ Swiftϓϩάϥϛϯάษڧձ 2016/5/19 51
  22. ྫ @objc protocol NSTableViewDelegate { @objc optional func tableView(_: NSTableView,

    viewFor: NSTableColumn, row: Int) -> NSView? // correct optional func tableView(_: NSTableView, heightOfRow: Int) -> CGFloat // error: 'optional' requirements are an Objective-C compatibility feature; add '@objc' } Swiftϓϩάϥϛϯάษڧձ 2016/5/19 52