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

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.

Porto Codes

July 20, 2016
Tweet

More Decks by Porto Codes

Other Decks in Programming

Transcript

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

    View Slide

  2. View Slide

  3. View Slide

  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

    View Slide

  5. “Swift is a general-purpose programming
    language built using a modern approach
    to safety, performance, and software
    design patterns.”

    View Slide

  6. What Swift is not
    • Weakly typed language
    • Interpreted
    • A functional programming language
    • Easier than Obj-c
    • Only for Apple Platforms

    View Slide

  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

    View Slide

  8. Some new cool stuff
    (for obj-c developers)
    • Mutability not on the type but on instantiation
    • Optionals
    • Generics
    • Value types and reference types

    View Slide

  9. let and var

    View Slide

  10. Optionals
    (nullabble)
    • Everything in Swift must have a value (much safer)
    • Sometimes we need nil variables
    • Optionals work for every type

    View Slide

  11. Implicitly Unwrapped
    Optional !

    View Slide

  12. Optional
    Unwrapping value with !

    View Slide

  13. Optional binding
    No need for unwrapping!

    View Slide

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

    View Slide

  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.

    View Slide

  16. Generics

    View Slide

  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

    View Slide

  18. Enumerations
    • Can contain associate values of any type
    • Can feature computed properties, initializers and
    instance methods

    View Slide

  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

    View Slide

  20. Class
    • Inheritance
    • Type casting
    • Deinitializers

    View Slide

  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’

    View Slide

  22. Protocols

    View Slide

  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

    View Slide

  24. Extensions
    String now conforms with Validable!

    View Slide

  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.

    View Slide

  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

    View Slide

  27. Closures

    View Slide

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

    View Slide

  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?

    View Slide

  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

    View Slide

  31. Patterns
    • Wildcard pattern
    • Tuple pattern
    • Type casting pattern
    • Etc.

    View Slide

  32. Swift in real life

    View Slide

  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 )

    View Slide

  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

    View Slide

  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

    View Slide

  36. Questions?

    View Slide

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

    View Slide

  38. Thank you

    View Slide