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.
print("Hello, world!")
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
var myVariable = 42
myVariable = 50
let myConstant = 42
- Use
var
to declare variables andlet
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
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
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);
let individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScores {
if score > 50 {
teamScore += 3
} else {
teamScore += 1
}
}
print(teamScore)
int accumulator = 0;
for (int i = 0; i <= 4; ++i) {
accumulator += i;
}
System.out.printf("%d\n", accumulator);
var accumulator = 0
for i in 0...4 {
accumulator += i
}
print(accumulator)
Same As
var accumulator = 0
for i in 0..<5 {
accumulator += i
}
print(accumulator)
accumulator = 0
for i in range(1, 5) {
accumulator += i
}
print(accumulator)
Similar to Python, a Swift function can return a tuple, which is effectively a way to return multiple values from one function.
x, y = getCoordinates()
var (x, y) = getCoordinates()
- We haven't even talked about protocols and all that cool stuff!
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
- Automatic Memory Management
- Undefined behavior is the enemy of safety
None of this matters if the language is slow.
- 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
- Creater of LLVM and clang
Being a "compiler guy" informs design decisions of Swift.
- The Swift Programming Language. Apple Inc. Used under a Creative Commons Attribution 4.0 International (CC BY 4.0) License.
- Wikipedia: Chris Lattner Used under a Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) License.