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

Daniel Steinberg: The Open World of Swift 3

Realm
April 25, 2016

Daniel Steinberg: The Open World of Swift 3

Realm

April 25, 2016
Tweet

More Decks by Realm

Other Decks in Programming

Transcript

  1. class MyClass { private var myProperty = "Hello" private func

    myPrivateMethod() { print(myProperty) } fileprivate func myFilePrivateMethod(numberOfTimes times: Int) { for _ in 1 ... times { myPrivateMethod() } } } extension MyClass { func myExtensionMethod(numberOfTimes times: Int) { myInternalMethod(numberOfTimes: times) } }
  2. class MyClass { private var myProperty = "Hello" private func

    myPrivateMethod() { print(myProperty) } fileprivate func myFilePrivateMethod(numberOfTimes times: Int) { for _ in 1 ... times { myPrivateMethod() } } } extension MyClass { func myExtensionMethod(numberOfTimes times: Int) { myInternalMethod(numberOfTimes: times) } }
  3. class MyClass { private var myProperty = "Hello" private func

    myPrivateMethod() { print(myProperty) } fileprivate func myFilePrivateMethod(numberOfTimes times: Int) { for _ in 1 ... times { myPrivateMethod() } } } extension MyClass { func myExtensionMethod(numberOfTimes times: Int) { myInternalMethod(numberOfTimes: times) } }
  4. class MyClass { private var myProperty = "Hello" private func

    myPrivateMethod() { print(myProperty) } fileprivate func myFilePrivateMethod(numberOfTimes times: Int) { for _ in 1 ... times { myPrivateMethod() } } } extension MyClass { func myExtensionMethod(numberOfTimes times: Int) { myInternalMethod(numberOfTimes: times) } }
  5. class MyClass { private var myProperty = "Hello" private func

    myPrivateMethod() { print(myProperty) } fileprivate func myFilePrivateMethod(numberOfTimes times: Int) { for _ in 1 ... times { myPrivateMethod() } } } extension MyClass { func myExtensionMethod(numberOfTimes times: Int) { myInternalMethod(numberOfTimes: times) } }
  6. func double(input: Int) -> Int { var localInput = input

    localInput = localInput * 2 return localInput }
  7. func double(input: Int) -> Int { var localInput = input

    localInput = localInput * 2 return localInput }
  8. func double(input: Int) -> Int { var input = input

    input = input * 2 return input } Name Shadowing
  9. func double(input: Int) -> Int { var input = input

    input = input * 2 return input }
  10. func noEscape(@noescape f: () -> ()) {} func example(inout x:

    Int) { noEscape { _ = x } } safe because @noescape => closure can't be called after function returns
  11. func escape(f: () -> ()) {} func example(inout x: Int)

    { escape {[x] in _ = x } } [x] is a capture list a constant is initialized to have the value of x
  12. func curried(x: Int)(y: Int) -> Int { return {(y: Int)

    -> Int in return x * y } } curried(7)(8)
  13. func curried(x: Int)(y: Int) -> Int { return {(y: Int)

    -> Int in return x * y } } curried(7)(8)
  14. func curried(x: Int)(y: Int) -> Int { return {(y: Int)

    -> Int in return x * y } } curried(7)(8)
  15. func curried(x: Int)(y: Int) -> Int { return {(y: Int)

    -> Int in return x * y } } curried(7)(8)
  16. func curried(x: Int)(y: Int) -> Int { return {(y: Int)

    -> Int in return x * y } } curried(7)(8) 7 * y
  17. func curried(x: Int)(y: Int) -> Int { return {(y: Int)

    -> Int in return x * y } } curried(7)(8) 7 * 8
  18. func curried(x: Int)(y: Int) -> Int { return {(y: Int)

    -> Int in return x * y } } curried(7)(8)
  19. func curried(x: Int)(y: Int) -> Int { return {(y: Int)

    -> Int in return x * y } } curried(7)(8)
  20. func curried(x: Int)(y: Int) -> Int { return {(y: Int)

    -> Int in return x * y } } curried(7)(8)
  21. func curried(x: Int) -> (y: Int) -> Int { return

    {(y: Int) -> Int in return x * y } } curried(7)(8)
  22. func curried(x: Int) -> (y: Int) -> Int { return

    {(y: Int) -> Int in return x * y } } curried(7)(8)
  23. func curried(x: Int) -> (y: Int) -> Int { return

    {(y: Int) -> Int in return x * y } } curried(7)(8)
  24. func curried(x: Int) -> (y: Int) -> Int { return

    {(y: Int) -> Int in return x * y } } curried(7)(8)
  25. func curried(x: Int) -> (y: Int) -> Int { return

    {(y: Int) -> Int in return x * y } } curried(7)(8)
  26. class MyClass : NSObject { func callbackMethod(with notification: NSNotification){} func

    setNotification() { let center = NSNotificationCenter.defaultCenter() center .addObserver(self, selector: "callbackMethod", name: NSApplicationWillResignActiveNotification, object: NSApplication.sharedApplication()) } }
  27. class MyClass : NSObject { func callbackMethod(with notification: NSNotification){} func

    setNotification() { let center = NSNotificationCenter.defaultCenter() center .addObserver(self, selector: "callbackMethod", name: NSApplicationWillResignActiveNotification, object: NSApplication.sharedApplication()) } }
  28. class MyClass : NSObject { func callbackMethod(with notification: NSNotification){} func

    setNotification() { let center = NSNotificationCenter.defaultCenter() center .addObserver(self, selector: "callbackMethod", name: NSApplicationWillResignActiveNotification, object: NSApplication.sharedApplication()) } }
  29. class MyClass : NSObject { func callbackMethod(with notification: NSNotification){} func

    setNotification() { let center = NSNotificationCenter.defaultCenter() center .addObserver(self, selector: "callbackMethod", name: NSApplicationWillResignActiveNotification, object: NSApplication.sharedApplication()) } }
  30. class MyClass : NSObject { func callbackMethod(with notification: NSNotification){} func

    setNotification() { let center = NSNotificationCenter.defaultCenter() center .addObserver(self, selector: "callbackMethod", name: NSApplicationWillResignActiveNotification, object: NSApplication.sharedApplication()) } }
  31. class MyClass : NSObject { func callbackMethod(with notification: NSNotification){} func

    setNotification() { let center = NSNotificationCenter.defaultCenter() center .addObserver(self, selector: "callbackMethod", name: NSApplicationWillResignActiveNotification, object: NSApplication.sharedApplication()) } }
  32. class MyClass : NSObject { func callbackMethod(with notification: NSNotification){} func

    setNotification() { let center = NSNotificationCenter.defaultCenter() center .addObserver(self, selector: #selector(callbackMethod), name: NSApplicationWillResignActiveNotification, object: NSApplication.sharedApplication()) } }
  33. class MyClass : NSObject { func callbackMethod(with notification: NSNotification){} func

    setNotification() { let center = NSNotificationCenter.defaultCenter() center .addObserver(self, selector: #selector(MyClass.callbackMethod), name: NSApplicationWillResignActiveNotification, object: NSApplication.sharedApplication()) } }
  34. class MyClass : NSObject { func callbackMethod(){} func callbackMethod(with notification:

    NSNotification){} func setNotification() { let center = NSNotificationCenter.defaultCenter() center .addObserver(self, selector: #selector(MyClass.callbackMethod), name: NSApplicationWillResignActiveNotification, object: NSApplication.sharedApplication()) } }
  35. class MyClass : NSObject { func callbackMethod(){} func callbackMethod(with notification:

    NSNotification){} func setNotification() { let center = NSNotificationCenter.defaultCenter() center .addObserver(self, selector: #selector(MyClass.callbackMethod), name: NSApplicationWillResignActiveNotification, object: NSApplication.sharedApplication()) } }
  36. class MyClass : NSObject { func callbackMethod(){} func callbackMethod(with notification:

    NSNotification){} func setNotification() { let center = NSNotificationCenter.defaultCenter() center .addObserver(self, selector: #selector(MyClass.callbackMethod(with:)), name: NSApplicationWillResignActiveNotification, object: NSApplication.sharedApplication()) } }
  37. HK_EXTERN NSString * const HKQuantityTypeIdentifierBodyMassIndex; HK_EXTERN NSString * const HKQuantityTypeIdentifierBodyFatPercentage;

    HK_EXTERN NSString * const HKQuantityTypeIdentifierHeight; HK_EXTERN NSString * const HKQuantityTypeIdentifierBodyMass; HK_EXTERN NSString * const HKQuantityTypeIdentifierLeanBodyMass;
  38. HK_EXTERN NSString * const HKQuantityTypeIdentifierBodyMassIndex; HK_EXTERN NSString * const HKQuantityTypeIdentifierBodyFatPercentage;

    HK_EXTERN NSString * const HKQuantityTypeIdentifierHeight; HK_EXTERN NSString * const HKQuantityTypeIdentifierBodyMass; HK_EXTERN NSString * const HKQuantityTypeIdentifierLeanBodyMass;
  39. HK_EXTERN NSString * const HKQuantityTypeIdentifierBodyMassIndex; HK_EXTERN NSString * const HKQuantityTypeIdentifierBodyFatPercentage;

    HK_EXTERN NSString * const HKQuantityTypeIdentifierHeight; HK_EXTERN NSString * const HKQuantityTypeIdentifierBodyMass; HK_EXTERN NSString * const HKQuantityTypeIdentifierLeanBodyMass;
  40. enum Currency { case dollars case euros case pounds case

    yen var symbol: String { switch self { case dollars: return "$" default: return "I don't know" } } }
  41. enum Currency { case dollars case euros case pounds case

    yen var symbol: String { switch self { case .dollars: return "$" default: return "I don't know" } } }
  42. enum Currency { case dollars case euros case pounds case

    yen var symbol: String { switch self { case dollars: return "$" default: return "I don't know" } } }
  43. enum Currency { case dollars case euros case pounds case

    yen var symbol: String { switch self { case .dollars: return "$" default: return "I don't know" } } }
  44. enum Currency { case dollars case euros case pounds case

    yen var symbol: String { switch self { case .dollars: return "$" default: return "I don't know" } } }
  45. 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 }
  46. 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 }
  47. 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 }
  48. 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 }
  49. 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 }
  50. 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 }
  51. 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 }
  52. struct Friend { let name: String let location: String func

    nameBadge() { print("I'm", name, "from", location) } }
  53. struct Friend { let name: String let location: String func

    nameBadge() { print("I'm", name, "from", location) } }
  54. struct Friend { let name: String let location: String func

    nameBadge() { print("I'm", name, "from", location) } }
  55. struct Friend { let name: String let location: String func

    nameBadge() { print("I'm", self.name, "from", self.location) } }
  56. struct Friend { let name: String let location: String func

    nameBadge() { print("I'm", self.name, "from", self.location) } } Require self for accessing instance members REJECTED
  57. protocol Prot { typealias Container : SequenceType } extension Prot

    { typealias Element = Container.Generator.Element }
  58. protocol Prot { typealias Container : SequenceType } extension Prot

    { typealias Element = Container.Generator.Element }
  59. protocol Prot { associatedtype Container : SequenceType } extension Prot

    { typealias Element = Container.Generator.Element }