SlideShare a Scribd company logo
1 of 57
Download to read offline
Introduction to Swift
Apple’s Objective-C Replacement
Agenda
- Who am I?
- How Apple arrived at Swift
- What’s new in Swift
- Swift’s syntax & basics
- Resources
What we will cover
Who am I?
- iOS + .NET Developer
- Write on all things development
- Game Developer (Specifically, Unity)
- Founded Dreaming In Binary
- Developer at HealthMEDX
Jordan Morgan
Apple’s road to Swift
Created in 1983 by Brad Cox & Tom Love
Built right on top of C
Added Object Oriented ideas (classes, objects, etc)
Objective-C is created
Apple’s road to Swift
2 years later, Jobs licenses it for NeXT (NeXTSTEP)
1996, Jobs is back at Apple and NeXT is acquired
Object Oriented libraries separated out from the OS as an API
called OpenStep
1997, Apples includes NeXT OS and APIs into new OS -
“Rhapsody”. This became Mac OS X.
This is where we get “NS” from - NeXT Step
Objective-C is engrained into Apple’s technology stack
Apple’s road to Swift
Objective-C became the native language for iOS/OS X
Improved and iterated upon over the years
Works fine - but starts to show its age
Objective-C peaks
Apple’s road to Swift
- Objective-C is not easy to learn
- Syntax is unusual and unfamiliar
- C is mixed in and heavily used in Foundation
- C baggage (.h & .m files, for example)
- No generics (leads to tedious casts)
- Overloading not supported
Objective-C’s weaknesses
Apple’s road to Swift
Keeps the best of Objective-C, such as named
parameters
Brings in modern programming language
advancements
Syntax is familiar - similar to PHP, Python, and
Rust
In comes Swift!
What’s new in Swift
Type inference
Cool new features
var aString:String = "String variable"
keyword name type initial value
What’s new in Swift
var 😼 = "Catface"
Type Inference
var anotherString = "Another string variable"
Variables can be unicode characters
Omit the type - it’s inferred to be a String
What’s new in Swift
String is lightweight like a C string
Powerful as an NSString
Concatenating is no longer a chore
Types
var twitterURL = "(kTwitterURL) + (twitterHandle)";
NSURL *twitterURL = [[NSURL alloc] initWithString:
[kTwitterURL stringByAppendingString:twitterHandle]];
What’s new in Swift
var speakers = 20
var attendees = 300
var audience = "On average, there will be 
(attendees / speakers) people at each session."
String Interpolation
Can even have expressions evaluated
No need for a mutable or immutable typed Strings
What’s new in Swift
The let keyword defines a constant
Constants
let speakers = 20
let attendees = 300
let audience = "On average, there will be 
(attendees / speakers) people at each session."
- Opt for immutability
- Forces you to think about your declarations
- Safer for multithreaded environments
- Optimization gains
What’s new in Swift
- Variables always initialized before use
- But what about nil?
- Objective - C nil = Pointer to a non existent object
- Swift nil = Absence of a value of a certain type
- Optionals - more on that later
Variable Initialization
What’s new in Swift
- Closures unified with function pointers
- Generics
- Tuples
- No more semicolons (though you can)
- and more
Other notable additions
If Statements
No parentheses required
Some basic and minor changes
Braces always required
if 1 < 2
{
println("True")
}
else
{
println("False")
}
Collections
They can work with any type - primitives included
Concise and powerful:
NSArray *numAr = @[[NSNumber numberWithInt:0]];
Array and Dictionary
let numAr = [1]
let names: [String] = ["Jordan", "Jansyn"]
Specify the type if you want:
Collections
Array
Literal declarations
Dictionary
let names = ["Jordan", "Jansyn", "Bennett"]
let namesAndAges = ["Jordan":25, "Jansyn":25,
"Bennett": 1]
No more “@“ in front of strings either
Collections
Easy to modify
Modify a collection with append(<T>):
var modify = [“Jordan”]
modify.appened(“Jansyn”)
//["Jordan","Jansyn"]
Specify index
var modify = ["Jordan"]
modify[0] = "Jansyn"
//["Jansyn"]
Use range operators
var modify = ["Jordan","Jansyn"]
modify[0...1] = ["Bennett","Whitney"]
//["Bennett","Whitney"]
Collections
Just define a new key
Modify a dictionary
var morganFam = ["Jordan":25]
morganFam["Jansyn"] = 25
Editing value works the same way
var morganFam = ["Jordan":25]
morganFam["Jansyn"] = 26
Collections
If you want a collection with more than one type :
Varied types
var multiTyped: [AnyObject] = ["foo", 01, true, 44.5]
That said, try to keep them strongly typed
For most intents and purposes, AnyObject is
analogous to id
Loops
for i in 0..<2
{
println(i)
}
//Output: 0,1
Ranges
Half open range Closed range
for i in 0...2
{
println(i)
}
//Output: 0,1,2
Loops
Easily loop through characters in a string
Flexible
let abc = "abc"
for char in abc
{
println(char)
}
In Objective-C :
NSString *myStrings = @"abc";
for (NSInteger charIdx=0; charIdx < myStrings.length; charIdx++)
{
NSLog(@"%C", [myStrings characterAtIndex:charIdx]);
}
Loops
Exclude value from the range (use _)
Loops cont.
let base = 3
let power = 10
var answer = 1
for _ in 1...power
{
answer *= base
}
Iterating over collections
let morganFam = ["Jordan":25,"Jansyn":25,"Bennett":1]
//KVPs from dictionary come back as tuples
for (name,age) in morganFam
{
println("(name) is (age) years old.")
}
Loops
Condition - increment looping
Traditional C-style iteration
for var idx = 0; idx < MAX; idx++
{
println("Index is (idx)")
}
No parentheses
Initialization with var and not let
While loops are here too
Switch Statements
No implicit fall through
Fallthrough
You can still “break” out before execution if finished
If you want to fallthrough, you can use fallthrough
Switch Statements
You can get cute with them
Switches cont.
let anInt = 40
switch anInt
{
case 0, 1, 2:
println("Tiny")
case 3...5:
println("Medium")
case 6..<39:
println("Large")
case _ where anInt % 2 == 1:
println("It's odd")
case _ where anInt % 2 == 0:
println("Nope, it's not odd, it's even")
default:
break
}
Switch Statements
The old days
Compared to Objective-C
NSString *morganFamMember = @"Jordan";
if ([morganFamMember isEqualToString:@"Jansyn"])
{
NSLog(@"It's mom!");
}
else if([morganFamMember isEqualToString:@"Bennett"])
{
NSLog(@"It's the baby boy!");
}
else if([morganFamMember isEqualToString:@"Whit"])
{
NSLog(@"It's Jordan's sister!");
}
else if([morganFamMember isEqualToString:@"Jordan"])
{
NSLog(@"It's dad!");
}
else
{
NSLog(@"We don't know who it is.");
}
The new days
let morganFamMember = "Jordan"
switch morganFamMember
{
case "Jansyn":
println("It's mom!”)
case "Bennett":
println("It's the baby boy!")
case "Whit":
println("It's Jordan's sister!")
case "Jordan":
println("It's dad!")
default:
println("We don't know who it is.")
}
Objective-C doesn’t support Switch with NSString
Optionals
We want the value - or to know it wasn’t found
A core concept of Swift
Optionals have ? by them
Means we get a value back - or nothing at all
Optionals
We could use magic numbers (i.e. -1)
Cont.
let jansynsAge:Int? = morganFam["Jansyn"]
NSNotFound if in Objective-C
Returns nil -or no value, or an int (Need to specify type)
Optionals
So if it’s there, how do we get it?
Unwrapping
let jansynsAge:Int? = morganFam["Jansyn"]
if jansynsAge == nil
{
println("Jansyn is apparently timeless.")
}
else
{
let foundAge = jansynsAge!
println("Jansyn is (foundAge) years old.")
}
Unwrap the value (i.e. the !)
No need to specify the type, the compiler knows
Optionals
This is a common pattern, shorthand is like so (no !):
Short syntax
if let foundAge = jansynsAge
{
println("Jansyn is (foundAge) years old.")
}
If you know the value is there, you can unwrap it directly:
var name: String? = "Jordan"
let anotherJordan = name!
println(anotherJordan)
You’re crashing if you’re wrong
If forced unwrapped, you don’t need to set it to a var.
Optional Chaining
What if you want a value that could be housed around
other nil values?
Query multiple optionals
class Residence
{
var street:String?
}
class Person
{
var humbleAbode:Residence?
}
var aPerson = Person()
Optional Chaining
Use ? operator to use optional chaining
Cont.
class Residence
{
var street:String?
}
class Person
{
var humbleAbode:Residence?
}
var aPerson = Person()
if let theStreet = aPerson.humbleAbode?.street
{
println("The street is (theStreet)")
}
else
{
println("Person has no street")
}
Remember, any optional must be unwrapped via !
Functions
Defined with func keyword
Overview
func printName()
{
println("It's Jordan")
}
Named parameters, like Objective-C
func printName(name:String)
{
println("It's (name)”)
}
Functions
Denote return type with ->
Return types
func printNameWithGreeting(name:String) -> String
{
return "It's (name), how ya doin' today?"
}
Define default values
func printNameWithGreeting(name:String = "Jansyn") -> String
{
return "It's (name), how ya doin' today?"
}
Functions
Return tuples
Multiple return types
func nameAndAge() -> (String, Int)
{
return ("Jordan",25)
}
Decompose them to access values
let (name,age) = nameAndAge()
println("(name) is (age) years old.")
Functions
Give meaningful names to return values
Name multiple return values
func nameAndAge() -> (name:String, age:Int)
{
return ("Jordan",25)
}
let Jordan = nameAndAge()
println("(Jordan.name) is (Jordan.age) years old.")
//Jordan is 25 years old.
Closures
Much like blocks in Objective-C
Similar functionality
Contain some code, you can pass them around
Lambdas or anonymous functions
let aClosure =
{
println("This is a closure")
}
Compiler sees it like this:
let aClosure: () -> () =
{
println("This is a closure")
}
Closures
Notice that’s similar to a function’s signature
Syntax
let aClosure: () -> () =
{
println("This is a closure")
}
func aClosure: () -> () =
{
println("This is a closure")
}
Functions are just named closures
Closures
Define in the formal parameter list
Passed as a parameter
func doTaskRepeated(count: Int, theTask: () -> ())
{
for i in 0..<count
{
theTask()
}
}
Calling it
doTaskRepeated(10, {
println("A complex and awesome task.")
})
Closures
Define closure as the last parameter in formal parameter
list
Trailing closure
doTaskRepeated(10) {
println("A complex and awesome task.")
}
Looks like control flow statement
Classes
No more import because Swift has no implementation file
Much like Java and .NET
No need to explicitly define a base class
class Person
{
}
Classes
class Jordan
{
let name = "Jordan"
}
Properties
Swift provides the backing store for you
By default, all entities have internal access
class Jordan
{
let name = "Jordan"
private let movieMostWatchedPastMonth = "Frozen"
}
Classes
Use internal, or nothing at all
Exposing properties
class Jordan
{
let name = "Jordan"
internal var age = 25
private let movieMostWatchedPastMonth = "Frozen"
}
let aJordan = Jordan()
//Grew up quick during this talk
aJordan.age = 35
Notice you don’t need “new” in front of the type
Classes
You can define custom getters and setters
Computed properties
class Jordan
{
let name = "Jordan"
var myLocation:(x:Float,y:Float)
{
get
{
return (10,30)
}
set
{
self.myLocation.x = newValue.x
self.myLocation.y = newValue.y
}
}
}
…can even use tuples
Create a read only computed property - just omit setter
Classes
init() keyword
Initialization
class Jordan
{
let name = "Jordan"
var age = 25
init()
{
//No need to return self
}
}
Can also initialize constant values
class Jordan
{
let name = "Jordan"
let hobby = ""
init()
{
//No need to return self
}
init(hobby:String)
{
self.hobby = hobby
}
}
var aJordan = Jordan(hobby: "Basketball")
- if you are inheriting, call super.init()
Classes
Fires just before and right after value changes
Property Observers
class Bennett
{
private let setCurfew = 9 //p.m.
var curfew : Int
{
willSet
{
if curfew > setCurfew
{
println("GROUNDED")
}
}
didSet
{
if curfew < setCurfew
{
println("I am glad you obey.")
}
}
}
init()
{
self.curfew = setCurfew
}
}
Classes
Work the same way as functions
Methods
Only need to use self when property has the same
name as a parameter in the function’s signature
class Bennett
{
var nickName = "Benny"
func changeNickName(nickName: String)
{
self.nickName = nickName
println("Bennett's new nickname is (self.nickName)")
}
}
Classes
You don’t even need to specify one
A note on initializers
super.init needs to happen last in custom initializers
Sole purpose is to initialize values for the class
Structs
Still works the same way
Not much has changed
- Doesn’t support inheritance
- Value types
Think of them as you do in your OOP language of choice
Enumerations
Value types
Enums
They can have raw values (like in C)
enum BestNFLTeams:Int
{
case StLouisRams = 1, Patriots, Bucs, Chiefs
}
BestNFLTeams.StLouisRams.toRaw()
//Prints 1, obviously
Enumerations
Don’t always need underlying values
enum Directions
{
case North, South, East, West
}
//Compiler infers Directions as type
var directionToGo = Directions.North
Also, compiler will again infer the type
let lbl = UILabel()
lbl.textAlignment = .Right
Value type constants have all constant members
Reference type constants can have mutable members
Enumerations
Associate values within an enum
Associated Values
enum RamsVictory
{
case ByOnePoint
case ByPoints(Int)
}
var ramsWinBig = RamsVictory.ByPoints(24)
Even custom properties
enum RamsVictory
{
case ByOnePoint, ByPoints(Int)
var winSummary:String{
switch self{
case .ByOnePoint:
return "Rams by one."
case .ByPoints(let points):
return "Rams win big by (points)!"
}
}
}
var ramsWinBig = RamsVictory.ByOnePoint
println(ramsWinBig.winSummary) //Rams by one.
ramsWinBig = RamsVictory.ByPoints(14)
println(ramsWinBig.winSummary) //Rams win big by 14!
Access Modifiers
Three modifiers
Recently Added
- Private - Available only from within source file
- Internal - Available to entire module that includes
the definition (i.e. app or framework)
- Public - Intended for use with APIs, means entity
can be accessed by any file that imports the
module
There’s much more
Interoperability with Objective-C
Lots of new features
Extensions
Automatic Reference Counting
Pattern Matching
Functional Programming
Resources
- www.dreaminginbinary.co
- The Swift Programming Language (iBook)
- The Swift Blog
- WWDC Videos
- @dibsoftware
- @jordanmorgan10
- facebook/dreaminginbinary

More Related Content

What's hot

3 kotlin vs. java- what kotlin has that java does not
3  kotlin vs. java- what kotlin has that java does not3  kotlin vs. java- what kotlin has that java does not
3 kotlin vs. java- what kotlin has that java does notSergey Bandysik
 
Programming Language Swift Overview
Programming Language Swift OverviewProgramming Language Swift Overview
Programming Language Swift OverviewKaz Yoshikawa
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldBTI360
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real Worldosfameron
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Cody Engel
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with ScalaDenis
 
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
 
The Ring programming language version 1.5.1 book - Part 21 of 180
The Ring programming language version 1.5.1 book - Part 21 of 180The Ring programming language version 1.5.1 book - Part 21 of 180
The Ring programming language version 1.5.1 book - Part 21 of 180Mahmoud Samir Fayed
 
JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )Victor Verhaagen
 
Metaprogramming in Haskell
Metaprogramming in HaskellMetaprogramming in Haskell
Metaprogramming in HaskellHiromi Ishii
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.jstimourian
 
Ceylon idioms by Gavin King
Ceylon idioms by Gavin KingCeylon idioms by Gavin King
Ceylon idioms by Gavin KingUnFroMage
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlinintelliyole
 

What's hot (20)

3 kotlin vs. java- what kotlin has that java does not
3  kotlin vs. java- what kotlin has that java does not3  kotlin vs. java- what kotlin has that java does not
3 kotlin vs. java- what kotlin has that java does not
 
Einführung in TypeScript
Einführung in TypeScriptEinführung in TypeScript
Einführung in TypeScript
 
Scala vs Ruby
Scala vs RubyScala vs Ruby
Scala vs Ruby
 
Programming Language Swift Overview
Programming Language Swift OverviewProgramming Language Swift Overview
Programming Language Swift Overview
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real World
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
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
 
SOLID Ruby, SOLID Rails
SOLID Ruby, SOLID RailsSOLID Ruby, SOLID Rails
SOLID Ruby, SOLID Rails
 
The Ring programming language version 1.5.1 book - Part 21 of 180
The Ring programming language version 1.5.1 book - Part 21 of 180The Ring programming language version 1.5.1 book - Part 21 of 180
The Ring programming language version 1.5.1 book - Part 21 of 180
 
Sneaking inside Kotlin features
Sneaking inside Kotlin featuresSneaking inside Kotlin features
Sneaking inside Kotlin features
 
Java string handling
Java string handlingJava string handling
Java string handling
 
JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )JavaScript introduction 1 ( Variables And Values )
JavaScript introduction 1 ( Variables And Values )
 
