summaryrefslogtreecommitdiffhomepage
path: root/swift.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'swift.html.markdown')
-rw-r--r--swift.html.markdown356
1 files changed, 259 insertions, 97 deletions
diff --git a/swift.html.markdown b/swift.html.markdown
index 0d1d2df4..516debed 100644
--- a/swift.html.markdown
+++ b/swift.html.markdown
@@ -3,6 +3,11 @@ language: swift
contributors:
- ["Grant Timmerman", "http://github.com/grant"]
- ["Christopher Bess", "http://github.com/cbess"]
+ - ["Joey Huang", "http://github.com/kamidox"]
+ - ["Anthony Nguyen", "http://github.com/anthonyn60"]
+ - ["Clayton Walker", "https://github.com/cwalk"]
+ - ["Fernando Valverde", "http://visualcosita.xyz"]
+ - ["Alexey Nazaroff", "https://github.com/rogaven"]
filename: learnswift.swift
---
@@ -10,7 +15,7 @@ Swift is a programming language for iOS and OS X development created by Apple. D
The official [Swift Programming Language](https://itunes.apple.com/us/book/swift-programming-language/id881256329) book from Apple is now available via iBooks.
-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.
+See also Apple's [getting started guide](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/), which has a complete tutorial on Swift.
```swift
// import a module
@@ -22,10 +27,13 @@ import UIKit
// Xcode supports landmarks to annotate your code and lists them in the jump bar
// MARK: Section mark
+// MARK: - Section mark with a separator line
// TODO: Do something soon
// FIXME: Fix this code
-println("Hello, world")
+// In Swift 2, println and print were combined into one print method. Print automatically appends a new line.
+print("Hello, world") // println is now print
+print("Hello, world", terminator: "") // printing without appending a newline
// variables (var) value can change after being set
// constants (let) value can NOT be changed after being set
@@ -39,27 +47,28 @@ let `class` = "keyword" // backticks allow keywords to be used as variable names
let explicitDouble: Double = 70
let intValue = 0007 // 7
let largeIntValue = 77_000 // 77000
-let label = "some text " + String(myVariable) // Casting
+let label = "some text " + String(myVariable) // String construction
let piText = "Pi = \(π), Pi 2 = \(π * 2)" // String interpolation
// Build Specific values
// uses -D build configuration
#if false
- println("Not printed")
+ print("Not printed")
let buildValue = 3
#else
let buildValue = 7
#endif
-println("Build value: \(buildValue)") // Build value: 7
+print("Build value: \(buildValue)") // Build value: 7
/*
- Optionals are a Swift language feature that allows you to store a `Some` or
- `None` value.
+Optionals are a Swift language feature that either contains a value,
+or contains nil (no value) to indicate that a value is missing.
+A question mark (?) after the type marks the value as optional.
- Because Swift requires every property to have a value, even nil must be
- explicitly stored as an Optional value.
+Because Swift requires every property to have a value, even nil must be
+explicitly stored as an Optional value.
- Optional<T> is an enum.
+Optional<T> is an enum.
*/
var someOptionalString: String? = "optional" // Can be nil
// same as above, but ? is a postfix operator (syntax candy)
@@ -68,18 +77,26 @@ var someOptionalString2: Optional<String> = "optional"
if someOptionalString != nil {
// I am not nil
if someOptionalString!.hasPrefix("opt") {
- println("has the prefix")
+ print("has the prefix")
}
-
+
let empty = someOptionalString?.isEmpty
}
someOptionalString = nil
+/*
+Trying to use ! to access a non-existent optional value triggers a runtime
+error. Always make sure that an optional contains a non-nil value before
+using ! to force-unwrap its value.
+*/
+
// implicitly unwrapped optional
var unwrappedString: String! = "Value is expected."
// same as above, but ! is a postfix operator (more syntax candy)
var unwrappedString2: ImplicitlyUnwrappedOptional<String> = "Value is expected."
+// If let structure -
+// If let is a special structure in Swift that allows you to check if an Optional rhs holds a value, and in case it does - unwraps and assigns it to the lhs.
if let someOptionalStringConstant = someOptionalString {
// has `Some` value, non-nil
if !someOptionalStringConstant.hasPrefix("ok") {
@@ -87,15 +104,22 @@ if let someOptionalStringConstant = someOptionalString {
}
}
+// The nil-coalescing operator ?? unwraps an optional if it contains a non-nil value, or returns a default value.
+var someOptionalString: String?
+let someString = someOptionalString ?? "abc"
+print(someString) // abc
+
// Swift has support for storing a value of any type.
-// AnyObject == id
-// Unlike Objective-C `id`, AnyObject works with any value (Class, Int, struct, etc)
-var anyObjectVar: AnyObject = 7
-anyObjectVar = "Changed value to a string, not good practice, but possible."
+// For that purposes there is two keywords: `Any` and `AnyObject`
+// `AnyObject` == `id` from Objective-C
+// `Any` – also works with any scalar values (Class, Int, struct, etc.)
+var anyVar: Any = 7
+anyVar = "Changed value to a string, not good practice, but possible."
+let anyObjectVar: AnyObject = Int(1) as NSNumber
/*
Comment here
-
+
/*
Nested comments are also supported
*/
@@ -106,8 +130,8 @@ anyObjectVar = "Changed value to a string, not good practice, but possible."
//
/*
- Array and Dictionary types are structs. So `let` and `var` also indicate
- that they are mutable (var) or immutable (let) when declaring these types.
+Array and Dictionary types are structs. So `let` and `var` also indicate
+that they are mutable (var) or immutable (let) when declaring these types.
*/
// Array
@@ -116,6 +140,7 @@ shoppingList[1] = "bottle of water"
let emptyArray = [String]() // let == immutable
let emptyArray2 = Array<String>() // same as above
var emptyMutableArray = [String]() // var == mutable
+var explicitEmptyMutableStringArray: [String] = [] // same as above
// Dictionary
@@ -127,31 +152,40 @@ occupations["Jayne"] = "Public Relations"
let emptyDictionary = [String: Float]() // let == immutable
let emptyDictionary2 = Dictionary<String, Float>() // same as above
var emptyMutableDictionary = [String: Float]() // var == mutable
+var explicitEmptyMutableDictionary: [String: Float] = [:] // same as above
//
// MARK: Control Flow
//
+// Condition statements support "," (comma) clauses, which can be used
+// to help provide conditions on optional values.
+// Both the assignment and the "," clause must pass.
+let someNumber = Optional<Int>(7)
+if let num = someNumber, num > 3 {
+ print("num is greater than 3")
+}
+
// for loop (array)
let myArray = [1, 1, 2, 3, 5]
for value in myArray {
if value == 1 {
- println("One!")
+ print("One!")
} else {
- println("Not one!")
+ print("Not one!")
}
}
// for loop (dictionary)
var dict = ["one": 1, "two": 2]
for (key, value) in dict {
- println("\(key): \(value)")
+ print("\(key): \(value)")
}
// for loop (range)
for i in -1...shoppingList.count {
- println(i)
+ print(i)
}
shoppingList[1...2] = ["steak", "peacons"]
// use ..< to exclude the last number
@@ -162,9 +196,9 @@ while i < 1000 {
i *= 2
}
-// do-while loop
-do {
- println("hello")
+// repeat-while loop
+repeat {
+ print("hello")
} while 1 == 2
// Switch
@@ -182,7 +216,6 @@ default: // required (in order to cover all possible input)
let vegetableComment = "Everything tastes good in soup."
}
-
//
// MARK: Functions
//
@@ -190,28 +223,28 @@ default: // required (in order to cover all possible input)
// Functions are a first-class type, meaning they can be nested
// in functions and can be passed around
-// Function with Swift header docs (format as reStructedText)
+// Function with Swift header docs (format as Swift-modified Markdown syntax)
/**
- A greet operation
+A greet operation
- - A bullet in docs
- - Another bullet in the docs
+- 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.
+- Parameter name : A name
+- Parameter day : A day
+- Returns : A string containing the name and day value.
*/
func greet(name: String, day: String) -> String {
return "Hello \(name), today is \(day)."
}
-greet("Bob", "Tuesday")
+greet(name: "Bob", day: "Tuesday")
// similar to above except for the function parameter behaviors
-func greet2(#requiredName: String, externalParamName localParamName: String) -> String {
- return "Hello \(requiredName), the day is \(localParamName)"
+func greet2(name: String, externalParamName localParamName: String) -> String {
+ return "Hello \(name), the day is \(localParamName)"
}
-greet2(requiredName:"John", externalParamName: "Sunday")
+greet2(name: "John", externalParamName: "Sunday")
// Function that returns multiple items in a tuple
func getGasPrices() -> (Double, Double, Double) {
@@ -221,18 +254,40 @@ let pricesTuple = getGasPrices()
let price = pricesTuple.2 // 3.79
// Ignore Tuple (or other) values by using _ (underscore)
let (_, price1, _) = pricesTuple // price1 == 3.69
-println(price1 == pricesTuple.1) // true
-println("Gas price: \(price)")
+print(price1 == pricesTuple.1) // true
+print("Gas price: \(price)")
+
+// Labeled/named tuple params
+func getGasPrices2() -> (lowestPrice: Double, highestPrice: Double, midPrice: Double) {
+ return (1.77, 37.70, 7.37)
+}
+let pricesTuple2 = getGasPrices2()
+let price2 = pricesTuple2.lowestPrice
+let (_, price3, _) = pricesTuple2
+print(pricesTuple2.highestPrice == pricesTuple2.1) // true
+print("Highest gas price: \(pricesTuple2.highestPrice)")
+
+// guard statements
+func testGuard() {
+ // guards provide early exits or breaks, placing the error handler code near the conditions.
+ // it places variables it declares in the same scope as the guard statement.
+ guard let aNumber = Optional<Int>(7) else {
+ return
+ }
+
+ print("number is \(aNumber)")
+}
+testGuard()
// Variadic Args
func setup(numbers: Int...) {
- // its an array
- let number = numbers[0]
- let argCount = numbers.count
+ // it's an array
+ let _ = numbers[0]
+ let _ = numbers.count
}
// Passing and returning functions
-func makeIncrementer() -> (Int -> Int) {
+func makeIncrementer() -> ((Int) -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
@@ -242,15 +297,15 @@ var increment = makeIncrementer()
increment(7)
// pass by ref
-func swapTwoInts(inout a: Int, inout b: Int) {
+func swapTwoInts(a: inout Int, b: inout Int) {
let tempA = a
a = b
b = tempA
}
var someIntA = 7
var someIntB = 3
-swapTwoInts(&someIntA, &someIntB)
-println(someIntB) // 7
+swapTwoInts(a: &someIntA, b: &someIntB)
+print(someIntB) // 7
//
@@ -277,24 +332,18 @@ numbers = numbers.map({ number in 3 * number })
print(numbers) // [3, 6, 18]
// Trailing closure
-numbers = sorted(numbers) { $0 > $1 }
+numbers = numbers.sorted { $0 > $1 }
print(numbers) // [18, 6, 3]
-// Super shorthand, since the < operator infers the types
-
-numbers = sorted(numbers, < )
-
-print(numbers) // [3, 6, 18]
-
//
// MARK: Structures
//
-// Structures and classes have very similar capabilites
+// Structures and classes have very similar capabilities
struct NamesTable {
- let names = [String]()
-
+ let names: [String]
+
// Custom subscript
subscript(index: Int) -> String {
return names[index]
@@ -304,7 +353,45 @@ struct NamesTable {
// Structures have an auto-generated (implicit) designated initializer
let namesTable = NamesTable(names: ["Me", "Them"])
let name = namesTable[1]
-println("Name is \(name)") // Name is Them
+print("Name is \(name)") // Name is Them
+
+//
+// MARK: Error Handling
+//
+
+// The `Error` protocol is used when throwing errors to catch
+enum MyError: Error {
+ case badValue(msg: String)
+ case reallyBadValue(msg: String)
+}
+
+// functions marked with `throws` must be called using `try`
+func fakeFetch(value: Int) throws -> String {
+ guard 7 == value else {
+ throw MyError.reallyBadValue(msg: "Some really bad value")
+ }
+
+ return "test"
+}
+
+func testTryStuff() {
+ // assumes there will be no error thrown, otherwise a runtime exception is raised
+ let _ = try! fakeFetch(value: 7)
+
+ // if an error is thrown, then it proceeds, but if the value is nil
+ // it also wraps every return value in an optional, even if its already optional
+ let _ = try? fakeFetch(value: 7)
+
+ do {
+ // normal try operation that provides error handling via `catch` block
+ try fakeFetch(value: 1)
+ } catch MyError.badValue(let msg) {
+ print("Error message: \(msg)")
+ } catch {
+ // must be exhaustive
+ }
+}
+testTryStuff()
//
// MARK: Classes
@@ -315,7 +402,7 @@ println("Name is \(name)") // Name is Them
public class Shape {
public func getArea() -> Int {
- return 0;
+ return 0
}
}
@@ -325,7 +412,7 @@ public class Shape {
internal class Rect: Shape {
var sideLength: Int = 1
-
+
// Custom getter and setter property
private var perimeter: Int {
get {
@@ -336,11 +423,16 @@ internal class Rect: Shape {
sideLength = newValue / 4
}
}
-
+
+ // Computed properties must be declared as `var`, you know, cause' they can change
+ var smallestSideLength: Int {
+ return self.sideLength - 1
+ }
+
// Lazily load a property
// subShape remains nil (uninitialized) until getter called
lazy var subShape = Rect(sideLength: 4)
-
+
// 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`
@@ -350,19 +442,19 @@ internal class Rect: Shape {
print(someIdentifier)
}
}
-
+
init(sideLength: Int) {
self.sideLength = sideLength
// always super.init last when init custom properties
super.init()
}
-
+
func shrink() {
if sideLength > 0 {
- --sideLength
+ sideLength -= 1
}
}
-
+
override func getArea() -> Int {
return sideLength * sideLength
}
@@ -385,7 +477,36 @@ let aShape = mySquare as Shape
// compare instances, not the same as == which compares objects (equal to)
if mySquare === mySquare {
- println("Yep, it's mySquare")
+ print("Yep, it's mySquare")
+}
+
+// Optional init
+class Circle: Shape {
+ var radius: Int
+ override func getArea() -> Int {
+ return 3 * radius * radius
+ }
+
+ // Place a question mark postfix after `init` is an optional init
+ // which can return nil
+ init?(radius: Int) {
+ self.radius = radius
+ super.init()
+
+ if radius <= 0 {
+ return nil
+ }
+ }
+}
+
+var myCircle = Circle(radius: 1)
+print(myCircle?.getArea()) // Optional(3)
+print(myCircle!.getArea()) // 3
+var myEmptyCircle = Circle(radius: -1)
+print(myEmptyCircle?.getArea()) // "nil"
+if let circle = myEmptyCircle {
+ // will not execute since myEmptyCircle is nil
+ print("circle is not nil")
}
@@ -397,27 +518,50 @@ if mySquare === mySquare {
// They can contain methods like classes.
enum Suit {
- case Spades, Hearts, Diamonds, Clubs
+ case spades, hearts, diamonds, clubs
func getIcon() -> String {
switch self {
- case .Spades: return "♤"
- case .Hearts: return "♡"
- case .Diamonds: return "♢"
- case .Clubs: return "♧"
+ case .spades: return "♤"
+ case .hearts: return "♡"
+ case .diamonds: return "♢"
+ case .clubs: return "♧"
}
}
}
// Enum values allow short hand syntax, no need to type the enum type
// when the variable is explicitly declared
-var suitValue: Suit = .Hearts
+var suitValue: Suit = .hearts
-// Non-Integer enums require direct raw value assignments
+// String enums can have direct raw value assignments
+// or their raw values will be derived from the Enum field
enum BookName: String {
- case John = "John"
- case Luke = "Luke"
+ case john
+ case luke = "Luke"
}
-println("Name: \(BookName.John.rawValue)")
+print("Name: \(BookName.john.rawValue)")
+
+// Enum with associated Values
+enum Furniture {
+ // Associate with Int
+ case desk(height: Int)
+ // Associate with String and Int
+ case chair(String, Int)
+
+ func description() -> String {
+ switch self {
+ case .desk(let height):
+ return "Desk with \(height) cm"
+ case .chair(let brand, let height):
+ return "Chair of \(brand) with \(height) cm"
+ }
+ }
+}
+
+var desk: Furniture = .desk(height: 80)
+print(desk.description()) // "Desk with 80 cm"
+var chair = Furniture.chair("Foo", 40)
+print(chair.description()) // "Chair of Foo with 40 cm"
//
@@ -434,21 +578,25 @@ protocol ShapeGenerator {
}
// Protocols declared with @objc allow optional functions,
-// which allow you to check for conformance
+// which allow you to check for conformance. These functions must be
+// marked with @objc also.
@objc protocol TransformShape {
- optional func reshaped()
- optional func canReshape() -> Bool
+ @objc optional func reshape()
+ @objc optional func canReshape() -> Bool
}
class MyShape: Rect {
var delegate: TransformShape?
-
+
func grow() {
sideLength += 2
-
- if let allow = self.delegate?.canReshape?() {
+
+ // Place a question mark after an optional property, method, or
+ // subscript to gracefully ignore a nil value and return nil
+ // instead of throwing a runtime error ("optional chaining").
+ if let reshape = self.delegate?.canReshape?(), reshape {
// test for delegate then for method
- self.delegate?.reshaped?()
+ self.delegate?.reshape?()
}
}
}
@@ -460,60 +608,74 @@ class MyShape: Rect {
// `extension`s: Add extra functionality to an already existing type
-// Square now "conforms" to the `Printable` protocol
-extension Square: Printable {
+// Square now "conforms" to the `CustomStringConvertible` protocol
+extension Square: CustomStringConvertible {
var description: String {
return "Area: \(self.getArea()) - ID: \(self.identifier)"
}
}
-println("Square: \(mySquare)")
+print("Square: \(mySquare)")
// 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
+print(7.customProperty) // "This is 7"
+print(14.multiplyBy(num: 3)) // 42
// Generics: Similar to Java and C#. Use the `where` keyword to specify the
// requirements of the generics.
func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {
- for (index, value) in enumerate(array) {
+ for (index, value) in array.enumerated() {
if value == valueToFind {
return index
}
}
return nil
}
-let foundAtIndex = findIndex([1, 2, 3, 4], 3)
-println(foundAtIndex == 2) // true
+let foundAtIndex = findIndex(array: [1, 2, 3, 4], valueToFind: 3)
+print(foundAtIndex == 2) // true
// Operators:
// Custom operators can start with the characters:
// / = - + * % < > ! & | ^ . ~
// or
// Unicode math, symbol, arrow, dingbat, and line/box drawing characters.
-prefix operator !!! {}
+prefix operator !!!
// A prefix operator that triples the side length when used
-prefix func !!! (inout shape: Square) -> Square {
+prefix func !!! (shape: inout Square) -> Square {
shape.sideLength *= 3
return shape
}
// current value
-println(mySquare.sideLength) // 4
+print(mySquare.sideLength) // 4
// change side length using custom !!! operator, increases size by 3
!!!mySquare
-println(mySquare.sideLength) // 12
+print(mySquare.sideLength) // 12
+
+// Operators can also be generics
+infix operator <->
+func <-><T: Equatable> (a: inout T, b: inout T) {
+ let c = a
+ a = b
+ b = c
+}
+
+var foo: Float = 10
+var bar: Float = 20
+
+foo <-> bar
+print("foo is \(foo), bar is \(bar)") // "foo is 20.0, bar is 10.0"
```