You are on page 1of 36

iOS applications with Swift

Make swiftly iOS development

Telerik Academy Plus

Telerik Academy
About Me
Doncho Minkov

Senior Technical Trainer

@ Telerik Software Academy
Contestant in the Informatics
Experience with Web and Mobile apps
Proficient with JavaScript and .NET
Email: doncho.minkov [at]
Table of Contents
Swift overview
Language syntax and specifics
Variables, structures and optional values
Control structures: if-else, switch, loops
Functions as first-class objects, closures, etc
OOP: classes, protocols, enums, extensions
Syntactic sugar: destructuring assignments
iOS applications with Swift
Handling gestures on the UI
Using graphics and animations
Swift Overview
What and Why?
Swift Overview
Swift is an innovative new programming
language for Cocoa and Cocoa Touch
Writing code is interactive and fun
The syntax is concise yet expressive
Apps run lightning-fast
Swift can be used for creating brand-new iOS
and OS X apps
Or to add features to applications written with
Swift Overview
Swift was introduced at WWDC 2014 as the
"new way to create apps for iPhone and iPad"
Objective-C without the C
Descendant to Objective-C
No pointers
Functions as first-class objects
Influenced from many modern languages:
JavaScript, Objective-C, C#, Python, Scala, etc
Swift Experiment Techniques
Since Swift is pretty new and still
exploring Apple has provided ways to
experiment the new features:
REPL (Read Eval Print Loop)
Run swift code from the Terminal
Xcode interactive Playground
Immediate code evaluation, while typing
Using the Swift REPL

To use the REPL of Swift follow the steps:

1. Start
2. Type $ swift

3. Type code directly inside the Terminal

4. The output is shown directly on the Terminal
You can also execute code from a swift file:
$ swift print-primes.swift
Using the Swift REPL
Live Demo
Using the Swift Playground

Apple has provided a Playground for

experimenting with Swift features inside Xcode
Write code and receive immediate result
Great for exploring Swift features and research
Allows fast testing of features and syntax
Going to the
Live Demo
Swift Syntax and Specifics
Laying the groundwork
Swift Syntax Overview
Swift syntax:

Variables and contants

Control structures
If-else, loops, switch
Data structures
Arrays, dictionaries, sets
Classes and structures
Init functions, methods, properties, fields
Protocols, extensions, inheritance
Data types, Variables and
var and let, primitive and reference, functions
Data Types in Swift
Swift supports the common data types, coming
from Objective-C
Int for creating integers numbers
UInt for creating unsigned integer numbers
Float, Double for floating-point numbers
Bool for Boolean values
[] for array, [Int] for array of integers
Swift supports both structures and classes
Structures create primitive types
Classes create reference types
Data Types, Variables
and Constants
Swift provides two keywords for creating
let and var
var creates a mutable variable
Its value can be changed any time
The type can be provided or inferred
let creates immutable variable
If the type is struct, its value cannot be changed
If the type is object, it can be changed only
Using let and var
Creating a variable of type integer:
Using let
let numberOfCourses: Int = 15
numberOfCourses = 16 // compiler error

Using var
var count :Int = 15
count++ //this is Ok

Variable types are inferred

The compiler will understand that you want Int
Works with let and var
let numberOfCourses = 15 var count = 15
Using var and let
Live Demo
Defining Functions in Swift
Functions in Swift have the following syntax:
Function returning void:
func printMessage(message: String) {
println(String(format: "Message: %@", message))

Function returning integer:

func sum(x: Int, y: Int) -> Int {
return x + y

Function with parameters put inside an array:

func sum(numbers: Int) -> Int {
var sum = 0
for number in numbers { sum += number}
return sum
Functions in Swift
Live Demo
Control Flow Structures:
if-else and switch
Swift supports the standard control flow
if-else switch
switch language {
if condition {
case "Objective-C", "Swift":
//run code
case "Java":
else if condition2 {
//run other code
case "C#":
println("Windows Phone")
else {
//run third code
println("Another platform")
If-else and Switch
Live Demo
Control Flow Structures: Loops
Swift supports the standard control flow
for loop
for i in 0...11 {
//i gets the values from 0 to 11, inclusive

for i in 0..<11 {
//i gets the values from 0 to 10, inclusive

while loop
while condition {
//run code
Swift Data Structures
Swift Data Structures
Swift has built-in arrays and dictionaries
let creates immutable dictionary/array
var creates mutable dictionary/array
Immutable arrays:
let numbers: [Int] = [1, 2, 3, 4, 5]
numbers[2] = -1 //compile error
numbers.append(6) //compile error

Mutable arrays:
var names: [String] = ["Doncho", "Ivaylo"]
names.append("Nikolay") //Doncho, Ivaylo, Nikolay
names.append("Pesho") //Doncho, Ivaylo, Nikolay, Pesho
names[3] = "Evlogi" //Doncho, Ivaylo, Nikolay, Evlogi

Same for dictionaries

Arrays and Dictionaries
Live Demo
Classes and Structures
Introducing the OOP principles in Swift
Swift Classes
Swift is an OOP language

Has classes, structures and protocols

class Square{
var x: Float
var y: Float
var side: Float
init(x: Float, y: Float, side: Float){
self.x = x
self.y = y
self.side = side

func calcArea() -> Float{

return self.side * self.side
Creating Simple Classes
Live Demo
Protocols in Swift
Protocols in Swift are pretty much the same as in
Almost like interfaces in C# and Java
They provide a public interface for the conforming
classes to implement
protocol Movable{
func moveToX(x: Int, y: Int)
func moveByDx(dx:Int, dy: Int)
class Shape: Movable{
func moveToX(x: Int, y: Int){
func moveByDx(dx: Int, dy: Int){
Protocols in Swift
Live Demo
UIKit Animations
UIKit Animations
A limited number of animatable properties
Custom animatable properties cannot be created
All animations are disabled by default
Animations are enabled only when using special
let view = UIView(frame: CGRectMake(x, y, w, h))

UIView.animateWithDuration( 1.0
animations: {
let frame = CGRectMake(x + 100,
y + 100,
view.frame = frame
UIKit Animations:
Animation Blocks
UIKit Animations with Animation Blocks
UIView.animateWithDuration( 1.0
delay: 1.0
options: UIViewAnimationOptions.CurveEaseIn
animations: {
let frame = CGRectMake(x + 100,
y + 100,
view.frame = frame
completion: {
view.alpha = 0.5
UIKit Animations
Live Demo
iOS Apps with Swift

, BG Coder - - online judge

SEO - ,
, iPhone, Android, WP7, PhoneGap
, HTML, CSS, JavaScript, Photoshop -
free C# book, C#, Java, C# -
" "
" cloud " C# , ,