SlideShare a Scribd company logo
1 of 70
Download to read offline
Swift
Kaz Yoshikawa
June 2014
Swift
What's Swift
What's Swift
Variables
// variable
var name: String = "John Doe"
var temperature: Double = 36.5
var year: Int = 2014
var visible: Bool = true
!
year = 2015
Type Inference
// variable
var name: String = "John Doe"
var temperature: Double = 36.5
var year: Int = 2014
var visible: Bool = true
!
name = "Steve Jobs" // OK
temperature = 36.8 // OK
year = 2015 // OK
visible = false // OK
Type Inference
// variable
var name: String = "John Doe"
var temperature: Double = 36.5
var year: Int = 2014
var visible: Bool = true
!
name = "Steve Jobs" // OK
temperature = 36.8 // OK
year = 2015 // OK
visible = false // OK
Constants
// constants
let name: String = "John Doe"
let temperature: Double = 36.5
let year: Int = 2014
let visible: Bool = true
!
name = "Steve Jobs" // error
temperature = 36.8 // error
year = 2015 // error
visible = false // error
Unicode Names
let 🐶: Character = "🐶"
let 🐮: String = "🐮"
let dog: Character = "dog" // error
let cow: String = "cow"
let π = 3.1415927
let 言語 = "Japanese"
String Operation
let hello = "Hello"
let world = "World"
let greeting = hello + " " + world
// "Hello World"
!
let a = 3, b = 5
let result = "(a) + (b) = (a + b)"
// "3 + 5 = 8"
Array and Dictionary Literals
// Mixed Object Type Array
var array = ["Tokyo", 3, true]
!
// Typed Collection
var list1: String[] = ["Ja", "En", "Fr"]
var list2: String[] = ["Ja", "En", 2.0] //NG
!
// Dictionary
var legs = ["cat":4, "snake":0, "dog":2]
Array Access
// declaration
var items = ["Ja", "En", "Fr"]
!
// accessors
let item = items[1] // "En"
items.insert("De", atIndex: 0) // De,Ja, …
items.append("It") // De,Ja,En,Fr,It
items.removeAtIndex(1) // De,En,Fr,It
items.removeLast() // De,En,Fr
Array Access
var names = ["Robert", "Ken"]
!
// append
names += "Joe"
names += ["Mike", "Paul"]
!
// replace
names[0] = "Bob"
names[2..4] = ["Jim", "Alex", "Bill"]
// Bob,Ken,Jim,Alex,Bill,Paul
Dictionary
var dict = ["Ja":"Japanese", "En":"English",
"Fr":"French"]
!
// read access
let lang = dict["Ja"] // "Japanese"
!
// write access
dict["De"] = "German"
dict["Ja"] = nil
Tuple
let rgb = (128, 255, 0)
let result = (404, "Not Found")
let item = (2, "banana", 0.75)
!
let r = rgb.0 // 128
let code = result.0 // 404
let name = item.1 // "banana"
// non optional
var str1: String = nil // error: non-opt
var str2: String = "" // OK
var num1: Int = nil // error: non-opt
var num2: Int = 496 // OK
!
// optional
var str3: String? = nil // OK: optional
var num3: Int? = nil // OK: optional
Optional & Non Optional
If Statement
!
if items.count > 0 {
// OK: some code here
}
!
if (items.count > 0) {
// OK: parentheses are optional
}
!
// error (requires curly brace)
if (items.count > 0) println("")
If Statement
if foo == 0 {
// some code here
} else if foo == 1 {
// some code here
} else {
// some code here
}
Switch Statement
// good
switch foo {
case 0: println("0")
case 1: println("1")
default: println("default")
}
// 1: break statement is not necessary!
// 2: default clause is mandatory!
// 3: fallthrough keyword available
Switch Statement
switch foo {
// multiple value
case 1, 3, 5: println("1,3,5");
case 2, 4, 6: println("2,4,6");
case 7...9: println("7,8,9");
case 10..100: println("10~99");
default: println("other");
}
Switch Statement

with Tuple
switch rgb {
case (0.0...0.25, 0.0...0.25, 0.0...0.25):
println("too dark")
case (0.75...1.0, 0.75...1.0, 0.75...1.0):
println("too bright")
case let (r, g, b) where r==g && g==b:
println("quite grey")
default:
println("just right");
}
switch rgb {
case (0.0...0.25, 0.0...0.25, 0.0...0.25):
println("too dark")
case (0.75...1.0, 0.75...1.0, 0.75...1.0):
println("too bright")
case let (r, g, b) where r==g && g==b:
println("quite grey")
default:
println("just right");
}
Switch Statement

