CS333

Mobile Development


Ilya Loshkarev
loshkarev.i@gmail.com

Apple Logo
iOS Logo
XCode Logo
Swift Logo

Brief history of iOS

1982

Brian Cox and Tom Love implement object oriented extention for C language

1986

Brian Cox publishes first book on Objective-C framework

1988

Steve Jobs presents NeXT Computer

NeXTSTEP OS with Objective-C as primary system language

1996

Apple acquires NeXT

NeXTSTEP becomes MacOS

OPENSTEP API becomes COCOA

Apple becomes holder of Objective-C rights

2001

2003

Apple releases iPod

iTunes Store

iPod Sales

2007

"Apple reinvents the phone"

Say hello to iPhone

Touch Interface

600 MHz CPU

128 MB RAM

16 GB storage

No access to file system

No third party apps

No 3G

No wireless features

Darwin 9.0

MacOS Kernel

Core Foundation

Foundation Kit

Cocoa Touch

UIKit

2008

App Store

Xcode 3.1

iOS SDK

3G and GPS

2009

Copy and Paste

Bluetooth and USB

Push Notifications

inApps and Subscriptions

2010

Multitasking

Retina

iAds

2014

Swift

Origins of Swift

Chris Latner

Chris Lattner

@clattner_llvm

Senior Director and Architect, Developer Tools Department, Apple Inc

http://llvm.org

2002

"LLVM: An Infrastructure for Multi-Stage Optimization"

Chris Lattner, Masters Thesis, Computer Science Dept., University of Illinois at Urbana-Champaign
LLVM Logo

2005

Apple Inc hires Chris Lattner as Senior Compiler Engineer

Clang project is born

I started work on the Swift Programming Language in July of 2010.

A few other (amazing) people started contributing in earnest late in 2011, and it became a major focus for the Apple Developer Tools group in July 2013

Chris Lattner

2010

Swift Logo

2014

September 9th – Swift 1.0

https://swift.org

2015

September 21th – Swift 2.0

December 8th – Swift goes open source

Swift Logo

2016

March 16th – Swift 2.2

September 7th – Swift 2.3 & Swift 3.0 ?

Reasons to go Swift

Modern Syntax?

Modern Syntax


									@interface AppDelegate : UIResponder <UIApplicationDelegate>
									@property (strong, nonatomic) UIWindow *window;
									@end
									@implementation AppDelegate
									- (BOOL)application: (UIApplication *)application
											didFinishLaunchingWithOptions:
											(NSDictionary *)launchOptions {
										return YES;
									}
									@end

							class AppDelegate: UIResponder, UIApplicationDelegate {
								var window: UIWindow?
								func application( application: UIApplication,
										didFinishLaunchingWithOptions
										launchOptions: [NSObject: AnyObject]?) -> Bool {
									return true
								}
							}
							

Safe Space

Swift Logo

Strong typing and type-safety

Initializers

Range Checks

Memory managment

Optionals


								if (myDelegate != nil) {
									if ([myDelegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
										[myDelegate scrollViewDidScroll:myScrollView];
									}
								}
							

								myDelegate?.scrollViewDidScroll?(myScrollView)
							

Optional chaining == 30% less code

Makes possibility of nil objects compile time errors!

Error Handling


								NSError *anyError;
								BOOL success = [receivedData writeToURL:someLocalFileURL
											options:0
											error:&anyError];
								if (!success) {
									NSLog(@"Write failed with error: %@", anyError);
								}
							

								do {
									try receivedData.writeToURL(someLocalFileURL, options:0)
								} catch let error as NSError {
									print("Write failed with error: \(error)")
								}
							

Native exception support!
(since Swift 2.2)

Perfomance

Source: yalantis.com

Better compile time optimisation!

The Catch-up Game

Source: github.com

Mix and Match


							// Special header provided for compiler with project options

							#import "MySuperObject.h"
						

							let myObject = MySuperObject()
						

Xcode allows full Swift ⇆ Objective-C interoperability!

Playground

Write Code

Interactive Swift experience

Get Errors

Get those errors as soon as possible

View results

  1. Strings
  2. Numbers
  1. Image
  2. UI

Let's take a look

Syntax

Constants and Variables


							let pi = 3.14
							var r = 1
							while r < 100 {
								let sqr = r * r * pi
								print("Radius: \(r); Square: \(sqr)")
								r += 1
							}
						
let decalres a constant
var declares a variable

Always declare a constant unless absolutely necessary

Basic Datatypes


							let i: Int = 1  // default Int is either Int32 or Int64
							let f: Float = 2.7
							let d: Double = 3.1
							let b: Bool = true
							let s: String = "hello"
						

Swift can infer type of a variable


								let x = 5, y = 3.142, z = true
								let names = ["Alex", "Anna", "Ivan", "Maria"]
						

Type Conversion

Swift is type-safe language


							var pi = 3 + 0.14 // literals don't have explicit type
							let three = 3
							let pointOneFour = 0.14
							pi = Double(three) + pointOneFour
							// implicit type conversions are not allowed
							let roundPi:Int = Int(pi)
						

All type conversions must be explicit

Functions


							func greet(person: String, from hometown: String) -> String {
								return "Hello, \(person)! Glad you could visit from \(hometown)!"
							}

							print(greet(person: "Ivan", from: "Rostov"))
						

Every parameter has a name and an argument label

Arguments must be labeled when function is called

Optionals


							let someNum = "3.14"
							let number = Double(someNum) // Double?
							if number != nil {
								print("parsed number: \(number!)") // unwraped value is 3.14
							}
						

Optional value contains either value or nil

Always make sure optional contains a value before foced unwraping

Conditional Statement


							let someChar: Character = "z"
							if someChar >= "0" && someChar <= "9" {
								print("This is a digit.")
							} else if someChar >= "a" && someChar <= "z" {
								print("This is a letter.")
							}
						

Curved braces are required

Optional Binding


							let someNum = "3.14"
							if let number = Double(someNum) { // number is Double!
								print("parsed number: \(number)")
							} else {
								print("\(someNum) is not a number") // number is not reachable
							}
						

Bound optional always contains a value

Conditional Statement – Switch


							let someChar: Character = "z"
							switch (someChar) {
								case "a":
									print("It is the first letter of the alphabet")
								case "b"..."y":
									print("It is some other letter of the alphabet")
								case "z":
									print("It is the last letter of the alphabet")
								default:
									print("It is some other character")
							}
						

Case must always have a body

Loops


							var i = 0
							while i < 5 {
								i += 1
							}
							repeat {
								i -= 1
							} while i > 0
						

							for i in 0...10 {
								for j in 0... {
									print(".")
								}
								print("\n")
							}
						

Related Resources

Swift-logo