Metaprogramming in Haskell
Metaprogramming in HaskellMetaprogramming in Haskell
Metaprogramming in Haskell
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.js
 
Ceylon idioms by Gavin King
Ceylon idioms by Gavin KingCeylon idioms by Gavin King
Ceylon idioms by Gavin King
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlin
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 

Viewers also liked

Open Solaris Introduction: How to make it feel like home
Open Solaris Introduction: How to make it feel like homeOpen Solaris Introduction: How to make it feel like home
Open Solaris Introduction: How to make it feel like homeAhmed Saeed
 
Trusted Virtual Domains on OpenSolaris: Usable Secure Desktop Environments
Trusted Virtual Domains on OpenSolaris: Usable Secure Desktop EnvironmentsTrusted Virtual Domains on OpenSolaris: Usable Secure Desktop Environments
Trusted Virtual Domains on OpenSolaris: Usable Secure Desktop EnvironmentsMarcel Winandy
 
Illumos la-jan11
Illumos la-jan11Illumos la-jan11
Illumos la-jan11Evan Powell
 
OpenSolaris On EeePc at Osc Spring
OpenSolaris On EeePc at Osc SpringOpenSolaris On EeePc at Osc Spring
OpenSolaris On EeePc at Osc SpringMasafumi Ohta
 
