- Programming Languages
- iOS History
- Intro to Swift
- App demo and code tour
- Q/A
- Getting Set Up
- Going the distance
I highly recomend watching the rest of this series in your freetime.
- Objective-C
- Next
- Swift
Swift is a modern, static typed, dynamic programming language used to write system and application code for OSX, iOS, and Watch-OS operating systems. Read the brief intoduction by Apple before moving on.
It's mostly what you know (loops, variables, functions, classes), but it has a number of unique features that make it stand out.
Swift is statically typed, which implies that you as the programmer must specify what type each variable is.
Type inference is when a programming language uses static typing, but can determine the type associated with a variable by its value when it is declared.
An optional value is one that might have a value or might not. That is to say, it will either be a value with the type specified, or nil. There are no other options. Here is an example use case:
func getHaterStatus(weather: String) -> String? {
if weather == "sunny" {
return nil
} else {
return "Hate"
}
}
Optionals remind me of checking for an error in JavaScript callbacks. They are not always going to be there, but you should always check for them. Similarly, Optionals don't walways have a nil value, but you should always check for one by unswrapping it.
var songs: [String] = ["Shake it Off", "You Belong with Me", "Back to December", 3]
var songs = [String]()
var songs = ["Shake it Off", "You Belong with Me", "Love Story"]
var songs2 = ["Today was a Fairytale", "White Horse", "Fifteen"]
var both = songs + songs2
var person = [
"first": "Taylor",
"middle": "Alison",
"last": "Swift",
"month": "December",
"website": "taylorswift.com"
]
person["middle"]
person["month"]
var age = 25
"You are \(age) years old. In another \(age) years you will be \(age * 2)."
var action: String
var person = "hater"
if person == "hater" {
action = "hate"
} else if person == "player" {
action = "play"
} else {
action = "cruise"
}
if !stayOutTooLate && !nothingInBrain {
action = "cruise"
}
for i in 1...10 {
print("\(i) x 10 is \(i * 10)")
}
var str = "Fakers gonna"
// Range
for _ in 1 ... 5 {
str += " fake"
}
print(str)
var people = ["players", "haters", "heart-breakers", "fakers"]
var actions = ["play", "hate", "break", "fake"]
for i in 0 ..< people.count {
print("\(people[i]) gonna \(actions[i])")
}
var counter = 0
while true {
print("Counter is now \(counter)")
++counter
if counter == 556 {
break
}
}
let studioAlbums = 5
switch studioAlbums {
case 0...1:
print("You're just starting out")
case 2...3:
print("You're a rising star")
case 4...5:
print("You're world famous!")
default:
print("Have you done something new?")
}
func printAlbumRelease(name: String, year: Int) {
print("\(name) was released in \(year)")
}
printAlbumRelease("Fearless", year: 2008)
printAlbumRelease("Speak Now", year: 2010)
printAlbumRelease("Red", year: 2012)
func albumsIsTaylor(name: String) -> Bool {
if name == "Taylor Swift" { return true }
if name == "Fearless" { return true }
if name == "Speak Now" { return true }
if name == "Red" { return true }
if name == "1989" { return true }
return false
}
func getName(status: String) -> String? {
if status == "happy" {
return "Aaron"
} else {
return nil
}
}
getName("happy")
getName("Mad")
- Latest Xcode
- Shortcuts
Work through Hacking with Swift
- The Swift Programming Language, About Swift (iOS Developer Library)
- Using Swift with Cocoa and Objective-C, Basic Setup (iOS Developer Library)
- iOS Developer Library
- [iOS Developer Library, Sample Code](https://developer.apple.com/library/prerelease/ios/navigation/#section=Resource Types&topic=Sample Code)
- StackOverflow's Swift Info
- StackOverflow's Swift Recent Questions
- Unofficial Swift Online Compiler (Try Swift!)
- iPhone Tutorials by Ray Wenderlich
- Hacking w/Swift eBook by Paul Hudson (free)