A Swift Approach (Warsaw 2014)

D200a17dd269fd4001bacb11662dab4b?s=47 Kyle Fuller
November 27, 2014

A Swift Approach (Warsaw 2014)

D200a17dd269fd4001bacb11662dab4b?s=128

Kyle Fuller

November 27, 2014
Tweet

Transcript

  1. A SWIFT APPROACH KYLEFULLER

  2. KYLEFULLER

  3. None
  4. None
  5. NEW CONCEPTS

  6. RUNTIME

  7. @objc / NSObject

  8. NSKeyValueObserving

  9. RUNTIME

  10. WWDC 2015

  11. OPTIONALS?

  12. func compute() -> Bool?

  13. true false neither

  14. LACK OF A VALUE

  15. NOT AN EMPTY VALUE

  16. CAN'T WE DO THIS IN OBJECTIVE-C?

  17. nil Nil NULL CGRectNull -1 0 NSNotFound NSNull ...

  18. EXPRESS YOU DON'T ACCEPT NIL

  19. EXPRESS YOU WILL NOT RETURN NIL

  20. /// You **MUST** pass in a URL - (instancetype)initWithURL:(NSURL *)URL

    { NSParameterAssert(URL != nil); }
  21. - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)path { return nil; }

  22. None
  23. RUNTIME ERROR

  24. RUNTIME ERROR

  25. COMPILE ERROR

  26. None
  27. TUPLES

  28. func get(username:String) -> (Person?, NSError?)

  29. let person, error = get("Kyle")

  30. OBJECTIVE-C

  31. NSError *error; Person *person = [Person getPerson:@"Kyle" error:&error];

  32. NAMED TUPLES

  33. func get(username:String) -> (person:Person?, error:NSError?)

  34. let result = get("Kyle") result.person result.error

  35. - (NSArray *)getPerson:(NSString *)username; - (NSDictionary *)getPerson:(NSString *)username;

  36. NSDictionary *components = [Person getPerson:@"kyle"]; Person *person = components["person"]; NSError

    *error = components["error"];
  37. TYPE SAFETY?

  38. FRAGILE

  39. ENUMERATIONS

  40. enum Result { case Success(String) case Error(Error) }

  41. switch result { case .Error(let error): println("There was an error

    (\(error)).") case .Success(let string): println("\(string)") }
  42. CLOSURES

  43. EVERYTHING IS A CLOSURE

  44. class TestObject { func testA() -> () { println("first test")

    } var testB:(() -> ()) = { println("second test") } }
  45. let testing = TestObject() testing.testA() testing.testB()

  46. testing.testB = testing.testA

  47. testing.testB() // Actually calls testA

  48. CONSISTENCY

  49. FUNCTIONAL

  50. None
  51. let string = "Hello World" let matches:[NSTextCheckingResult] = /* regex

    for words in string */ matches.map { string.substringWithRange($0.range) }
  52. let string = "Hello World" let matches:[NSTextCheckingResult] = /* regex

    for words in string */ var strings = [String]() for match in matches { let catch = string.substringWithRange(match.range) strings.append(catch) }
  53. func reduce(initial, combine) -> result

  54. combine : ((previous, current) -> (result))

  55. let items = [["a", "b"], ["c", "d"], ["e"]] items.reduce([]) {

    initial, expressions -> [String] in initial + expressions } => ["a", "b", "c", "d", "e"]
  56. let items = [["a", "b"], ["c", "d"], ["e"]] items.reduce([], +)

    => ["a", "b", "c", "d", "e"]
  57. func +<T>(lhs:[T], rhs:[T]) -> [T]

  58. items.sort { $0 > $1 }

  59. items.sort(>)

  60. items.filter { $0.hasPermission }

  61. func isAdmin(user:User) -> Bool { return user.username == "Kyle" }

    users.filter(isAdmin)
  62. GENERICS

  63. Array<String>

  64. Dictionary<String, Int>

  65. func max<T : Comparable>(a:T, b:T) -> T { return a

    > b ? a : b }
  66. max(anInteger, anotherInteger) max(aFloat, anotherFloat) max(aString, anotherString)

  67. PATTERN MATCHING

  68. let point = (1, 2) switch point { case (0,

    0): println("(0, 0) is at the origin.") case (-2...2, -2...2): println("(\(point.0), \(point.1)) is near the origin.") default: println("The point is at (\(point.0), \(point.1)).") } // prints "(1, 2) is near the origin."
  69. @auto_closure

  70. delay(5, println("Hello World"))

  71. func delay(time:UInt32, block:@auto_closure) { sleep(time) block() }

  72. BUILDING ASSERT() IN SWIFT

  73. assert(someExpensiveComputation() != 42)

  74. func assert(predicate : () -> Bool) { #if !NDEBUG if

    !predicate() { abort() } #endif }
  75. PLAYGROUNDS

  76. PLAYGROUNDS

  77. SWIFT

  78. SAFER

  79. CONVENTIONS

  80. CONVENTIONS

  81. LANGUAGE CONSTRAINTS

  82. BETTER CODE

  83. BETTER DEVELOPERS

  84. LESS BUGS

  85. WE'RE ALL BEGINNERS

  86. PERFECTING

  87. BREAKING CHANGES

  88. - Xcode 6.1 + Xcode 6.1.1

  89. FOR GOOD, NOT EVIL

  90. !

  91. FILE RADARS

  92. FUTURE

  93. COMMON DESIGN PATTERNS

  94. KYLEFULLER