OpenSolaris School OS Beginners Guide
OpenSolaris School OS Beginners GuideOpenSolaris School OS Beginners Guide
OpenSolaris School OS Beginners GuideAngad Singh
 
OpenSolaris + Netbeans + Glassfish + MySQL on VirtualBox Install Guide
OpenSolaris + Netbeans + Glassfish + MySQL on VirtualBox Install GuideOpenSolaris + Netbeans + Glassfish + MySQL on VirtualBox Install Guide
OpenSolaris + Netbeans + Glassfish + MySQL on VirtualBox Install GuideSean O'Quin
 
Solaris 10 Advanced Features.
Solaris 10 Advanced Features.Solaris 10 Advanced Features.
Solaris 10 Advanced Features.Aram Avetisyan
 
Surge2014 talk - illumos State of the Community & POSIX Update
Surge2014 talk - illumos State of the Community & POSIX UpdateSurge2014 talk - illumos State of the Community & POSIX Update
Surge2014 talk - illumos State of the Community & POSIX UpdateGarrett D'Amore
 
Introducción a OpenSolaris
Introducción a OpenSolarisIntroducción a OpenSolaris
Introducción a OpenSolarisjuandanielp
 
The Microkernel Mach Under NeXTSTEP
The Microkernel Mach Under NeXTSTEPThe Microkernel Mach Under NeXTSTEP
The Microkernel Mach Under NeXTSTEPGregor Schmidt
 