with where clause
Loops
// while loop
while !done { /*code*/ }
!
// do while loop
do { /*code*/ } while !done
!
// for loop
for var i = 0 ; i < 100 ; ++i { /*code*/ }
For-In Statement
for ch in "123" {
// "1", "2", "3"
}
!
for num in 1...5 {
// 1, 2, 3, 4, 5
}
!
for num in 1..5 {
// 1, 2, 3, 4, not 5
}
For-In
// array
for item in ["En", "Ja", "Fr"] {
// "En", "Ja", "Fr"
}
// dictionary
var items = ["Ja":"Japanese","En":"English"]
for (code, name) in items {
println("(code): (name)")
}
Functions
// no parameter
func foo() { … }
// with parameter
func bar(name: String) { … }
// with default parameter
func buz(name: String = "World") { … }
!
foo()
bar("Tokyo")
buz()
Functions with Returning
Values
func foo() -> String {
return "foo"
}
func bar() -> String? {
return nil;
}
func buz() -> (Int, String) {
return (404, "Not Found")
}
Optional Return Type
func indexOfString(string: String,
array: String[]) -> Int {
for (index, value) in enumerate(array) {
return index;
}
return nil; // error: non optional
}
Optional Return Type
func indexOfString(string: String,
array: String[]) -> Int? {
for (index, value) in enumerate(array) {
return index;
}
return nil; // OK: optional
}
Optional Parameters
func foo(name: String) {
}
!
func bar(name: String?) {
}
!
foo("Ken") // OK
foo(nil) // NG: not optional
bar("Ken") // OK
bar(nil) // OK: optional
Closures
// Closure
let foo: () -> () = { println("foo") }
// Closure (short form)
var bar = { println("bar") }
// function
func buz() -> () { println("buz") }
!
bar() // "bar"
bar = foo; bar() // "foo"
bar = buz; bar() // "buz"
class Shape {
var center:CGPoint;
init(center:CGPoint) {
self.center = center;
}
}
class Circle : Shape {
var radius: Double;
init(center:CGPoint, radius:Double) {
self.radius = radius;
super.init(center: center)
}
}
Classes
class Counter {
var value: Int = 0
init() {
}
func increment() {
value++
}
}
!
var counter = Counter() // no alloc
counter.increment()
println(counter.value) // 1
Properties and Methods
class Range {
var location, length: Double;
init(location:Double, length:Double) {
self.location = location;
self.length = length;
}
var mid: Double {
get {
return location + length/2.0;
}
set {
location = newValue - length/2.0;
}
} // if no setter then readonly property
}
Getter & Setter
class Ship {
var health: Double
}
!
class Car : Mammal {
override var description: String {
return super.description + ".Ape"
}
}
Overriding Properties
class User {
var name: String = "";
}
!
class Admin : User {
override var name: String {
willSet {
println("(newValue)");
}
didSet {
println("(oldValue)");
}
}
}
Property Observers
struct Point {
var x, y: Double
}
struct Size {
var width, height: Double;
}
struct Rect {
var origin: Point;
var size: Size;
}
var point = Point(x:0, y:0)
var size = Size(width:640, height:480)
var rect = Rect(origin: point, size: size)
Structures
struct Rect {
var origin: Point;
var size: Size;
!
var area: Double {
return size.width * size.height;
}
func isSquare() -> Bool {
return size.width == size.height;
}
}
Structures
Classes & Structures
• Classes
• instances are passed by reference
• subclassing
!
• Structures
• instances are passed by value
• no subclassing
struct Point {
var x, y: Double
mutating func hello(dx: Double, dy: Double)
{
x += dx;
y += dy;
}
}
!
var pt1 = Point(x: 0.0, y: 0.0)
pt1.hello(10, dy: 0)
!
let pt2 = Point(x: 0.0, y: 0.0)
pt2.moveBy(10, dy: 0) // error mutating const
Mutating a Structure
enum
enum Direction {
case Left, Right
init() {
self = .Left;
}
var description: String {
switch self {
case .Left: return "Left"
case .Right: return "Right"
}
}
}
var direction1 = Direction.Left;
direction1 = .Right;
var direction2 = Direction()
println(direction1.description)
enum Direction {
case Left, Right
init() {
self = .Left;
}
var description: String {
switch self {
case .Left: return "Left"
case .Right: return "Right"
}
}
}
var direction1 = Direction.Left;
direction1 = .Right;
var direction2 = Direction()
println(direction1.description)
enum
Nested Types
class Book
{
enum Direction {
case Left, Right
}
var direction: Direction;
var title: String?;
init () {
self.direction = .Left;
}
}
var book: Book = Book();
book.direction = .Right;
Extensions
extension Size {
mutating func scaleBy(scale: Double) {
width *= scale
height *= scale
}
}
!
var size = Size(width: 100, height: 100);
size.scaleBy(2.0)
Extensions
extension Int {
func repetitions(task: () -> ()) {
for i in 0..self {
task()
}
}
}
!
32.repetitions({
println("Hello")
})
Extension
extension Double {
var km: Double { return self * 1_000.0 }
var m: Double { return self }
var cm: Double { return self / 100.0 }
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
!
let marathon = 42.km + 195.m // 42,195.0
Generic
struct Stack<T> {
var elements = T[]()
mutating func push(element: T) {
elements.append(element)
}
mutating func pop() -> T {
return elements.removeLast()
}
}
var stack1 = Stack<Int>()
var stack2 = Stack<Point>()
var stack3 = Stack<Rect>()
Generic
var dic1: Dictionary =
Dictionary<String, String>()
!
var dic2: Dictionary =
Dictionary<String, Int>()
!
var dic3: Dictionary =
Dictionary<String, AnyObject>()
Anonymous
for (key, _) in dictionary {
println(key)
}
!
let (red, green, blue, _) = color.rgba;
Protocols
protocol Countable {
var count : Int { get };
func increment();
}
!
class PageCounter : Countable {
var count : Int = 0;
func increment() {
count++;
}
}
Designated Initializers and
Convenience Initializers
1. Designated initializers must
call a designated initializer
from their immediate
superclass.
2. Convenience initializers must
call another initializer
available in the same class.
3. Convenience initializers must
ultimately end up calling a
designated initializer.
Deinitialization
class SomeObject {
var fileHandle: NSFileHandle?
init(path: String) {
var fileURL = NSURL.fileURLWithPath(path);
var error: NSError? = nil;
fileHandle = NSFileHandle.

fileHandleForReadingFromURL(fileURL, error: &error)
}
func parse() -> AnyObject? {
// ...
return nil
}
deinit {
fileHandle?.closeFile()
}
}
Shorthand External
Parameter Names
func stringFromInt(#value:Int) -> String {
return "(value)"
}
func stringFromDouble(value:Double) -> 

String {
return "(value)"
}
var a = stringFromInt(value:256); // OK
var b = stringFromInt(256); // NG
var c = stringFromDouble(value:256.3); // NG
var d = stringFromDouble(256); // OK
Variadic Parameters
func total(numbers: Int...) -> Int {
var total: Int = 0;
for number in numbers {
total += number;
}
return total;
}
!
let sum = total(1, 2, 3, 4, 5)
In-Out Parameters
func swapInts(inout a: Int, inout b: Int) {
let c = a;
a = b;
b = c;
}
!
var a = 100;
var b = 200;
swapInts(&a, &b)
In-Out Parameters
func swapValues<T>(inout a: T, inout b: T) {
let temporaryA = a
a = b
b = temporaryA
}
// somehow rename to swap<T> causes error
!
var c = 1
var d = 2
swapValues(&c, &d)
Typealias
typealias AudioSample = UInt16
Labeled Statements
var strings: String[] =
["Hello World", "This is a pen", "Wow!"]
!
label: for string in strings {
for ch in string {
if (ch == "!") {
break label;
}
}
}
Closure Expression Syntax
reversed = sort(array, {
(s1: String, s2: String) -> Bool in
return s1 > s2
})
!
reversed = sort(array, { s1, s2 in
return s1 > s2 })
!
reversed = sort(array, { $0 > $1 })
Lazy Stored Properties
class BookManager {
init() {
println("BookManager")
}
}
class Book {
@lazy var manager = BookManager()
init() {
println("Book")
}
}
var book = Book()
book.manager
Subscript Syntax
class Paragraphs {
var strings = String[]();
// ...
subscript(index: Int) -> String {
get {
return strings[index]
}
set {
strings[index] = newValue
}
}
}
Identity Operators
• Identity Operators
• bridgeToObjectiveC
Downcasting
for object in objects {
let view = object as UIView
view.hidden = true
}
!
for view in objects as UIView[] {
view.hidden = true
}
Type Evaluation
for thing in things {
switch thing {
case 0 as Int: println("0 as Int")
case 0 as Double: println("0 as Double")
case let value as Int: println("Int")
case let value as Double where value > 0:
println("Double value > 0")
case is Double: println("Double")
case let string as String: println("")
case let (x, y) as (Double, Double):
println("(x),(y)")
case let v as Vector2D: println("Vector2D")
default: println("other")
}
}
operator
struct Vector2D {
var x = 0.0, y = 0.0
}
!
@infix func + (lhs: Vector2D, rhs: Vector2D) ->
Vector2D {
return Vector2D(x: lhs.x + rhs.x,
y: lhs.y + rhs.y)
}
!
var v1 = Vector2D(x:10, y:20)
var v2 = v1 + Vector2D(x:3, y:4)
Note: @prefix @infix @postfix
Exception
Left Intentionally Blank
@synchronized
Left Intentionally Blank
!
Why not using NSLock?
Key Value Observer
class Car : NSObject {
var speed : Double = 0.0
override var description : String {
return "car: speed=(speed)"
}
}
class Dashboard : NSObject {
var car : Car;
init(car: Car) {
self.car = car;
super.init();
car.addObserver(self, forKeyPath: "speed", options: .New, context: nil)
}
override func observeValueForKeyPath(keyPath: String!, ofObject: AnyObject!,

change: NSDictionary!, context: CMutableVoidPointer) {
if keyPath == "speed" {
println(ofObject.description)
}
}
}
var car = Car()
var dashboard = Dashboard(car:car)
car.speed = 2
Class Method
class Foo {
// class var bar: Int = 0;
class func classMethod() {
}
}
!
var foo = Foo()
foo.dynamicType.classMethod()
Thank you

More Related Content

What's hot

Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, SwiftYandex
 
How to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftHow to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftGiordano Scalzo
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기진성 오
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.VimLin Yo-An
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation TutorialLorna Mitchell
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)ujihisa
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is hereSebastiano Armeli
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In PerlKang-min Liu
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象勇浩 赖
 
ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation JavascriptRamesh Nair
 
