Ilya Loshkarev loshkarev.i@gmail.com
SFEDU 2017
Smalltalk-80
first language to implement OOPSteve Jobs presents NeXT Computer
Apple acquires NeXT
NeXTSTEP becomes MacOS
OPENSTEP API becomes COCOA
Apple becomes holder of Objective-C rights
iPod
iTunes Store
"Apple reinvents the phone"
After "failure" with iTunes on Motorola ROHR E1Touch Interface
600 MHz CPU
128 MB RAM
16 GB storage
No access to file system
No third party apps
No 3G
No wireless features
POSIX API
Core Foundation
Cocoa Touch
UIKit
iOS SDK
App Store
Xcode 3.1
3G and GPS
Copy and Paste
Bluetooth and USB
Push Notifications
inApps and Subscriptions
Multitasking
Retina
iAds
"LLVM: An Infrastructure for Multi-Stage Optimization"
Chris Lattner, Masters Thesis, Computer Science Dept., University of Illinois at Urbana-ChampaignApple 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
September 9th – Swift 1.0
September 21th – Swift 2.0
Not bin compatable!December 8th – Swift goes open source
March 16th – Swift 2.2
Exceptions!September 7th – Swift 3.0
Not bin compatable!September 19th – Swift 4.0
Ted Kremenek@tkremenek
Early 2019 – Swift 5.0
https://apple.github.io/swift-evolution/
@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
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application( application: UIApplication,
didFinishLaunchingWithOptions
launchOptions: [NSObject: AnyObject]?) -> Bool {
return true
}
}
Strong typing & type-safety
Initializers
Range Checks
Memory managment
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!
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)
Source: yalantis.com
Better compile time optimisation!
Source: github.com
// Special header provided for compiler with project options
#import "MySuperObject.h"
let myObject = MySuperObject()
Xcode allows full Swift ⇆ Objective-C interoperability!
$ xcrun swift
> 1 + 2
$R0: Int = 3
> let greeting = "Hello!"
greeting: String = "Hello!"
> print(greeting)
Hello!
"Read Eval Print Loop"
Integrated REPL Enviroment
Each line gets evaluated on built
Get those errors as soon as possible
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
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"]
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
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
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
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
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
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
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")
}