Solaris, OpenSolaris y Virtualización
Solaris, OpenSolaris y VirtualizaciónSolaris, OpenSolaris y Virtualización
Solaris, OpenSolaris y Virtualizaciónjuandanielp
 
Solaris 10 virtualization presentation
Solaris 10 virtualization presentationSolaris 10 virtualization presentation
Solaris 10 virtualization presentationxKinAnx
 
Solaris 10 administration 2 Configuring NFS
Solaris 10 administration 2 Configuring NFSSolaris 10 administration 2 Configuring NFS
Solaris 10 administration 2 Configuring NFSAhmedEidNassef
 
OpenSolaris Clusters And Clouds From Your Laptop
OpenSolaris Clusters And Clouds From Your LaptopOpenSolaris Clusters And Clouds From Your Laptop
OpenSolaris Clusters And Clouds From Your Laptopbogdanv
 

Viewers also liked (20)

Open Solaris Introduction: How to make it feel like home
Open Solaris Introduction: How to make it feel like homeOpen Solaris Introduction: How to make it feel like home
Open Solaris Introduction: How to make it feel like home
 
Sun Web Server Brief
Sun Web Server BriefSun Web Server Brief
Sun Web Server Brief
 
Trusted Virtual Domains on OpenSolaris: Usable Secure Desktop Environments
Trusted Virtual Domains on OpenSolaris: Usable Secure Desktop EnvironmentsTrusted Virtual Domains on OpenSolaris: Usable Secure Desktop Environments
Trusted Virtual Domains on OpenSolaris: Usable Secure Desktop Environments
 
