A Swift Approach

A Swift Approach

A quick lightning talk I given at NSLondon on some new concepts and features in Apple's new Language, Swift.

https://twitter.com/kylefuller

D200a17dd269fd4001bacb11662dab4b?s=128

Kyle Fuller

July 24, 2014
Tweet

Transcript

  1. A SWIFT APPROACH

  2. NEW CONCEPTS

  3. RUNTIME

  4. @objc / NSObject

  5. RUNTIME

  6. WWDC 2015

  7. OPTIONALS?

  8. func compute() -> Bool?

  9. true false neither

  10. LACK OF A VALUE

  11. NOT AN EMPTY VALUE

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

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

  14. EXPRESS YOU DON'T ACCEPT NIL

  15. EXPRESS YOU WILL NOT RETURN NIL

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

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

  18. None
  19. RUNTIME ERROR

  20. RUNTIME ERROR

  21. COMPILE ERROR

  22. TUPLES

  23. PYTHON

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

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

  26. OBJECTIVE-C

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

  28. NAMED TUPLES

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

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

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

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

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

  34. FRAGILE

  35. CLOSURES

  36. EVERYTHING IS A CLOSURE

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

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

  39. testing.testB = testing.testA

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

  41. CONSISTENCY

  42. GENERICS

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

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

  45. PATTERN MATCHING

  46. 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."
  47. @auto_closure

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

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

  50. BUILDING ASSERT() IN SWIFT

  51. assert(someExpensiveComputation() != 42)

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

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

  54. PLAYGROUNDS

  55. SWIFT

  56. SAFER

  57. CONVENTIONS

  58. CONVENTIONS

  59. LANGUAGE CONSTRAINTS

  60. BETTER CODE

  61. BETTER DEVELOPERS

  62. LESS BUGS

  63. WE'RE ALL BEGINNERS

  64. None
  65. "I DON'T KNOW IF IT'S A BUG OR I'M DOING

    IT WRONG" ASH FURROW
  66. FILE RADARS

  67. SHAPE

  68. APPLE WILL break YOUR CODE

  69. FOR GOOD, NOT EVIL

  70. XCODE 6 GM?

  71. "WE EXPECT TO LOCK THIS DOWN"

  72. "BUT NOT BY THE FINAL RELEASE OF SWIFT THIS FALL."

  73. EARLY

  74. FUTURE

  75. COMMON PATTERNS

  76. TWITTER.COM/KYLEFULLER