2014-06-04 11:50:17 +04:00
---
language: swift
contributors:
- ["Grant Timmerman", "http://github.com/grant"]
2014-08-24 07:48:10 +04:00
- ["Christopher Bess", "http://github.com/cbess"]
2014-06-04 11:50:17 +04:00
filename: learnswift.swift
---
Swift is a programming language for iOS and OS X development created by Apple. Designed to coexist with Objective-C and to be more resilient against erroneous code, Swift was introduced in 2014 at Apple's developer conference WWDC. It is built with the LLVM compiler included in Xcode 6 beta.
2014-08-25 03:33:16 +04:00
The official [Swift Programming Language ](https://itunes.apple.com/us/book/swift-programming-language/id881256329 ) book from Apple is now available via iBooks.
2014-06-04 11:50:17 +04:00
See also Apple's [getting started guide ](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/LandingPage/index.html ), which has a complete tutorial on Swift.
2014-09-08 15:08:28 +04:00
```swift
2014-06-05 00:22:01 +04:00
//
2014-08-24 07:48:10 +04:00
// MARK: Basics
2014-06-05 00:22:01 +04:00
//
2014-08-24 23:37:09 +04:00
// Xcode supports landmarks to annotate your code and lists them in the jump bar
// MARK: Section mark
// TODO: Do something soon
// FIXME Fix this code
2014-06-04 11:50:17 +04:00
println("Hello, world")
2014-08-24 07:48:10 +04:00
2014-06-04 11:50:17 +04:00
var myVariable = 42
2014-08-25 03:33:16 +04:00
let øπΩ = "value" // unicode variable names
2014-06-04 11:50:17 +04:00
let myConstant = 3.1415926
2014-08-24 07:48:10 +04:00
let convenience = "keyword" // contextual variable name
let weak = "keyword"; let override = "another keyword" // statements can be separated by a semi-colon
let `class` = "keyword" // backticks allow keywords to be used as variable names
2014-06-04 11:50:17 +04:00
let explicitDouble: Double = 70
2014-08-24 07:48:10 +04:00
let intValue = 0007 // 7
let largeIntValue = 77_000 // 77000
2014-08-24 09:48:54 +04:00
let label = "some text " + String(myVariable) // Casting
let piText = "Pi = \(myConstant), Pi 2 = \(myConstant * 2)" // String interpolation
var optionalString: String? = "optional" // Can be nil
2014-06-04 11:50:17 +04:00
optionalString = nil
2014-08-24 07:48:10 +04:00
/*
Comment here
/*
2014-08-25 03:33:16 +04:00
Nested comments are also supported
2014-08-24 07:48:10 +04:00
*/
*/
2014-06-04 11:50:17 +04:00
2014-06-05 00:22:01 +04:00
//
2014-08-24 07:48:10 +04:00
// MARK: Collections
2014-06-05 00:22:01 +04:00
//
2014-06-04 11:50:17 +04:00
// Array
var shoppingList = ["catfish", "water", "lemons"]
shoppingList[1] = "bottle of water"
2014-07-16 14:20:57 +04:00
let emptyArray = [String]()
2014-06-04 11:50:17 +04:00
// Dictionary
var occupations = [
2014-08-24 07:48:10 +04:00
"Malcolm": "Captain",
"kaylee": "Mechanic"
2014-06-04 11:50:17 +04:00
]
occupations["Jayne"] = "Public Relations"
2014-08-26 21:48:35 +04:00
let emptyDictionary = [String: Float]()
2014-06-04 11:50:17 +04:00
2014-06-05 00:22:01 +04:00
//
2014-08-24 07:48:10 +04:00
// MARK: Control Flow
2014-06-05 00:22:01 +04:00
//
2014-06-04 11:50:17 +04:00
// for loop (array)
let myArray = [1, 1, 2, 3, 5]
for value in myArray {
2014-08-24 07:48:10 +04:00
if value == 1 {
println("One!")
} else {
println("Not one!")
}
2014-06-04 11:50:17 +04:00
}
// for loop (dictionary)
2014-08-24 07:48:10 +04:00
var dict = ["one": 1, "two": 2]
2014-06-04 11:50:17 +04:00
for (key, value) in dict {
2014-08-24 07:48:10 +04:00
println("\(key): \(value)")
2014-06-04 11:50:17 +04:00
}
// for loop (range)
for i in -1...1 { // [-1, 0, 1]
2014-08-24 07:48:10 +04:00
println(i)
2014-06-04 11:50:17 +04:00
}
2014-07-16 14:20:57 +04:00
// use ..< to exclude the last number
2014-06-04 11:50:17 +04:00
// while loop
var i = 1
while i < 1000 {
2014-08-24 07:48:10 +04:00
i *= 2
2014-06-04 11:50:17 +04:00
}
// do-while loop
do {
2014-08-24 07:48:10 +04:00
println("hello")
2014-06-04 11:50:17 +04:00
} while 1 == 2
// Switch
let vegetable = "red pepper"
switch vegetable {
case "celery":
2014-08-24 07:48:10 +04:00
let vegetableComment = "Add some raisins and make ants on a log."
2014-06-04 11:50:17 +04:00
case "cucumber", "watercress":
2014-08-24 07:48:10 +04:00
let vegetableComment = "That would make a good tea sandwich."
2014-06-04 11:50:17 +04:00
case let x where x.hasSuffix("pepper"):
2014-08-24 07:48:10 +04:00
let vegetableComment = "Is it a spicy \(x)?"
2014-06-04 11:50:17 +04:00
default: // required (in order to cover all possible input)
2014-08-24 07:48:10 +04:00
let vegetableComment = "Everything tastes good in soup."
2014-06-04 11:50:17 +04:00
}
2014-06-05 00:22:01 +04:00
//
2014-08-24 07:48:10 +04:00
// MARK: Functions
2014-06-05 00:22:01 +04:00
//
// Functions are a first-class type, meaning they can be nested
// in functions and can be passed around
2014-06-04 11:50:17 +04:00
2014-08-25 03:33:16 +04:00
// Function with Swift header docs (format as reStructedText)
2014-08-24 09:48:54 +04:00
/**
A greet operation
- A bullet in docs
- Another bullet in the docs
:param: name A name
:param: day A day
:returns: A string containing the name and day value.
*/
2014-06-04 11:50:17 +04:00
func greet(name: String, day: String) -> String {
2014-08-24 07:48:10 +04:00
return "Hello \(name), today is \(day)."
2014-06-04 11:50:17 +04:00
}
greet("Bob", "Tuesday")
// Function that returns multiple items in a tuple
func getGasPrices() -> (Double, Double, Double) {
2014-08-24 07:48:10 +04:00
return (3.59, 3.69, 3.79)
2014-06-04 11:50:17 +04:00
}
2014-08-24 07:48:10 +04:00
// Variadic Args
2014-06-04 11:50:17 +04:00
func setup(numbers: Int...) {}
// Passing and returning functions
func makeIncrementer() -> (Int -> Int) {
2014-08-24 07:48:10 +04:00
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
2014-06-04 11:50:17 +04:00
}
var increment = makeIncrementer()
increment(7)
2014-06-05 00:22:01 +04:00
//
2014-08-24 07:48:10 +04:00
// MARK: Closures
2014-06-05 00:22:01 +04:00
//
2014-07-16 14:20:57 +04:00
var numbers = [1, 2, 6]
2014-06-05 00:22:01 +04:00
// Functions are special case closures ({})
2014-06-04 11:50:17 +04:00
// Closure example.
// `->` separates the arguments and return type
// `in` separates the closure header from the closure body
numbers.map({
2014-08-24 07:48:10 +04:00
(number: Int) -> Int in
let result = 3 * number
return result
})
2014-06-04 11:50:17 +04:00
// When the type is known, like above, we can do this
numbers = numbers.map({ number in 3 * number })
2014-08-24 07:48:10 +04:00
// Or even this
2014-07-16 14:20:57 +04:00
//numbers = numbers.map({ $0 * 3 })
2014-06-04 11:50:17 +04:00
print(numbers) // [3, 6, 18]
2014-08-24 07:48:10 +04:00
// Trailing closure
numbers = sorted(numbers) { $0 > $1 }
print(numbers) // [18, 6, 3]
// Super shorthand, since the < operator infers the types
numbers = sorted(numbers, < )
print(numbers) // [3, 6, 18]
2014-06-04 11:50:17 +04:00
2014-08-25 03:33:16 +04:00
//
// MARK: Structures
//
// Structures and classes have very similar capabilites
struct NamesTable {
let names: [String]
// Custom subscript
subscript(index: Int) -> String {
return names[index]
}
}
// Structures have an auto-generated (implicit) designated initializer
let namesTable = NamesTable(names: ["Me", "Them"])
//let name = namesTable[2]
//println("Name is \(name)") // Name is Them
2014-06-05 00:22:01 +04:00
//
2014-08-24 07:48:10 +04:00
// MARK: Classes
2014-06-05 00:22:01 +04:00
//
2014-08-25 03:33:16 +04:00
// Classes, structures and its members have three levels of access control
// They are: internal (default), public, private
public class Shape {
public func getArea() -> Int {
2014-08-24 07:48:10 +04:00
return 0;
}
}
2014-06-05 00:22:01 +04:00
// All methods and properties of a class are public.
// If you just need to store data in a
// structured object, you should use a `struct`
2014-06-04 11:50:17 +04:00
2014-08-25 03:33:16 +04:00
internal class Rect: Shape {
2014-08-24 07:48:10 +04:00
var sideLength: Int = 1
// Custom getter and setter property
2014-08-25 03:33:16 +04:00
private var perimeter: Int {
2014-08-24 07:48:10 +04:00
get {
return 4 * sideLength
}
set {
2014-08-25 03:33:16 +04:00
// `newValue` is an implicit variable available to setters
2014-08-24 07:48:10 +04:00
sideLength = newValue / 4
}
2014-06-04 11:50:17 +04:00
}
2014-08-25 03:33:16 +04:00
// Lazily load a property
// subShape remains nil (uninitialized) until getter called
lazy var subShape = Rect(sideLength: 4)
2014-08-24 07:48:10 +04:00
// If you don't need a custom getter and setter,
// but still want to run code before and after getting or setting
// a property, you can use `willSet` and `didSet`
var identifier: String = "defaultID" {
2014-08-25 03:33:16 +04:00
// the `willSet` arg will be the variable name for the new value
2014-08-24 07:48:10 +04:00
willSet(someIdentifier) {
print(someIdentifier)
}
2014-06-04 11:50:17 +04:00
}
2014-08-24 07:48:10 +04:00
init(sideLength: Int) {
super.init()
self.sideLength = sideLength
}
func shrink() {
if sideLength > 0 {
--sideLength
}
2014-06-04 11:50:17 +04:00
}
2014-08-24 07:48:10 +04:00
override func getArea() -> Int {
return sideLength * sideLength
}
}
2014-06-04 11:50:17 +04:00
2014-09-03 03:52:04 +04:00
// A simple class `Square` extends `Rect`
2014-08-24 07:48:10 +04:00
class Square: Rect {
convenience init() {
self.init(sideLength: 5)
}
2014-06-04 11:50:17 +04:00
}
2014-08-24 07:48:10 +04:00
var mySquare = Square()
2014-06-04 11:50:17 +04:00
print(mySquare.getArea()) // 25
mySquare.shrink()
print(mySquare.sideLength) // 4
2014-08-24 09:48:54 +04:00
// compare instances, not the same as == which compares objects (equal to)
if mySquare === mySquare {
2014-08-25 03:33:16 +04:00
println("Yep, it's mySquare")
2014-08-24 09:48:54 +04:00
}
2014-06-04 11:50:17 +04:00
2014-06-05 00:22:01 +04:00
//
2014-08-24 07:48:10 +04:00
// MARK: Enums
2014-06-05 00:22:01 +04:00
//
// Enums can optionally be of a specific type or on their own.
// They can contain methods like classes.
2014-06-04 11:50:17 +04:00
enum Suit {
2014-08-24 07:48:10 +04:00
case Spades, Hearts, Diamonds, Clubs
func getIcon() -> String {
switch self {
case .Spades: return "♤"
case .Hearts: return "♡"
case .Diamonds: return "♢"
case .Clubs: return "♧"
}
2014-06-04 11:50:17 +04:00
}
}
2014-06-05 00:22:01 +04:00
//
2014-08-25 03:33:16 +04:00
// MARK: Protocols
2014-06-05 00:22:01 +04:00
//
2014-06-04 11:50:17 +04:00
2014-08-25 03:33:16 +04:00
// `protocol` s can require that conforming types have specific
// instance properties, instance methods, type methods,
// operators, and subscripts.
2014-08-24 07:48:10 +04:00
protocol ShapeGenerator {
2014-08-25 03:33:16 +04:00
var enabled: Bool { get set }
2014-08-24 07:48:10 +04:00
func buildShape() -> Shape
}
2014-08-25 03:33:16 +04:00
/*
// Protocols declared with @objc allow optional functions,
// which allow you to check for conformance
@objc protocol TransformShape {
optional func reshaped()
optional func canReshape() -> Bool
}
class MyShape: Rect {
var delegate: TransformShape?
func grow() {
sideLength += 2
if let allow = self.delegate?.canReshape?() {
// test for delegate then for method
self.delegate?.reshaped?()
}
}
}
*/
//
// MARK: Other
//
2014-08-24 07:48:10 +04:00
// `extension` s: Add extra functionality to an already existing type
2014-08-25 03:33:16 +04:00
// Square now "conforms" to the `Printable` protocol
2014-08-24 07:48:10 +04:00
extension Square: Printable {
var description: String {
return "Area: \(self.getArea()) - ID: \(self.identifier)"
}
}
println("Square: \(mySquare)")
2014-08-24 09:48:54 +04:00
// You can also extend built-in types
extension Int {
var customProperty: String {
return "This is \(self)"
}
func multiplyBy(num: Int) -> Int {
return num * self
}
}
println(7.customProperty) // "This is 7"
println(14.multiplyBy(2)) // 42
2014-06-05 00:22:01 +04:00
// Generics: Similar to Java. Use the `where` keyword to specify the
// requirements of the generics.
2014-06-04 11:50:17 +04:00
2014-08-24 07:48:10 +04:00
func findIndex< T: Equatable > (array: [T], valueToFind: T) -> Int? {
for (index, value) in enumerate(array) {
if value == valueToFind {
return index
}
}
return nil
}
2014-08-25 03:33:16 +04:00
let foundAtIndex = findIndex([1, 2, 3, 4], 3)
println(foundAtIndex == 2) // true
2014-08-24 09:48:54 +04:00
// Operators:
// Custom operators can start with the characters:
// / = - + * % < > ! & | ^ . ~
// or
// Unicode math, symbol, arrow, dingbat, and line/box drawing characters.
prefix operator !!! {}
2014-08-25 03:33:16 +04:00
// A prefix operator that triples the side length when used
2014-08-24 09:48:54 +04:00
prefix func !!! (inout shape: Square) -> Square {
shape.sideLength *= 3
return shape
}
2014-08-25 03:33:16 +04:00
// current value
println(mySquare.sideLength) // 4
// change side length using custom !!! operator, increases size by 3
!!!mySquare
println(mySquare.sideLength) // 12
2014-08-24 09:48:54 +04:00
2014-07-16 14:20:57 +04:00
```