Zope component architechture
Zope component architechtureZope component architechture
Zope component architechtureAnatoly Bubenkov
 
Swift internals
Swift internalsSwift internals
Swift internalsJung Kim
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)David de Boer
 
FParsec Hands On - F#unctional Londoners 2014
FParsec Hands On -  F#unctional Londoners 2014FParsec Hands On -  F#unctional Londoners 2014
FParsec Hands On - F#unctional Londoners 2014Phillip Trelford
 
Groovy grails types, operators, objects
Groovy grails types, operators, objectsGroovy grails types, operators, objects
Groovy grails types, operators, objectsHusain Dalal
 

What's hot (20)

Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
 
Swift Introduction
Swift IntroductionSwift Introduction
Swift Introduction
 
How to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftHow to Clone Flappy Bird in Swift
How to Clone Flappy Bird in Swift
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 
EcmaScript 6 - The future is here
EcmaScript 6 - The future is hereEcmaScript 6 - The future is here
EcmaScript 6 - The future is here
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
 
ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation Javascript
 
Zope component architechture
Zope component architechtureZope component architechture
Zope component architechture
 
Swift internals
Swift internalsSwift internals
Swift internals
 
Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)Being functional in PHP (DPC 2016)
Being functional in PHP (DPC 2016)
 
Scala vs Ruby
Scala vs RubyScala vs Ruby
Scala vs Ruby
 
