MCSv3

Mobile Development


Ilya Loshkarev
loshkarev.i@gmail.com

Welcome

Language

Lab Hours (205)

  • Tuesday 13:45 - 15:20
  • Tuesday 15:30 - 17:25

Lectures

  • Tuesday 11:55 - 13:30
macOS Logo
iOS Logo
XCode Logo
Swift Logo

Brief history of iOS

1980

Smalltalk-80

first language to implement OOP
as a core design principles

1982

1986

  • Brad Cox and Tom Love start a company
  • Objective-C framework is released

1988

Steve Jobs presents NeXT Computer

NeXT Computer

"The Black Cube"
  • 25 MHz CPU
  • 8-16 MB RAM
  • Optical Disk Drive
  • NeXTSTEP OS

1996

Apple acquires NeXT

NeXTSTEP becomes MacOS

OPENSTEP API becomes COCOA

Apple becomes holder of Objective-C rights

2001

2003

iPod

iTunes Store

2007

"Apple reinvents the phone"

After "failure" with iTunes on Motorola ROHR E1

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

XNU Kernel

POSIX API

Core Foundation

Cocoa Touch

UIKit

2008

iOS SDK

App Store

Xcode 3.1

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

Goals for Swift

  • Safety
  • Expressiveness
  • Perfomance
  • Interoperability with Objective-C
Swift Logo

2014

September 9th – Swift 1.0

https://swift.org

2015

September 21th – Swift 2.0

Not bin compatable!

December 8th – Swift goes open source

Swift Logo

2016

March 16th – Swift 2.2

Exceptions!

September 7th – Swift 3.0

Not bin compatable!
Swift Logo

2017

Lattner leaves Apple

September 19th – Swift 4.0

Swift Logo

Future of Swift

Ted Kremenek
@tkremenek

Early 2019 – Swift 5.0

https://apple.github.io/swift-evolution/

Reasons to go Swift

Modern Syntax?

Obj-C Syntax


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

								@implementation AppDelegate
								- (BOOL)application: (UIApplication *)application
										didFinishLaunchingWithOptions:
										(NSDictionary *)launchOptions {
									return YES;
								}
								@end
							

Swift Syntax


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

Safe Space

Swift Logo

Strong typing & 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.write(to: 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

Swift REPL


							$ xcrun swift
							> 1 + 2
							$R0: Int = 3
							> let greeting = "Hello!"
							greeting: String = "Hello!"
							> print(greeting)
							Hello!
						

"Read Eval Print Loop"

Write Code

Integrated REPL Enviroment

Each line gets evaluated on built

Get Errors

Get those errors as soon as possible

View Results

  1. Strings
  2. Numbers
  1. Images
  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  // Int
							let pointOneFour = 0.14 // Double
							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)!"
							}

							greet(person: "Ilya", 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 type contains either value or nil

Always make sure optional contains a value
before you unwrap

Conditional Statements – If


							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 Statements – 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...i {
									print(".")
								}
								print("\n")
							}
						

Related Resources

Swift-logo