Introduction to Swift

Introduction to Swift

Presentation by Jaime Azevedo

When talking about programming languages, Swift is the new kid in Apple’s block. In this session we are introducing Swift from an iOS developer's perspective and how it helps build more reliable code compared to its ancestor Objective-C while crushing some myths along the way.

But, is it mature enough? XCode. Using Swift in production and in building apps where Obj-c meets Swift.

Now in version 2.2, we will also talk about where we are going and how open sourcing Swift enables for new interactions with the developer community such as language improvement and other applications of the language.

B4fbb97c316798392c24b92f866c5ed6?s=128

Porto Codes

July 20, 2016
Tweet

Transcript

  1. Introduction to Swift Porto Codes: Julho 2016 - Jaime Azevedo

  2. None
  3. None
  4. • Released in June 2014 by Apple • Used to

    develop for iOS/macOS/watchOS/tvOS • Currently on version 2.3 and 3.0 preview 1
  5. “Swift is a general-purpose programming language built using a modern

    approach to safety, performance, and software design patterns.”
  6. What Swift is not • Weakly typed language • Interpreted

    • A functional programming language • Easier than Obj-c • Only for Apple Platforms
  7. Obj-c vs Swift old vs new • No need for

    a root class • No more Obj-c messaging • Much less [] and ; • No more interface file (.h) and implementation file (.m) • No more imports inside the same project • Simpler on simple tasks
  8. Some new cool stuff (for obj-c developers) • Mutability not

    on the type but on instantiation • Optionals • Generics • Value types and reference types
  9. let and var

  10. Optionals (nullabble) • Everything in Swift must have a value

    (much safer) • Sometimes we need nil variables • Optionals work for every type
  11. Implicitly Unwrapped Optional !

  12. Optional Unwrapping value with !

  13. Optional binding No need for unwrapping!

  14. Optional chaining Company is nil? Don’t address company, don’t crash…

  15. Generics • Just like C++ templates and Java generics •

    Enables you to write flexible, reusable functions and types • Swift standard library is built with generic code Arrays and dictionaries are examples of generic collections • Avoids duplication and expresses its intent in a clear, abstracted manner.
  16. Generics

  17. Enumerations • Enumerations are first-class types • Unlike Obj-c they

    are not a set of integers • Can have no value or a raw value Same type of raw values
  18. Enumerations • Can contain associate values of any type •

    Can feature computed properties, initializers and instance methods
  19. Structs • Allows definition of properties to store values •

    Allows methods to provide functionality • Allows define subscripts to access values • Can have initializers to set values
  20. Class • Inheritance • Type casting • Deinitializers

  21. Protocols (just like java interfaces) • Require confirming types to

    provide defined properties and methods • Do not care if properties are stored or computed, only name and type • Define if a property is gettable or gettable and settable • Properties are all ‘vars’
  22. Protocols

  23. Extensions • Add new functionality to existing classes, structs, enums

    or protocols • Extensions work for code that you have no access • Similar to Obj-c categories, but doesn’t need a name • Extensions can: • Make an existing type conform to a protocol • Provide new initializers • Define instance methods and type methods
  24. Extensions String now conforms with Validable!

  25. Extensions all the way • Extensions are really being pushed

    by Swift as the way to go • Extensions add functionality without the coupling of inheritance • Bad when needed to add properties.
  26. Closures (lambdas in Python) • Closures are self-contained blocks just

    like Obj-c • Are reference types • Syntax optimization to write closures in shortened form without loss of clarity or intent • Can capture reference to variables in context
  27. Closures

  28. Closures • Oh, and btw, Swift standard library contains map,

    reduce, filter, transform.
  29. Functions • Special type of closure, a named closure •

    Parameters can define external and internal names • Can have default parameter values What is going on there?
  30. Functions • Takes functions as parameters and can return functions

    • Function types can be used just like any other type • Can nest other named functions that only exist on parent function’s scope and capture parent’s values
  31. Patterns • Wildcard pattern • Tuple pattern • Type casting

    pattern • Etc.
  32. Swift in real life

  33. Swift in real life • Tools are much better than

    ever, still not flawless • Combining Swift and Obj-c can be tricky • Playgrounds are a great tool to play with code • Migrating between versions can be a pain (looking at you 3.0 )
  34. Swift <-> Obj-c (interoperability would do another talk ) •

    Using Obj-c in Swift projects can be tricky, bridging headers are simple to create and majority of Obj-c API’s will work • Some customization needed to make Obj-c and C API’s more “Swifty” • Import Swift into Obj-c can be a pain
  35. Future • Swift 3.0 is the starting point from Apple

    to be more aware on dependencies • Swift 3.0 is also removing some syntax from Obj-c legacy (GCD, Core Graphics, first parameter labels and unsafe pointers are now optional) • Other Swift projects • Swift is very dependant on Foundation frameworks as a standalone language
  36. Questions?

  37. We’re hiring at Doist! doist.com/jobs

  38. Thank you