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. • Released in June 2014 by Apple • Used to

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

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

    • A functional programming language • Easier than Obj-c • Only for Apple Platforms
  4. 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
  5. Some new cool stuff (for obj-c developers) • Mutability not

    on the type but on instantiation • Optionals • Generics • Value types and reference types
  6. Optionals (nullabble) • Everything in Swift must have a value

    (much safer) • Sometimes we need nil variables • Optionals work for every type
  7. 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.
  8. 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
  9. Enumerations • Can contain associate values of any type •

    Can feature computed properties, initializers and instance methods
  10. 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
  11. 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’
  12. 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
  13. 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.
  14. 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
  15. 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?
  16. 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
  17. 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 )
  18. 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
  19. 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