Kotlin
KotlinKotlin
Kotlin
 
FParsec Hands On - F#unctional Londoners 2014
FParsec Hands On -  F#unctional Londoners 2014FParsec Hands On -  F#unctional Londoners 2014
FParsec Hands On - F#unctional Londoners 2014
 
Groovy grails types, operators, objects
Groovy grails types, operators, objectsGroovy grails types, operators, objects
Groovy grails types, operators, objects
 

Viewers also liked

Viewers also liked (20)

Programming Complex Algorithm in Swift
Programming Complex Algorithm in SwiftProgramming Complex Algorithm in Swift
Programming Complex Algorithm in Swift
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05c
 
Extracting text from PDF (iOS)
Extracting text from PDF (iOS)Extracting text from PDF (iOS)
Extracting text from PDF (iOS)
 
Newsstand
NewsstandNewsstand
Newsstand
 
Investing in Mobile Games: What Investors Are Looking For | Tom van Dam
Investing in Mobile Games: What Investors Are Looking For | Tom van DamInvesting in Mobile Games: What Investors Are Looking For | Tom van Dam
Investing in Mobile Games: What Investors Are Looking For | Tom van Dam
 
Learn basics
Learn basicsLearn basics
Learn basics
 
English Teacher
English TeacherEnglish Teacher
English Teacher
 
подсчет вариантов с помощью графов
подсчет вариантов с помощью графовподсчет вариантов с помощью графов
подсчет вариантов с помощью графов
 
учимся видеть прекрасное
учимся  видеть прекрасноеучимся  видеть прекрасное
учимся видеть прекрасное
 
Lirik lagu
Lirik laguLirik lagu
Lirik lagu
 
Ats powerpoint
Ats powerpointAts powerpoint
Ats powerpoint
 
