Skip to content

Instantly share code, notes, and snippets.

@pathawks
Last active February 19, 2016 22:09
Show Gist options
  • Save pathawks/d639e13e59431c41da6d to your computer and use it in GitHub Desktop.
Save pathawks/d639e13e59431c41da6d to your computer and use it in GitHub Desktop.
I ❤️ Swift

I ❤️ Swift

📜 View The Slides

About Swift Philosophy

Swift is friendly to new programmers. It is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language.

The compiler is optimized for performance, and the language is optimized for development, without compromising on either. It’s designed to scale from “hello, world” to an entire operating system.

Swift Tour

Hello World

print("Hello, world!")

🔎 See The Code

If you have written code in C or Objective-C, this syntax looks familiar to you—in Swift, this line of code is a complete program. You don’t need to import a separate library for functionality like input/output or string handling. Code written at global scope is used as the entry point for the program, so you don’t need a main() function. You also don’t need to write semicolons at the end of every statement.

  • No main()
  • No semicolons needed
  • No need to import stdio.h for input/output

Variables

var myVariable = 42
myVariable = 50
let myConstant = 42
  • Use var to declare variables and let to declare constants
  • If variable is given a value immediately, its type is implied, and does not need to be explicitly provided
let implicitInteger = 70
let implicitDouble = 70.0
let explicitDouble: Double = 70
Safe Typing

Swift is a type-safe language, which means the language helps you to be clear about the types of values your code can work with. If part of your code expects a String, type safety prevents you from passing it an Int by mistake.

Because Swift is type safe, it performs type checks when compiling your code and flags any mismatched types as errors. This enables you to catch and fix errors as early as possible in the development process.

  • Variables cannot change types once declared, but often do not need explicit type information

Python is a Dynamically Typed language

myVariable = 42
myVariable = "42" # Valid

Swift is a Statically Typed language

var myVariable = 42
myVariable = "42" // Invalid, because `myVariable` is an Int

Loops

Enhanced For Loop in Java
final int[] individualScores  = [75, 43, 103, 87, 12];
int teamScore = 0;
for (int score : individualScores) {
    if (score > 50) {
        teamScore += 3;
    } else {
        teamScore += 1;
    }
}
System.out.printf("%d\n", teamScore);
Enhanced For Loop in Swift
let individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScores {
    if score > 50 {
        teamScore += 3
    } else {
        teamScore += 1
    }
}
print(teamScore)

🔎 See The Code

For Loops in Java
int accumulator = 0;
for (int i = 0; i <= 4; ++i) {
    accumulator += i;
}
System.out.printf("%d\n", accumulator);
For Loops in Swift
var accumulator = 0
for i in 0...4 {
    accumulator += i
}
print(accumulator)

🔎 See The Code

Same As

var accumulator = 0
for i in 0..<5 {
    accumulator += i
}
print(accumulator)

🔎 See The Code

For Loops in Python
accumulator = 0
for i in range(1, 5) {
    accumulator += i
}
print(accumulator)

Functions can return multiple values

Similar to Python, a Swift function can return a tuple, which is effectively a way to return multiple values from one function.

Returning multiple values in Python
x, y = getCoordinates()
Returning multiple values in Swift
var (x, y) = getCoordinates()

Expressive Syntax?

  • We haven't even talked about protocols and all that cool stuff!

Swift Goals

The goal of the Swift project is to create the best available language for uses ranging from systems programming, to mobile and desktop apps, scaling up to cloud services. Most importantly, Swift is designed to make writing and maintaining correct programs easier for the developer.

  • Safe
  • Fast
  • Expressive

We've seen Expressive, Let's talk Safe

  • Automatic Memory Management
  • Undefined behavior is the enemy of safety

Let's talk about "Fast"

None of this matters if the language is slow.

Chris Lattner

  • Designer of Swift
  • Compiler Guy
    • Creater of LLVM and clang
      • LLVM is default toolchain on FreeBSD
    • Completed a Ph.D. researching new techniques for optimizing pointer-intensive programs

Being a "compiler guy" informs design decisions of Swift.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment