2014-06-04 07:50:17 +00:00
---
language: swift
contributors:
- ["Grant Timmerman", "http://github.com/grant"]
2014-08-24 03:48:10 +00:00
- ["Christopher Bess", "http://github.com/cbess"]
2015-10-08 03:11:24 +00:00
- ["Joey Huang", "http://github.com/kamidox"]
2015-08-04 03:11:44 +00:00
- ["Anthony Nguyen", "http://github.com/anthonyn60"]
2015-10-07 03:00:11 +00:00
- ["Clayton Walker", "https://github.com/cwalk"]
2015-11-01 00:48:44 +00:00
- ["Fernando Valverde", "http://visualcosita.xyz"]
2016-10-08 09:59:20 +00:00
- ["Alexey Nazaroff", "https://github.com/rogaven"]
2014-06-04 07:50:17 +00:00
filename: learnswift.swift
---
2014-12-14 02:01:08 +00:00
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+.
2014-06-04 07:50:17 +00:00
2014-08-24 23:33:16 +00:00
The official [Swift Programming Language ](https://itunes.apple.com/us/book/swift-programming-language/id881256329 ) book from Apple is now available via iBooks.
2015-10-07 02:25:38 +00:00
See also Apple's [getting started guide ](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ ), which has a complete tutorial on Swift.
2014-06-04 07:50:17 +00:00
2014-09-08 11:08:28 +00:00
```swift
2014-10-04 17:08:23 +00:00
// import a module
import UIKit
2014-06-04 20:22:01 +00:00
//
2014-08-24 03:48:10 +00:00
// MARK: Basics
2014-06-04 20:22:01 +00:00
//
2014-08-24 19:37:09 +00:00
// Xcode supports landmarks to annotate your code and lists them in the jump bar
// MARK: Section mark
2015-11-01 00:48:44 +00:00
// MARK: - Section mark with a separator line
2014-08-24 19:37:09 +00:00
// TODO: Do something soon
2014-12-14 02:01:08 +00:00
// FIXME: Fix this code
2014-08-24 19:37:09 +00:00
2015-08-04 03:10:10 +00:00
// 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
2015-11-10 00:09:48 +00:00
print("Hello, world", terminator: "") // printing without appending a newline
2014-08-24 03:48:10 +00:00
2014-10-18 02:22:28 +00:00
// variables (var) value can change after being set
// constants (let) value can NOT be changed after being set
2014-06-04 07:50:17 +00:00
var myVariable = 42
2014-08-24 23:33:16 +00:00
let øπΩ = "value" // unicode variable names
2014-10-04 17:08:23 +00:00
let π = 3.1415926
2014-08-24 03:48:10 +00: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 07:50:17 +00:00
let explicitDouble: Double = 70
2014-08-24 03:48:10 +00:00
let intValue = 0007 // 7
let largeIntValue = 77_000 // 77000
2015-11-13 14:52:21 +00:00
let label = "some text " + String(myVariable) // String construction
2014-10-04 17:08:23 +00:00
let piText = "Pi = \(π), Pi 2 = \(π * 2)" // String interpolation
2014-10-18 02:22:28 +00:00
// Build Specific values
// uses -D build configuration
#if false
2015-08-04 03:05:22 +00:00
print("Not printed")
2014-10-18 02:22:28 +00:00
let buildValue = 3
#else
let buildValue = 7
#endif
2015-08-04 03:05:22 +00:00
print("Build value: \(buildValue)") // Build value: 7
2014-10-18 02:22:28 +00:00
/*
2015-11-10 00:09:48 +00:00
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.
2014-12-14 02:01:08 +00:00
2015-11-10 00:09:48 +00:00
Because Swift requires every property to have a value, even nil must be
explicitly stored as an Optional value.
2014-10-18 02:22:28 +00:00
2015-11-10 00:09:48 +00:00
Optional< T > is an enum.
2014-10-18 02:22:28 +00:00
*/
2014-10-04 17:08:23 +00:00
var someOptionalString: String? = "optional" // Can be nil
2014-10-18 02:22:28 +00:00
// same as above, but ? is a postfix operator (syntax candy)
var someOptionalString2: Optional< String > = "optional"
2014-10-04 17:08:23 +00:00
if someOptionalString != nil {
// I am not nil
if someOptionalString!.hasPrefix("opt") {
2015-08-04 03:05:22 +00:00
print("has the prefix")
2014-10-04 17:08:23 +00:00
}
2015-10-08 03:11:24 +00:00
2014-10-04 17:08:23 +00:00
let empty = someOptionalString?.isEmpty
}
someOptionalString = nil
2015-10-07 03:36:32 +00:00
/*
2015-11-10 00:09:48 +00:00
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.
2015-10-07 03:36:32 +00:00
*/
2014-10-18 02:22:28 +00:00
// 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."
2016-10-01 20:33:04 +00:00
// If let structure -
2016-02-19 16:13:46 +00:00
// 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.
2014-10-18 02:22:28 +00:00
if let someOptionalStringConstant = someOptionalString {
// has `Some` value, non-nil
if !someOptionalStringConstant.hasPrefix("ok") {
// does not have the prefix
}
2014-10-04 17:08:23 +00:00
}
2016-11-12 00:12:13 +00:00
// 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
2014-10-18 02:22:28 +00:00
// Swift has support for storing a value of any type.
2016-10-08 09:59:20 +00:00
// 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
2014-06-04 07:50:17 +00:00
2014-08-24 03:48:10 +00:00
/*
2014-12-14 02:01:08 +00:00
Comment here
2015-10-08 03:11:24 +00:00
2014-08-24 03:48:10 +00:00
/*
2014-08-24 23:33:16 +00:00
Nested comments are also supported
2014-08-24 03:48:10 +00:00
*/
*/
2014-06-04 07:50:17 +00:00
2014-06-04 20:22:01 +00:00
//
2014-08-24 03:48:10 +00:00
// MARK: Collections
2014-06-04 20:22:01 +00:00
//
2014-10-18 02:22:28 +00:00
/*
2015-11-10 00:09:48 +00:00
Array and Dictionary types are structs. So `let` and `var` also indicate
that they are mutable (var) or immutable (let) when declaring these types.
2014-10-18 02:22:28 +00:00
*/
2014-06-04 07:50:17 +00:00
// Array
var shoppingList = ["catfish", "water", "lemons"]
shoppingList[1] = "bottle of water"
2014-12-14 02:01:08 +00:00
let emptyArray = [String]() // let == immutable
let emptyArray2 = Array< String > () // same as above
var emptyMutableArray = [String]() // var == mutable
2015-10-30 21:43:44 +00:00
var explicitEmptyMutableStringArray: [String] = [] // same as above
2014-10-18 02:22:28 +00:00
2014-06-04 07:50:17 +00:00
// Dictionary
var occupations = [
2014-08-24 03:48:10 +00:00
"Malcolm": "Captain",
"kaylee": "Mechanic"
2014-06-04 07:50:17 +00:00
]
occupations["Jayne"] = "Public Relations"
2014-12-14 02:01:08 +00:00
let emptyDictionary = [String: Float]() // let == immutable
let emptyDictionary2 = Dictionary< String , Float > () // same as above
var emptyMutableDictionary = [String: Float]() // var == mutable
2015-10-30 21:43:44 +00:00
var explicitEmptyMutableDictionary: [String: Float] = [:] // same as above
2014-06-04 07:50:17 +00:00
2014-06-04 20:22:01 +00:00
//
2014-08-24 03:48:10 +00:00
// MARK: Control Flow
2014-06-04 20:22:01 +00:00
//
2016-10-08 09:59:20 +00:00
// Condition statements support "," (comma) clauses, which can be used
2015-11-10 23:03:40 +00:00
// to help provide conditions on optional values.
2016-10-08 09:59:20 +00:00
// Both the assignment and the "," clause must pass.
2015-11-10 23:03:40 +00:00
let someNumber = Optional< Int > (7)
2016-10-08 09:59:20 +00:00
if let num = someNumber, num > 3 {
2015-11-10 23:03:40 +00:00
print("num is greater than 3")
}
2014-06-04 07:50:17 +00:00
// for loop (array)
let myArray = [1, 1, 2, 3, 5]
for value in myArray {
2014-08-24 03:48:10 +00:00
if value == 1 {
2015-08-04 03:05:22 +00:00
print("One!")
2014-08-24 03:48:10 +00:00
} else {
2015-08-04 03:05:22 +00:00
print("Not one!")
2014-08-24 03:48:10 +00:00
}
2014-06-04 07:50:17 +00:00
}
// for loop (dictionary)
2014-08-24 03:48:10 +00:00
var dict = ["one": 1, "two": 2]
2014-06-04 07:50:17 +00:00
for (key, value) in dict {
2015-08-04 03:05:22 +00:00
print("\(key): \(value)")
2014-06-04 07:50:17 +00:00
}
// for loop (range)
2014-10-04 17:08:23 +00:00
for i in -1...shoppingList.count {
2015-08-04 03:05:22 +00:00
print(i)
2014-06-04 07:50:17 +00:00
}
2014-10-04 17:08:23 +00:00
shoppingList[1...2] = ["steak", "peacons"]
2014-07-16 10:20:57 +00:00
// use ..< to exclude the last number
2014-06-04 07:50:17 +00:00
// while loop
var i = 1
while i < 1000 {
2014-08-24 03:48:10 +00:00
i *= 2
2014-06-04 07:50:17 +00:00
}
2015-11-10 00:09:48 +00:00
// repeat-while loop
repeat {
2015-08-04 03:05:22 +00:00
print("hello")
2014-06-04 07:50:17 +00:00
} while 1 == 2
// Switch
2014-12-14 02:01:08 +00:00
// Very powerful, think `if` statements with syntax candy
// They support String, object instances, and primitives (Int, Double, etc)
2014-06-04 07:50:17 +00:00
let vegetable = "red pepper"
switch vegetable {
case "celery":
2014-08-24 03:48:10 +00:00
let vegetableComment = "Add some raisins and make ants on a log."
2014-06-04 07:50:17 +00:00
case "cucumber", "watercress":
2014-08-24 03:48:10 +00:00
let vegetableComment = "That would make a good tea sandwich."
2014-12-14 02:01:08 +00:00
case let localScopeValue where localScopeValue.hasSuffix("pepper"):
let vegetableComment = "Is it a spicy \(localScopeValue)?"
2014-06-04 07:50:17 +00:00
default: // required (in order to cover all possible input)
2014-08-24 03:48:10 +00:00
let vegetableComment = "Everything tastes good in soup."
2014-06-04 07:50:17 +00:00
}
2014-06-04 20:22:01 +00:00
//
2014-08-24 03:48:10 +00:00
// MARK: Functions
2014-06-04 20:22:01 +00:00
//
// Functions are a first-class type, meaning they can be nested
// in functions and can be passed around
2014-06-04 07:50:17 +00:00
2016-01-14 18:11:45 +00:00
// Function with Swift header docs (format as Swift-modified Markdown syntax)
2014-12-14 02:01:08 +00:00
2014-08-24 05:48:54 +00:00
/**
2015-11-10 00:09:48 +00:00
A greet operation
2014-08-24 05:48:54 +00:00
2015-11-10 00:09:48 +00:00
- A bullet in docs
- Another bullet in the docs
2014-08-24 05:48:54 +00:00
2016-01-13 09:01:46 +00:00
- Parameter name : A name
- Parameter day : A day
- Returns : A string containing the name and day value.
2014-08-24 05:48:54 +00:00
*/
2014-06-04 07:50:17 +00:00
func greet(name: String, day: String) -> String {
2014-08-24 03:48:10 +00:00
return "Hello \(name), today is \(day)."
2014-06-04 07:50:17 +00:00
}
2016-10-08 09:59:20 +00:00
greet(name: "Bob", day: "Tuesday")
2014-06-04 07:50:17 +00:00
2014-12-14 02:01:08 +00:00
// similar to above except for the function parameter behaviors
2016-10-08 09:59:20 +00:00
func greet2(name: String, externalParamName localParamName: String) -> String {
return "Hello \(name), the day is \(localParamName)"
2014-12-14 02:01:08 +00:00
}
2016-10-08 09:59:20 +00:00
greet2(name: "John", externalParamName: "Sunday")
2014-12-14 02:01:08 +00:00
2014-06-04 07:50:17 +00:00
// Function that returns multiple items in a tuple
func getGasPrices() -> (Double, Double, Double) {
2014-08-24 03:48:10 +00:00
return (3.59, 3.69, 3.79)
2014-06-04 07:50:17 +00:00
}
2014-10-04 17:08:23 +00:00
let pricesTuple = getGasPrices()
let price = pricesTuple.2 // 3.79
2014-10-18 02:22:28 +00:00
// Ignore Tuple (or other) values by using _ (underscore)
let (_, price1, _) = pricesTuple // price1 == 3.69
2015-08-04 03:05:22 +00:00
print(price1 == pricesTuple.1) // true
print("Gas price: \(price)")
2014-06-04 07:50:17 +00:00
2015-11-10 23:03:40 +00:00
// Labeled/named tuple params
2015-11-09 23:54:05 +00:00
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
2015-11-10 00:09:48 +00:00
print(pricesTuple2.highestPrice == pricesTuple2.1) // true
print("Highest gas price: \(pricesTuple2.highestPrice)")
2015-11-09 23:54:05 +00:00
2015-11-10 23:03:40 +00:00
// 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()
2014-08-24 03:48:10 +00:00
// Variadic Args
2014-10-04 17:08:23 +00:00
func setup(numbers: Int...) {
2017-08-23 08:14:39 +00:00
// it's an array
2015-11-10 00:09:48 +00:00
let _ = numbers[0]
let _ = numbers.count
2014-10-04 17:08:23 +00:00
}
2014-06-04 07:50:17 +00:00
// Passing and returning functions
2016-10-08 09:59:20 +00:00
func makeIncrementer() -> ((Int) -> Int) {
2014-08-24 03:48:10 +00:00
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
2014-06-04 07:50:17 +00:00
}
var increment = makeIncrementer()
increment(7)
2014-10-04 17:08:23 +00:00
// pass by ref
2016-10-08 09:59:20 +00:00
func swapTwoInts(a: inout Int, b: inout Int) {
2014-10-04 17:08:23 +00:00
let tempA = a
a = b
b = tempA
}
var someIntA = 7
var someIntB = 3
2016-10-08 09:59:20 +00:00
swapTwoInts(a: & someIntA, b: & someIntB)
2015-08-04 03:05:22 +00:00
print(someIntB) // 7
2014-10-04 17:08:23 +00:00
2014-06-04 07:50:17 +00:00
2014-06-04 20:22:01 +00:00
//
2014-08-24 03:48:10 +00:00
// MARK: Closures
2014-06-04 20:22:01 +00:00
//
2014-07-16 10:20:57 +00:00
var numbers = [1, 2, 6]
2014-06-04 20:22:01 +00:00
// Functions are special case closures ({})
2014-06-04 07:50:17 +00:00
// Closure example.
// `->` separates the arguments and return type
// `in` separates the closure header from the closure body
numbers.map({
2014-08-24 03:48:10 +00:00
(number: Int) -> Int in
let result = 3 * number
return result
})
2014-06-04 07:50:17 +00:00
// When the type is known, like above, we can do this
numbers = numbers.map({ number in 3 * number })
2014-08-24 03:48:10 +00:00
// Or even this
2014-07-16 10:20:57 +00:00
//numbers = numbers.map({ $0 * 3 })
2014-06-04 07:50:17 +00:00
print(numbers) // [3, 6, 18]
2014-08-24 03:48:10 +00:00
// Trailing closure
2016-10-08 09:59:20 +00:00
numbers = numbers.sorted { $0 > $1 }
2014-08-24 03:48:10 +00:00
print(numbers) // [18, 6, 3]
2014-08-24 23:33:16 +00:00
//
// MARK: Structures
//
2015-10-07 03:36:32 +00:00
// Structures and classes have very similar capabilities
2014-08-24 23:33:16 +00:00
struct NamesTable {
2015-11-10 00:09:48 +00:00
let names: [String]
2015-10-08 03:11:24 +00:00
2014-08-24 23:33:16 +00:00
// Custom subscript
subscript(index: Int) -> String {
return names[index]
}
}
// Structures have an auto-generated (implicit) designated initializer
let namesTable = NamesTable(names: ["Me", "Them"])
2014-12-14 02:01:08 +00:00
let name = namesTable[1]
2015-08-04 03:05:22 +00:00
print("Name is \(name)") // Name is Them
2014-08-24 23:33:16 +00:00
2015-11-23 19:12:49 +00:00
//
// MARK: Error Handling
//
2016-10-08 09:59:20 +00:00
// The `Error` protocol is used when throwing errors to catch
enum MyError: Error {
2015-11-23 19:12:49 +00:00
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
2016-10-08 09:59:20 +00:00
let _ = try! fakeFetch(value: 7)
2015-11-23 19:12:49 +00:00
// 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
2016-10-08 09:59:20 +00:00
let _ = try? fakeFetch(value: 7)
2015-11-23 19:12:49 +00:00
do {
// normal try operation that provides error handling via `catch` block
2016-10-08 09:59:20 +00:00
try fakeFetch(value: 1)
2015-11-23 19:12:49 +00:00
} catch MyError.BadValue(let msg) {
print("Error message: \(msg)")
} catch {
// must be exhaustive
}
}
testTryStuff()
2014-06-04 20:22:01 +00:00
//
2014-08-24 03:48:10 +00:00
// MARK: Classes
2014-06-04 20:22:01 +00:00
//
2014-08-24 23:33:16 +00: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 {
2016-01-16 18:30:25 +00:00
return 0
2014-08-24 03:48:10 +00:00
}
}
2014-06-04 20:22:01 +00: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 07:50:17 +00:00
2014-08-24 23:33:16 +00:00
internal class Rect: Shape {
2014-08-24 03:48:10 +00:00
var sideLength: Int = 1
2015-10-08 03:11:24 +00:00
2014-08-24 03:48:10 +00:00
// Custom getter and setter property
2014-08-24 23:33:16 +00:00
private var perimeter: Int {
2014-08-24 03:48:10 +00:00
get {
return 4 * sideLength
}
set {
2014-08-24 23:33:16 +00:00
// `newValue` is an implicit variable available to setters
2014-08-24 03:48:10 +00:00
sideLength = newValue / 4
}
2014-06-04 07:50:17 +00:00
}
2015-10-08 03:11:24 +00:00
2015-11-09 23:55:53 +00:00
// Computed properties must be declared as `var` , you know, cause' they can change
2015-11-09 23:54:05 +00:00
var smallestSideLength: Int {
return self.sideLength - 1
}
2015-11-09 23:55:53 +00:00
2014-08-24 23:33:16 +00:00
// Lazily load a property
// subShape remains nil (uninitialized) until getter called
lazy var subShape = Rect(sideLength: 4)
2015-10-08 03:11:24 +00:00
2014-08-24 03:48:10 +00: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-24 23:33:16 +00:00
// the `willSet` arg will be the variable name for the new value
2014-08-24 03:48:10 +00:00
willSet(someIdentifier) {
print(someIdentifier)
}
2014-06-04 07:50:17 +00:00
}
2015-10-08 03:11:24 +00:00
2014-08-24 03:48:10 +00:00
init(sideLength: Int) {
self.sideLength = sideLength
2014-12-14 02:01:08 +00:00
// always super.init last when init custom properties
2014-10-04 17:08:23 +00:00
super.init()
2014-08-24 03:48:10 +00:00
}
2015-10-08 03:11:24 +00:00
2014-08-24 03:48:10 +00:00
func shrink() {
if sideLength > 0 {
2016-10-01 20:33:04 +00:00
sideLength -= 1
2014-08-24 03:48:10 +00:00
}
2014-06-04 07:50:17 +00:00
}
2015-10-08 03:11:24 +00:00
2014-08-24 03:48:10 +00:00
override func getArea() -> Int {
return sideLength * sideLength
}
}
2014-06-04 07:50:17 +00:00
2014-09-02 23:52:04 +00:00
// A simple class `Square` extends `Rect`
2014-08-24 03:48:10 +00:00
class Square: Rect {
convenience init() {
self.init(sideLength: 5)
}
2014-06-04 07:50:17 +00:00
}
2014-08-24 03:48:10 +00:00
var mySquare = Square()
2014-06-04 07:50:17 +00:00
print(mySquare.getArea()) // 25
mySquare.shrink()
print(mySquare.sideLength) // 4
2014-12-14 02:01:08 +00:00
// cast instance
let aShape = mySquare as Shape
2014-08-24 05:48:54 +00:00
// compare instances, not the same as == which compares objects (equal to)
if mySquare === mySquare {
2015-08-04 03:05:22 +00:00
print("Yep, it's mySquare")
2014-08-24 05:48:54 +00:00
}
2015-02-28 11:28:34 +00:00
// Optional init
class Circle: Shape {
var radius: Int
override func getArea() -> Int {
return 3 * radius * radius
}
2015-10-08 03:11:24 +00:00
2015-02-28 11:28:34 +00:00
// Place a question mark postfix after `init` is an optional init
// which can return nil
init?(radius: Int) {
self.radius = radius
super.init()
2015-10-08 03:11:24 +00:00
2015-02-28 11:28:34 +00:00
if radius < = 0 {
return nil
}
}
}
var myCircle = Circle(radius: 1)
2015-08-04 03:05:22 +00:00
print(myCircle?.getArea()) // Optional(3)
print(myCircle!.getArea()) // 3
2015-02-28 11:28:34 +00:00
var myEmptyCircle = Circle(radius: -1)
2015-08-04 03:05:22 +00:00
print(myEmptyCircle?.getArea()) // "nil"
2015-02-28 11:28:34 +00:00
if let circle = myEmptyCircle {
// will not execute since myEmptyCircle is nil
2015-08-04 03:05:22 +00:00
print("circle is not nil")
2014-08-24 05:48:54 +00:00
}
2014-06-04 07:50:17 +00:00
2014-06-04 20:22:01 +00:00
//
2014-08-24 03:48:10 +00:00
// MARK: Enums
2014-06-04 20:22:01 +00:00
//
// Enums can optionally be of a specific type or on their own.
// They can contain methods like classes.
2014-06-04 07:50:17 +00:00
enum Suit {
2014-08-24 03:48:10 +00: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 07:50:17 +00:00
}
}
2014-12-14 02:01:08 +00:00
// Enum values allow short hand syntax, no need to type the enum type
// when the variable is explicitly declared
var suitValue: Suit = .Hearts
2015-11-10 00:09:48 +00:00
// String enums can have direct raw value assignments
// or their raw values will be derived from the Enum field
2014-12-14 02:01:08 +00:00
enum BookName: String {
2015-11-10 00:09:48 +00:00
case John
2014-12-14 02:01:08 +00:00
case Luke = "Luke"
}
2015-08-04 03:05:22 +00:00
print("Name: \(BookName.John.rawValue)")
2014-12-14 02:01:08 +00:00
2015-02-28 11:28:34 +00:00
// Enum with associated Values
enum Furniture {
// Associate with Int
case Desk(height: Int)
// Associate with String and Int
case Chair(String, Int)
2015-10-08 03:11:24 +00:00
2015-02-28 11:28:34 +00:00
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)
2015-08-04 03:05:22 +00:00
print(desk.description()) // "Desk with 80 cm"
2015-02-28 11:28:34 +00:00
var chair = Furniture.Chair("Foo", 40)
2015-08-04 03:05:22 +00:00
print(chair.description()) // "Chair of Foo with 40 cm"
2014-12-14 02:01:08 +00:00
2014-06-04 07:50:17 +00:00
2014-06-04 20:22:01 +00:00
//
2014-08-24 23:33:16 +00:00
// MARK: Protocols
2014-06-04 20:22:01 +00:00
//
2014-06-04 07:50:17 +00:00
2014-08-24 23:33:16 +00:00
// `protocol` s can require that conforming types have specific
2014-10-04 17:08:23 +00:00
// instance properties, instance methods, type methods,
2014-08-24 23:33:16 +00:00
// operators, and subscripts.
2014-08-24 03:48:10 +00:00
protocol ShapeGenerator {
2014-08-24 23:33:16 +00:00
var enabled: Bool { get set }
2014-08-24 03:48:10 +00:00
func buildShape() -> Shape
}
2014-08-24 23:33:16 +00:00
// Protocols declared with @objc allow optional functions,
2016-10-08 09:59:20 +00:00
// which allow you to check for conformance. These functions must be
// marked with @objc also.
2014-08-24 23:33:16 +00:00
@objc protocol TransformShape {
2016-10-08 09:59:20 +00:00
@objc optional func reshape()
@objc optional func canReshape() -> Bool
2014-08-24 23:33:16 +00:00
}
class MyShape: Rect {
var delegate: TransformShape?
2015-10-08 03:11:24 +00:00
2014-08-24 23:33:16 +00:00
func grow() {
sideLength += 2
2015-01-20 22:52:31 +00:00
// 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").
2016-10-08 09:59:20 +00:00
if let reshape = self.delegate?.canReshape?(), reshape {
2014-08-24 23:33:16 +00:00
// test for delegate then for method
2015-11-10 00:09:48 +00:00
self.delegate?.reshape?()
2014-08-24 23:33:16 +00:00
}
}
}
2014-10-04 17:08:23 +00:00
2014-08-24 23:33:16 +00:00
//
// MARK: Other
//
2014-08-24 03:48:10 +00:00
// `extension` s: Add extra functionality to an already existing type
2014-08-24 23:33:16 +00:00
2015-11-23 19:12:49 +00:00
// Square now "conforms" to the `CustomStringConvertible` protocol
2015-11-10 00:09:48 +00:00
extension Square: CustomStringConvertible {
2014-08-24 03:48:10 +00:00
var description: String {
return "Area: \(self.getArea()) - ID: \(self.identifier)"
}
}
2015-08-04 03:05:22 +00:00
print("Square: \(mySquare)")
2014-08-24 03:48:10 +00:00
2014-08-24 05:48:54 +00:00
// You can also extend built-in types
extension Int {
var customProperty: String {
return "This is \(self)"
}
2015-10-08 03:11:24 +00:00
2014-08-24 05:48:54 +00:00
func multiplyBy(num: Int) -> Int {
return num * self
}
}
2015-08-04 03:05:22 +00:00
print(7.customProperty) // "This is 7"
2016-10-08 09:59:20 +00:00
print(14.multiplyBy(num: 3)) // 42
2014-08-24 05:48:54 +00:00
2014-10-04 17:08:23 +00:00
// Generics: Similar to Java and C#. Use the `where` keyword to specify the
2014-06-04 20:22:01 +00:00
// requirements of the generics.
2014-06-04 07:50:17 +00:00
2016-10-08 09:59:20 +00:00
func findIndex< T: Equatable > (array: [T], valueToFind: T) -> Int? {
for (index, value) in array.enumerated() {
2014-08-24 03:48:10 +00:00
if value == valueToFind {
return index
}
}
return nil
}
2016-10-08 09:59:20 +00:00
let foundAtIndex = findIndex(array: [1, 2, 3, 4], valueToFind: 3)
2015-08-04 03:05:22 +00:00
print(foundAtIndex == 2) // true
2014-08-24 05:48:54 +00:00
// Operators:
// Custom operators can start with the characters:
// / = - + * % < > ! & | ^ . ~
// or
// Unicode math, symbol, arrow, dingbat, and line/box drawing characters.
2016-10-08 09:59:20 +00:00
prefix operator !!!
2014-08-24 05:48:54 +00:00
2014-08-24 23:33:16 +00:00
// A prefix operator that triples the side length when used
2016-10-08 09:59:20 +00:00
prefix func !!! (shape: inout Square) -> Square {
2014-08-24 05:48:54 +00:00
shape.sideLength *= 3
return shape
}
2014-08-24 23:33:16 +00:00
// current value
2015-08-04 03:05:22 +00:00
print(mySquare.sideLength) // 4
2014-08-24 23:33:16 +00:00
// change side length using custom !!! operator, increases size by 3
!!!mySquare
2015-08-04 03:05:22 +00:00
print(mySquare.sideLength) // 12
2015-10-08 07:00:13 +00:00
// Operators can also be generics
2016-10-08 09:59:20 +00:00
infix operator < - >
func < - > < T: Equatable > (a: inout T, b: inout T) {
2015-10-08 07:00:13 +00:00
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"
2014-07-16 10:20:57 +00:00
```