Illumos la-jan11
Illumos la-jan11Illumos la-jan11
Illumos la-jan11
 
OpenSolaris On EeePc at Osc Spring
OpenSolaris On EeePc at Osc SpringOpenSolaris On EeePc at Osc Spring
OpenSolaris On EeePc at Osc Spring
 
OpenSolaris Web Stack MySQL BOF
OpenSolaris Web Stack MySQL BOFOpenSolaris Web Stack MySQL BOF
OpenSolaris Web Stack MySQL BOF
 
OpenSolaris School OS Beginners Guide
OpenSolaris School OS Beginners GuideOpenSolaris School OS Beginners Guide
OpenSolaris School OS Beginners Guide
 
Sun Web Server Brief
Sun Web Server BriefSun Web Server Brief
Sun Web Server Brief
 
OpenSolaris + Netbeans + Glassfish + MySQL on VirtualBox Install Guide
OpenSolaris + Netbeans + Glassfish + MySQL on VirtualBox Install GuideOpenSolaris + Netbeans + Glassfish + MySQL on VirtualBox Install Guide
OpenSolaris + Netbeans + Glassfish + MySQL on VirtualBox Install Guide
 
Solaris 10 Advanced Features.
Solaris 10 Advanced Features.Solaris 10 Advanced Features.
Solaris 10 Advanced Features.
 
Surge2014 talk - illumos State of the Community & POSIX Update
Surge2014 talk - illumos State of the Community & POSIX UpdateSurge2014 talk - illumos State of the Community & POSIX Update
Surge2014 talk - illumos State of the Community & POSIX Update
 
Introducción a OpenSolaris
Introducción a OpenSolarisIntroducción a OpenSolaris
Introducción a OpenSolaris
 
The Microkernel Mach Under NeXTSTEP
The Microkernel Mach Under NeXTSTEPThe Microkernel Mach Under NeXTSTEP
The Microkernel Mach Under NeXTSTEP
 
2009 04.s10-admin-topics4
2009 04.s10-admin-topics42009 04.s10-admin-topics4
2009 04.s10-admin-topics4
 
Solaris, OpenSolaris y Virtualización
Solaris, OpenSolaris y VirtualizaciónSolaris, OpenSolaris y Virtualización
Solaris, OpenSolaris y Virtualización
 
Solaris 10 virtualization presentation
Solaris 10 virtualization presentationSolaris 10 virtualization presentation
Solaris 10 virtualization presentation
 
Solaris 10 administration 2 Configuring NFS
Solaris 10 administration 2 Configuring NFSSolaris 10 administration 2 Configuring NFS
Solaris 10 administration 2 Configuring NFS
 
OpenSolaris Overview
OpenSolaris OverviewOpenSolaris Overview
OpenSolaris Overview
 
OpenSolaris Clusters And Clouds From Your Laptop
OpenSolaris Clusters And Clouds From Your LaptopOpenSolaris Clusters And Clouds From Your Laptop
OpenSolaris Clusters And Clouds From Your Laptop
 
OpenSolaris 2009.06 Workshop
OpenSolaris 2009.06 WorkshopOpenSolaris 2009.06 Workshop
OpenSolaris 2009.06 Workshop
 

Similar to Intro toswift1

Basic iOS Training with SWIFT - Part 2
Basic iOS Training with SWIFT - Part 2Basic iOS Training with SWIFT - Part 2
Basic iOS Training with SWIFT - Part 2Manoj Ellappan
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme SwiftMovel
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)William Narmontas
 