Multimedia...
Multimedia...Multimedia...
Multimedia...
 
Leo minor
Leo minorLeo minor
Leo minor
 
Financial maths Questions
Financial maths QuestionsFinancial maths Questions
Financial maths Questions
 
Microbiology .
Microbiology . Microbiology .
Microbiology .
 
текст открытый урок
текст открытый уроктекст открытый урок
текст открытый урок
 
Zagadki jesienne
Zagadki jesienneZagadki jesienne
Zagadki jesienne
 
Plantillas de mandalas
Plantillas de mandalasPlantillas de mandalas
Plantillas de mandalas
 
Excel charts lesson 8
Excel charts lesson 8Excel charts lesson 8
Excel charts lesson 8
 
Dis organizacional
Dis organizacionalDis organizacional
Dis organizacional
 

Similar to Swift Language Essentials

Introduction to ECMAScript 2015
Introduction to ECMAScript 2015Introduction to ECMAScript 2015
Introduction to ECMAScript 2015Tomasz Dziuda
 
JavaScript 1 for high school
JavaScript 1 for high schoolJavaScript 1 for high school
JavaScript 1 for high schooljekkilekki
 
Swift - the future of iOS app development
Swift - the future of iOS app developmentSwift - the future of iOS app development
Swift - the future of iOS app developmentopenak
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick touraztack
 
ScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyGautam Rege
 
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Gesh Markov
 
NUS iOS Swift Talk
NUS iOS Swift TalkNUS iOS Swift Talk
NUS iOS Swift TalkGabriel Lim
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kirill Rozov
 
Stuff you didn't know about action script
Stuff you didn't know about action scriptStuff you didn't know about action script
Stuff you didn't know about action scriptChristophe Herreman
 
【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽
【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽
【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽tbosstraining
 
The Great Scala Makeover
The Great Scala MakeoverThe Great Scala Makeover
The Great Scala MakeoverGarth Gilmour
 
What Swift can teach us all
What Swift can teach us allWhat Swift can teach us all
What Swift can teach us allPablo Villar
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlinThijs Suijten
 
A comparison between C# and Java
A comparison between C# and JavaA comparison between C# and Java
A comparison between C# and JavaAli MasudianPour
 
Coffeescript: No really, it's just Javascript
Coffeescript: No really, it's just JavascriptCoffeescript: No really, it's just Javascript
Coffeescript: No really, it's just JavascriptBrian Mann
 

Similar to Swift Language Essentials (20)

Quick swift tour
Quick swift tourQuick swift tour
Quick swift tour
 
Introduction to ECMAScript 2015
Introduction to ECMAScript 2015Introduction to ECMAScript 2015
Introduction to ECMAScript 2015
 
JavaScript 1 for high school
JavaScript 1 for high schoolJavaScript 1 for high school
JavaScript 1 for high school
 
Swift - the future of iOS app development
Swift - the future of iOS app developmentSwift - the future of iOS app development
Swift - the future of iOS app development
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
 
ScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyScotRuby - Dark side of ruby
ScotRuby - Dark side of ruby
 
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)
 
NUS iOS Swift Talk
NUS iOS Swift TalkNUS iOS Swift Talk
NUS iOS Swift Talk
 
ES6, WTF?
ES6, WTF?ES6, WTF?
ES6, WTF?
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
 
Stuff you didn't know about action script
Stuff you didn't know about action scriptStuff you didn't know about action script
Stuff you didn't know about action script
 
【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽
【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽
【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽
 
Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015Pooya Khaloo Presentation on IWMC 2015
Pooya Khaloo Presentation on IWMC 2015
 
The Great Scala Makeover
The Great Scala MakeoverThe Great Scala Makeover
The Great Scala Makeover
 
What Swift can teach us all
What Swift can teach us allWhat Swift can teach us all
What Swift can teach us all
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlin
 
A comparison between C# and Java
A comparison between C# and JavaA comparison between C# and Java
A comparison between C# and Java
 
Coffeescript: No really, it's just Javascript
Coffeescript: No really, it's just JavascriptCoffeescript: No really, it's just Javascript
Coffeescript: No really, it's just Javascript
 
Intro toswift1
Intro toswift1Intro toswift1
Intro toswift1
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Recently uploaded (20)

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