Criando app Android utilizando Kotlin
Criando app Android utilizando KotlinCriando app Android utilizando Kotlin
Criando app Android utilizando KotlinLuiz Henrique Santana
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanKavita Ganesan
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)Tak Lee
 
Cocoa Design Patterns in Swift
Cocoa Design Patterns in SwiftCocoa Design Patterns in Swift
Cocoa Design Patterns in SwiftMichele Titolo
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 

Similar to Intro toswift1 (20)

Basic iOS Training with SWIFT - Part 2
Basic iOS Training with SWIFT - Part 2Basic iOS Training with SWIFT - Part 2
Basic iOS Training with SWIFT - Part 2
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 
Criando app Android utilizando Kotlin
Criando app Android utilizando KotlinCriando app Android utilizando Kotlin
Criando app Android utilizando Kotlin
 
M C6java7
M C6java7M C6java7
M C6java7
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
 
Cocoa Design Patterns in Swift
Cocoa Design Patterns in SwiftCocoa Design Patterns in Swift
Cocoa Design Patterns in Swift
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
07slide
07slide07slide
07slide
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 

Intro toswift1

  • 1. Introduction to Swift Apple’s Objective-C Replacement
  • 2. Agenda - Who am I? - How Apple arrived at Swift - What’s new in Swift - Swift’s syntax & basics - Resources What we will cover
  • 3. Who am I? - iOS + .NET Developer - Write on all things development - Game Developer (Specifically, Unity) - Founded Dreaming In Binary - Developer at HealthMEDX Jordan Morgan
  • 4. Apple’s road to Swift Created in 1983 by Brad Cox & Tom Love Built right on top of C Added Object Oriented ideas (classes, objects, etc) Objective-C is created
  • 5. Apple’s road to Swift 2 years later, Jobs licenses it for NeXT (NeXTSTEP) 1996, Jobs is back at Apple and NeXT is acquired Object Oriented libraries separated out from the OS as an API called OpenStep 1997, Apples includes NeXT OS and APIs into new OS - “Rhapsody”. This became Mac OS X. This is where we get “NS” from - NeXT Step Objective-C is engrained into Apple’s technology stack
  • 6. Apple’s road to Swift Objective-C became the native language for iOS/OS X Improved and iterated upon over the years Works fine - but starts to show its age Objective-C peaks
  • 7. Apple’s road to Swift - Objective-C is not easy to learn - Syntax is unusual and unfamiliar - C is mixed in and heavily used in Foundation - C baggage (.h & .m files, for example) - No generics (leads to tedious casts) - Overloading not supported Objective-C’s weaknesses
  • 8. Apple’s road to Swift Keeps the best of Objective-C, such as named parameters Brings in modern programming language advancements Syntax is familiar - similar to PHP, Python, and Rust In comes Swift!
  • 9. What’s new in Swift Type inference Cool new features var aString:String = "String variable" keyword name type initial value
  • 10. What’s new in Swift var 😼 = "Catface" Type Inference var anotherString = "Another string variable" Variables can be unicode characters Omit the type - it’s inferred to be a String
  • 11. What’s new in Swift String is lightweight like a C string Powerful as an NSString Concatenating is no longer a chore Types var twitterURL = "(kTwitterURL) + (twitterHandle)"; NSURL *twitterURL = [[NSURL alloc] initWithString: [kTwitterURL stringByAppendingString:twitterHandle]];
  • 12. What’s new in Swift var speakers = 20 var attendees = 300 var audience = "On average, there will be (attendees / speakers) people at each session." String Interpolation Can even have expressions evaluated No need for a mutable or immutable typed Strings
  • 13. What’s new in Swift The let keyword defines a constant Constants let speakers = 20 let attendees = 300 let audience = "On average, there will be (attendees / speakers) people at each session." - Opt for immutability - Forces you to think about your declarations - Safer for multithreaded environments - Optimization gains
  • 14. What’s new in Swift - Variables always initialized before use - But what about nil? - Objective - C nil = Pointer to a non existent object - Swift nil = Absence of a value of a certain type - Optionals - more on that later Variable Initialization
  • 15. What’s new in Swift - Closures unified with function pointers - Generics - Tuples - No more semicolons (though you can) - and more Other notable additions
  • 16. If Statements No parentheses required Some basic and minor changes Braces always required if 1 < 2 { println("True") } else { println("False") }
  • 17. Collections They can work with any type - primitives included Concise and powerful: NSArray *numAr = @[[NSNumber numberWithInt:0]]; Array and Dictionary let numAr = [1] let names: [String] = ["Jordan", "Jansyn"] Specify the type if you want:
  • 18. Collections Array Literal declarations Dictionary let names = ["Jordan", "Jansyn", "Bennett"] let namesAndAges = ["Jordan":25, "Jansyn":25, "Bennett": 1] No more “@“ in front of strings either
  • 19. Collections Easy to modify Modify a collection with append(<T>): var modify = [“Jordan”] modify.appened(“Jansyn”) //["Jordan","Jansyn"] Specify index var modify = ["Jordan"] modify[0] = "Jansyn" //["Jansyn"] Use range operators var modify = ["Jordan","Jansyn"] modify[0...1] = ["Bennett","Whitney"] //["Bennett","Whitney"]
  • 20. Collections Just define a new key Modify a dictionary var morganFam = ["Jordan":25] morganFam["Jansyn"] = 25 Editing value works the same way var morganFam = ["Jordan":25] morganFam["Jansyn"] = 26
  • 21. Collections If you want a collection with more than one type : Varied types var multiTyped: [AnyObject] = ["foo", 01, true, 44.5] That said, try to keep them strongly typed For most intents and purposes, AnyObject is analogous to id
  • 22. Loops for i in 0..<2 { println(i) } //Output: 0,1 Ranges Half open range Closed range for i in 0...2 { println(i) } //Output: 0,1,2
  • 23. Loops Easily loop through characters in a string Flexible let abc = "abc" for char in abc { println(char) } In Objective-C : NSString *myStrings = @"abc"; for (NSInteger charIdx=0; charIdx < myStrings.length; charIdx++) { NSLog(@"%C", [myStrings characterAtIndex:charIdx]); }
  • 24. Loops Exclude value from the range (use _) Loops cont. let base = 3 let power = 10 var answer = 1 for _ in 1...power { answer *= base } Iterating over collections let morganFam = ["Jordan":25,"Jansyn":25,"Bennett":1] //KVPs from dictionary come back as tuples for (name,age) in morganFam { println("(name) is (age) years old.") }
  • 25. Loops Condition - increment looping Traditional C-style iteration for var idx = 0; idx < MAX; idx++ { println("Index is (idx)") } No parentheses Initialization with var and not let While loops are here too
  • 26. Switch Statements No implicit fall through Fallthrough You can still “break” out before execution if finished If you want to fallthrough, you can use fallthrough
  • 27. Switch Statements You can get cute with them Switches cont. let anInt = 40 switch anInt { case 0, 1, 2: println("Tiny") case 3...5: println("Medium") case 6..<39: println("Large") case _ where anInt % 2 == 1: println("It's odd") case _ where anInt % 2 == 0: println("Nope, it's not odd, it's even") default: break }
  • 28. Switch Statements The old days Compared to Objective-C NSString *morganFamMember = @"Jordan"; if ([morganFamMember isEqualToString:@"Jansyn"]) { NSLog(@"It's mom!"); } else if([morganFamMember isEqualToString:@"Bennett"]) { NSLog(@"It's the baby boy!"); } else if([morganFamMember isEqualToString:@"Whit"]) { NSLog(@"It's Jordan's sister!"); } else if([morganFamMember isEqualToString:@"Jordan"]) { NSLog(@"It's dad!"); } else { NSLog(@"We don't know who it is."); } The new days let morganFamMember = "Jordan" switch morganFamMember { case "Jansyn": println("It's mom!”) case "Bennett": println("It's the baby boy!") case "Whit": println("It's Jordan's sister!") case "Jordan": println("It's dad!") default: println("We don't know who it is.") } Objective-C doesn’t support Switch with NSString
  • 29. Optionals We want the value - or to know it wasn’t found A core concept of Swift Optionals have ? by them Means we get a value back - or nothing at all
  • 30. Optionals We could use magic numbers (i.e. -1) Cont. let jansynsAge:Int? = morganFam["Jansyn"] NSNotFound if in Objective-C Returns nil -or no value, or an int (Need to specify type)
  • 31. Optionals So if it’s there, how do we get it? Unwrapping let jansynsAge:Int? = morganFam["Jansyn"] if jansynsAge == nil { println("Jansyn is apparently timeless.") } else { let foundAge = jansynsAge! println("Jansyn is (foundAge) years old.") } Unwrap the value (i.e. the !) No need to specify the type, the compiler knows
  • 32. Optionals This is a common pattern, shorthand is like so (no !): Short syntax if let foundAge = jansynsAge { println("Jansyn is (foundAge) years old.") } If you know the value is there, you can unwrap it directly: var name: String? = "Jordan" let anotherJordan = name! println(anotherJordan) You’re crashing if you’re wrong If forced unwrapped, you don’t need to set it to a var.
  • 33. Optional Chaining What if you want a value that could be housed around other nil values? Query multiple optionals class Residence { var street:String? } class Person { var humbleAbode:Residence? } var aPerson = Person()
  • 34. Optional Chaining Use ? operator to use optional chaining Cont. class Residence { var street:String? } class Person { var humbleAbode:Residence? } var aPerson = Person() if let theStreet = aPerson.humbleAbode?.street { println("The street is (theStreet)") } else { println("Person has no street") } Remember, any optional must be unwrapped via !
  • 35. Functions Defined with func keyword Overview func printName() { println("It's Jordan") } Named parameters, like Objective-C func printName(name:String) { println("It's (name)”) }
  • 36. Functions Denote return type with -> Return types func printNameWithGreeting(name:String) -> String { return "It's (name), how ya doin' today?" } Define default values func printNameWithGreeting(name:String = "Jansyn") -> String { return "It's (name), how ya doin' today?" }
  • 37. Functions Return tuples Multiple return types func nameAndAge() -> (String, Int) { return ("Jordan",25) } Decompose them to access values let (name,age) = nameAndAge() println("(name) is (age) years old.")
  • 38. Functions Give meaningful names to return values Name multiple return values func nameAndAge() -> (name:String, age:Int) { return ("Jordan",25) } let Jordan = nameAndAge() println("(Jordan.name) is (Jordan.age) years old.") //Jordan is 25 years old.
  • 39. Closures Much like blocks in Objective-C Similar functionality Contain some code, you can pass them around Lambdas or anonymous functions let aClosure = { println("This is a closure") } Compiler sees it like this: let aClosure: () -> () = { println("This is a closure") }
  • 40. Closures Notice that’s similar to a function’s signature Syntax let aClosure: () -> () = { println("This is a closure") } func aClosure: () -> () = { println("This is a closure") } Functions are just named closures
  • 41. Closures Define in the formal parameter list Passed as a parameter func doTaskRepeated(count: Int, theTask: () -> ()) { for i in 0..<count { theTask() } } Calling it doTaskRepeated(10, { println("A complex and awesome task.") })
  • 42. Closures Define closure as the last parameter in formal parameter list Trailing closure doTaskRepeated(10) { println("A complex and awesome task.") } Looks like control flow statement
  • 43. Classes No more import because Swift has no implementation file Much like Java and .NET No need to explicitly define a base class class Person { }
  • 44. Classes class Jordan { let name = "Jordan" } Properties Swift provides the backing store for you By default, all entities have internal access class Jordan { let name = "Jordan" private let movieMostWatchedPastMonth = "Frozen" }
  • 45. Classes Use internal, or nothing at all Exposing properties class Jordan { let name = "Jordan" internal var age = 25 private let movieMostWatchedPastMonth = "Frozen" } let aJordan = Jordan() //Grew up quick during this talk aJordan.age = 35 Notice you don’t need “new” in front of the type
  • 46. Classes You can define custom getters and setters Computed properties class Jordan { let name = "Jordan" var myLocation:(x:Float,y:Float) { get { return (10,30) } set { self.myLocation.x = newValue.x self.myLocation.y = newValue.y } } } …can even use tuples Create a read only computed property - just omit setter
  • 47. Classes init() keyword Initialization class Jordan { let name = "Jordan" var age = 25 init() { //No need to return self } } Can also initialize constant values class Jordan { let name = "Jordan" let hobby = "" init() { //No need to return self } init(hobby:String) { self.hobby = hobby } } var aJordan = Jordan(hobby: "Basketball") - if you are inheriting, call super.init()
  • 48. Classes Fires just before and right after value changes Property Observers class Bennett { private let setCurfew = 9 //p.m. var curfew : Int { willSet { if curfew > setCurfew { println("GROUNDED") } } didSet { if curfew < setCurfew { println("I am glad you obey.") } } } init() { self.curfew = setCurfew } }
  • 49. Classes Work the same way as functions Methods Only need to use self when property has the same name as a parameter in the function’s signature class Bennett { var nickName = "Benny" func changeNickName(nickName: String) { self.nickName = nickName println("Bennett's new nickname is (self.nickName)") } }
  • 50. Classes You don’t even need to specify one A note on initializers super.init needs to happen last in custom initializers Sole purpose is to initialize values for the class
  • 51. Structs Still works the same way Not much has changed - Doesn’t support inheritance - Value types Think of them as you do in your OOP language of choice
  • 52. Enumerations Value types Enums They can have raw values (like in C) enum BestNFLTeams:Int { case StLouisRams = 1, Patriots, Bucs, Chiefs } BestNFLTeams.StLouisRams.toRaw() //Prints 1, obviously
  • 53. Enumerations Don’t always need underlying values enum Directions { case North, South, East, West } //Compiler infers Directions as type var directionToGo = Directions.North Also, compiler will again infer the type let lbl = UILabel() lbl.textAlignment = .Right Value type constants have all constant members Reference type constants can have mutable members
  • 54. Enumerations Associate values within an enum Associated Values enum RamsVictory { case ByOnePoint case ByPoints(Int) } var ramsWinBig = RamsVictory.ByPoints(24) Even custom properties enum RamsVictory { case ByOnePoint, ByPoints(Int) var winSummary:String{ switch self{ case .ByOnePoint: return "Rams by one." case .ByPoints(let points): return "Rams win big by (points)!" } } } var ramsWinBig = RamsVictory.ByOnePoint println(ramsWinBig.winSummary) //Rams by one. ramsWinBig = RamsVictory.ByPoints(14) println(ramsWinBig.winSummary) //Rams win big by 14!
  • 55. Access Modifiers Three modifiers Recently Added - Private - Available only from within source file - Internal - Available to entire module that includes the definition (i.e. app or framework) - Public - Intended for use with APIs, means entity can be accessed by any file that imports the module
  • 56. There’s much more Interoperability with Objective-C Lots of new features Extensions Automatic Reference Counting Pattern Matching Functional Programming
  • 57. Resources - www.dreaminginbinary.co - The Swift Programming Language (iBook) - The Swift Blog - WWDC Videos - @dibsoftware - @jordanmorgan10 - facebook/dreaminginbinary