Swift Language Essentials

  • 5. Variables // variable var name: String = "John Doe" var temperature: Double = 36.5 var year: Int = 2014 var visible: Bool = true ! year = 2015
  • 6. Type Inference // variable var name: String = "John Doe" var temperature: Double = 36.5 var year: Int = 2014 var visible: Bool = true ! name = "Steve Jobs" // OK temperature = 36.8 // OK year = 2015 // OK visible = false // OK
  • 7. Type Inference // variable var name: String = "John Doe" var temperature: Double = 36.5 var year: Int = 2014 var visible: Bool = true ! name = "Steve Jobs" // OK temperature = 36.8 // OK year = 2015 // OK visible = false // OK
  • 8. Constants // constants let name: String = "John Doe" let temperature: Double = 36.5 let year: Int = 2014 let visible: Bool = true ! name = "Steve Jobs" // error temperature = 36.8 // error year = 2015 // error visible = false // error
  • 9. Unicode Names let 🐶: Character = "🐶" let 🐮: String = "🐮" let dog: Character = "dog" // error let cow: String = "cow" let π = 3.1415927 let 言語 = "Japanese"
  • 10. String Operation let hello = "Hello" let world = "World" let greeting = hello + " " + world // "Hello World" ! let a = 3, b = 5 let result = "(a) + (b) = (a + b)" // "3 + 5 = 8"
  • 11. Array and Dictionary Literals // Mixed Object Type Array var array = ["Tokyo", 3, true] ! // Typed Collection var list1: String[] = ["Ja", "En", "Fr"] var list2: String[] = ["Ja", "En", 2.0] //NG ! // Dictionary var legs = ["cat":4, "snake":0, "dog":2]
  • 12. Array Access // declaration var items = ["Ja", "En", "Fr"] ! // accessors let item = items[1] // "En" items.insert("De", atIndex: 0) // De,Ja, … items.append("It") // De,Ja,En,Fr,It items.removeAtIndex(1) // De,En,Fr,It items.removeLast() // De,En,Fr
  • 13. Array Access var names = ["Robert", "Ken"] ! // append names += "Joe" names += ["Mike", "Paul"] ! // replace names[0] = "Bob" names[2..4] = ["Jim", "Alex", "Bill"] // Bob,Ken,Jim,Alex,Bill,Paul
  • 14. Dictionary var dict = ["Ja":"Japanese", "En":"English", "Fr":"French"] ! // read access let lang = dict["Ja"] // "Japanese" ! // write access dict["De"] = "German" dict["Ja"] = nil
  • 15. Tuple let rgb = (128, 255, 0) let result = (404, "Not Found") let item = (2, "banana", 0.75) ! let r = rgb.0 // 128 let code = result.0 // 404 let name = item.1 // "banana"
  • 16. // non optional var str1: String = nil // error: non-opt var str2: String = "" // OK var num1: Int = nil // error: non-opt var num2: Int = 496 // OK ! // optional var str3: String? = nil // OK: optional var num3: Int? = nil // OK: optional Optional & Non Optional
  • 17. If Statement ! if items.count > 0 { // OK: some code here } ! if (items.count > 0) { // OK: parentheses are optional } ! // error (requires curly brace) if (items.count > 0) println("")
  • 18. If Statement if foo == 0 { // some code here } else if foo == 1 { // some code here } else { // some code here }
  • 19. Switch Statement // good switch foo { case 0: println("0") case 1: println("1") default: println("default") } // 1: break statement is not necessary! // 2: default clause is mandatory! // 3: fallthrough keyword available
  • 20. Switch Statement switch foo { // multiple value case 1, 3, 5: println("1,3,5"); case 2, 4, 6: println("2,4,6"); case 7...9: println("7,8,9"); case 10..100: println("10~99"); default: println("other"); }
  • 21. Switch Statement
 with Tuple switch rgb { case (0.0...0.25, 0.0...0.25, 0.0...0.25): println("too dark") case (0.75...1.0, 0.75...1.0, 0.75...1.0): println("too bright") case let (r, g, b) where r==g && g==b: println("quite grey") default: println("just right"); }
  • 22. switch rgb { case (0.0...0.25, 0.0...0.25, 0.0...0.25): println("too dark") case (0.75...1.0, 0.75...1.0, 0.75...1.0): println("too bright") case let (r, g, b) where r==g && g==b: println("quite grey") default: println("just right"); } Switch Statement
 with where clause
  • 23. Loops // while loop while !done { /*code*/ } ! // do while loop do { /*code*/ } while !done ! // for loop for var i = 0 ; i < 100 ; ++i { /*code*/ }
  • 24. For-In Statement for ch in "123" { // "1", "2", "3" } ! for num in 1...5 { // 1, 2, 3, 4, 5 } ! for num in 1..5 { // 1, 2, 3, 4, not 5 }
  • 25. For-In // array for item in ["En", "Ja", "Fr"] { // "En", "Ja", "Fr" } // dictionary var items = ["Ja":"Japanese","En":"English"] for (code, name) in items { println("(code): (name)") }
  • 26. Functions // no parameter func foo() { … } // with parameter func bar(name: String) { … } // with default parameter func buz(name: String = "World") { … } ! foo() bar("Tokyo") buz()
  • 27. Functions with Returning Values func foo() -> String { return "foo" } func bar() -> String? { return nil; } func buz() -> (Int, String) { return (404, "Not Found") }
  • 28. Optional Return Type func indexOfString(string: String, array: String[]) -> Int { for (index, value) in enumerate(array) { return index; } return nil; // error: non optional }
  • 29. Optional Return Type func indexOfString(string: String, array: String[]) -> Int? { for (index, value) in enumerate(array) { return index; } return nil; // OK: optional }
  • 30. Optional Parameters func foo(name: String) { } ! func bar(name: String?) { } ! foo("Ken") // OK foo(nil) // NG: not optional bar("Ken") // OK bar(nil) // OK: optional
  • 31. Closures // Closure let foo: () -> () = { println("foo") } // Closure (short form) var bar = { println("bar") } // function func buz() -> () { println("buz") } ! bar() // "bar" bar = foo; bar() // "foo" bar = buz; bar() // "buz"
  • 32. class Shape { var center:CGPoint; init(center:CGPoint) { self.center = center; } } class Circle : Shape { var radius: Double; init(center:CGPoint, radius:Double) { self.radius = radius; super.init(center: center) } } Classes
  • 33. class Counter { var value: Int = 0 init() { } func increment() { value++ } } ! var counter = Counter() // no alloc counter.increment() println(counter.value) // 1 Properties and Methods
  • 34. class Range { var location, length: Double; init(location:Double, length:Double) { self.location = location; self.length = length; } var mid: Double { get { return location + length/2.0; } set { location = newValue - length/2.0; } } // if no setter then readonly property } Getter & Setter
  • 35. class Ship { var health: Double } ! class Car : Mammal { override var description: String { return super.description + ".Ape" } } Overriding Properties
  • 36. class User { var name: String = ""; } ! class Admin : User { override var name: String { willSet { println("(newValue)"); } didSet { println("(oldValue)"); } } } Property Observers
  • 37. struct Point { var x, y: Double } struct Size { var width, height: Double; } struct Rect { var origin: Point; var size: Size; } var point = Point(x:0, y:0) var size = Size(width:640, height:480) var rect = Rect(origin: point, size: size) Structures
  • 38. struct Rect { var origin: Point; var size: Size; ! var area: Double { return size.width * size.height; } func isSquare() -> Bool { return size.width == size.height; } } Structures
  • 39. Classes & Structures • Classes • instances are passed by reference • subclassing ! • Structures • instances are passed by value • no subclassing
  • 40. struct Point { var x, y: Double mutating func hello(dx: Double, dy: Double) { x += dx; y += dy; } } ! var pt1 = Point(x: 0.0, y: 0.0) pt1.hello(10, dy: 0) ! let pt2 = Point(x: 0.0, y: 0.0) pt2.moveBy(10, dy: 0) // error mutating const Mutating a Structure
  • 41. enum enum Direction { case Left, Right init() { self = .Left; } var description: String { switch self { case .Left: return "Left" case .Right: return "Right" } } } var direction1 = Direction.Left; direction1 = .Right; var direction2 = Direction() println(direction1.description)
  • 42. enum Direction { case Left, Right init() { self = .Left; } var description: String { switch self { case .Left: return "Left" case .Right: return "Right" } } } var direction1 = Direction.Left; direction1 = .Right; var direction2 = Direction() println(direction1.description) enum
  • 43. Nested Types class Book { enum Direction { case Left, Right } var direction: Direction; var title: String?; init () { self.direction = .Left; } } var book: Book = Book(); book.direction = .Right;
  • 44. Extensions extension Size { mutating func scaleBy(scale: Double) { width *= scale height *= scale } } ! var size = Size(width: 100, height: 100); size.scaleBy(2.0)
  • 45. Extensions extension Int { func repetitions(task: () -> ()) { for i in 0..self { task() } } } ! 32.repetitions({ println("Hello") })
  • 46. Extension extension Double { var km: Double { return self * 1_000.0 } var m: Double { return self } var cm: Double { return self / 100.0 } var mm: Double { return self / 1_000.0 } var ft: Double { return self / 3.28084 } } ! let marathon = 42.km + 195.m // 42,195.0
  • 47. Generic struct Stack<T> { var elements = T[]() mutating func push(element: T) { elements.append(element) } mutating func pop() -> T { return elements.removeLast() } } var stack1 = Stack<Int>() var stack2 = Stack<Point>() var stack3 = Stack<Rect>()
  • 48. Generic var dic1: Dictionary = Dictionary<String, String>() ! var dic2: Dictionary = Dictionary<String, Int>() ! var dic3: Dictionary = Dictionary<String, AnyObject>()
  • 49. Anonymous for (key, _) in dictionary { println(key) } ! let (red, green, blue, _) = color.rgba;
  • 50. Protocols protocol Countable { var count : Int { get }; func increment(); } ! class PageCounter : Countable { var count : Int = 0; func increment() { count++; } }
  • 51. Designated Initializers and Convenience Initializers 1. Designated initializers must call a designated initializer from their immediate superclass. 2. Convenience initializers must call another initializer available in the same class. 3. Convenience initializers must ultimately end up calling a designated initializer.
  • 52. Deinitialization class SomeObject { var fileHandle: NSFileHandle? init(path: String) { var fileURL = NSURL.fileURLWithPath(path); var error: NSError? = nil; fileHandle = NSFileHandle.
 fileHandleForReadingFromURL(fileURL, error: &error) } func parse() -> AnyObject? { // ... return nil } deinit { fileHandle?.closeFile() } }
  • 53. Shorthand External Parameter Names func stringFromInt(#value:Int) -> String { return "(value)" } func stringFromDouble(value:Double) -> 
 String { return "(value)" } var a = stringFromInt(value:256); // OK var b = stringFromInt(256); // NG var c = stringFromDouble(value:256.3); // NG var d = stringFromDouble(256); // OK
  • 54. Variadic Parameters func total(numbers: Int...) -> Int { var total: Int = 0; for number in numbers { total += number; } return total; } ! let sum = total(1, 2, 3, 4, 5)
  • 55. In-Out Parameters func swapInts(inout a: Int, inout b: Int) { let c = a; a = b; b = c; } ! var a = 100; var b = 200; swapInts(&a, &b)
  • 56. In-Out Parameters func swapValues<T>(inout a: T, inout b: T) { let temporaryA = a a = b b = temporaryA } // somehow rename to swap<T> causes error ! var c = 1 var d = 2 swapValues(&c, &d)
  • 58. Labeled Statements var strings: String[] = ["Hello World", "This is a pen", "Wow!"] ! label: for string in strings { for ch in string { if (ch == "!") { break label; } } }
  • 59. Closure Expression Syntax reversed = sort(array, { (s1: String, s2: String) -> Bool in return s1 > s2 }) ! reversed = sort(array, { s1, s2 in return s1 > s2 }) ! reversed = sort(array, { $0 > $1 })
  • 60. Lazy Stored Properties class BookManager { init() { println("BookManager") } } class Book { @lazy var manager = BookManager() init() { println("Book") } } var book = Book() book.manager
  • 61. Subscript Syntax class Paragraphs { var strings = String[](); // ... subscript(index: Int) -> String { get { return strings[index] } set { strings[index] = newValue } } }
  • 62. Identity Operators • Identity Operators • bridgeToObjectiveC
  • 63. Downcasting for object in objects { let view = object as UIView view.hidden = true } ! for view in objects as UIView[] { view.hidden = true }
  • 64. Type Evaluation for thing in things { switch thing { case 0 as Int: println("0 as Int") case 0 as Double: println("0 as Double") case let value as Int: println("Int") case let value as Double where value > 0: println("Double value > 0") case is Double: println("Double") case let string as String: println("") case let (x, y) as (Double, Double): println("(x),(y)") case let v as Vector2D: println("Vector2D") default: println("other") } }
  • 65. operator struct Vector2D { var x = 0.0, y = 0.0 } ! @infix func + (lhs: Vector2D, rhs: Vector2D) -> Vector2D { return Vector2D(x: lhs.x + rhs.x, y: lhs.y + rhs.y) } ! var v1 = Vector2D(x:10, y:20) var v2 = v1 + Vector2D(x:3, y:4) Note: @prefix @infix @postfix
  • 68. Key Value Observer class Car : NSObject { var speed : Double = 0.0 override var description : String { return "car: speed=(speed)" } } class Dashboard : NSObject { var car : Car; init(car: Car) { self.car = car; super.init(); car.addObserver(self, forKeyPath: "speed", options: .New, context: nil) } override func observeValueForKeyPath(keyPath: String!, ofObject: AnyObject!,
 change: NSDictionary!, context: CMutableVoidPointer) { if keyPath == "speed" { println(ofObject.description) } } } var car = Car() var dashboard = Dashboard(car:car) car.speed = 2
  • 69. Class Method class Foo { // class var bar: Int = 0; class func classMethod() { } } ! var foo = Foo() foo.dynamicType.classMethod()