Skip to content

Commit

Permalink
add episode 67 on swift
Browse files Browse the repository at this point in the history
  • Loading branch information
sayanee committed Apr 9, 2016
1 parent 8c36dd6 commit 8706482
Show file tree
Hide file tree
Showing 19 changed files with 257 additions and 3 deletions.
1 change: 1 addition & 0 deletions 067-swift/1-print.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("hello world")
12 changes: 12 additions & 0 deletions 067-swift/10-function.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 1 function argument
func greet(name: String) -> String {
return "Hello \(name)"
}

print(greet("Bob"))

func divide(dividend: Int, divisor: Int) -> Double {
return Double(dividend / divisor)
}

print(divide(20, divisor: 2)) // need to put the name of second argumnet
15 changes: 15 additions & 0 deletions 067-swift/11-tuple.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func calc(num: [Int]) -> (add: Int, sub: Int, area: Int) {
let addAnswer = num[0] + num[1]
let subAnswer = num[0] - num[1]
let area = num[0] * num[1]

return (addAnswer, subAnswer, area)
}

let stats = calc([5, 3])
print(stats)
print(stats.add)
print(stats.0)

print(stats.sub)
print(stats.1)
12 changes: 12 additions & 0 deletions 067-swift/12-unsepcified-args.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Int... : any number of integer arguments
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}

print(sumOf())
print(sumOf(4, 5, 12))
print(sumOf(4, 5, 12, 10))
11 changes: 11 additions & 0 deletions 067-swift/13-return-func.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// return a function that takes in an integer and returns an integer
// () -> ((Int) -> Int)
func cube() -> ((Int) -> Int) {
func powerThree(num: Int) -> Int {
return num * num * num
}
return powerThree
}

var cuboid1 = cube()
print(cuboid1(2))
16 changes: 16 additions & 0 deletions 067-swift/14-closure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// map
var numbers = [3, 9, 10, 12]

var mul1 = numbers.map({
(num: Int) -> Int in // define params and return type followed by in
return 3 * num
})
print(mul1)

// when params are known: omit type of parameters or return type
let mul2 = numbers.map({ num in 3 * num })
print(mul2)

// using $0
let mul3 = numbers.map({ 3 * $0 })
print(mul3)
33 changes: 33 additions & 0 deletions 067-swift/15-class.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Person {
var firstname: String
var lastname: String

init(firstname: String, lastname: String) {
self.firstname = firstname
self.lastname = lastname
}

func fullname() -> String {
return "\(firstname) \(lastname)"
}
}

var p = Person(firstname: "Igor", lastname: "Novak")
print(p.fullname())

// Inheritance
class Neighbour: Person {
var neighbourhood: String

init(firstname: String, lastname: String, neighbourhood: String) {
self.neighbourhood = neighbourhood
super.init(firstname: firstname, lastname: lastname)
}

override func fullname() -> String {
return "\(firstname) \(lastname) from \(neighbourhood)"
}
}

let n = Neighbour(firstname: "Min", lastname: "Ong", neighbourhood: "Stockholm")
print(n.fullname())
11 changes: 11 additions & 0 deletions 067-swift/2-var-let.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// mutable or changeable
var roomTemperature = 25 // celcius
roomTemperature = 20

print(roomTemperature)

// non-mutable or unchangeable
let speedOfLight = 299792458 // m/s
// speedOfLight = 300000000

print(speedOfLight)
5 changes: 5 additions & 0 deletions 067-swift/3-implicit-explicit-conversation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let implicitInteger = 70
let implicitDouble = 80.0
let explicitDouble: Double = 90

print(explicitDouble)
11 changes: 11 additions & 0 deletions 067-swift/4-explicit-string.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// http://cosmicdiary.org/fmarchis/files/2014/05/50-years-of-exploration.jpg
// http://waitbutwhy.com/2015/08/how-and-why-spacex-will-colonize-mars.html

let moonMissions = 72
let marsMissions = 38

let marsSummary = "There are \(marsMissions) mars missions till today."
let moonSummary = "There are \(moonMissions - marsMissions) more moon missions till today."

print(marsSummary)
print(moonSummary)
17 changes: 17 additions & 0 deletions 067-swift/5-array-dictionary.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// https://en.wikipedia.org/wiki/Internet_Protocol

var ip = [String]()
ip = [ "link", "internet", "transport", "application" ]

print(ip)
ip[3] = "app"
print(ip)

var layers = [
"link": [ "Ethernet", "WiFi", "Bluetooth" ],
"transport": [ "TCP", "UDP" ]
]

print(layers)
layers["app"] = [ "DNS", "HTTP", "SMTP" ]
print(layers)
17 changes: 17 additions & 0 deletions 067-swift/6-control-flow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// https://en.wikipedia.org/wiki/Planetary_mass

let planetMass = [0.06, 0.82, 1, 0.11, 317.8, 95.2, 14.6, 17.2]

var terrestrialPlanetMass: Double = 0
var jovianPlanetMass: Double = 0

for mass in planetMass {
if (mass > 1) {
jovianPlanetMass += mass
} else {
terrestrialPlanetMass += mass
}
}

print("Terrestrial planet mass is \(terrestrialPlanetMass) earth mass.")
print("Jovian planet mass is \(jovianPlanetMass) earth mass.")
20 changes: 20 additions & 0 deletions 067-swift/7-optional.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// http://voyager.jpl.nasa.gov/spacecraft/greetings.html
var spaceCraft: String = "Voyager"
// try assigning it nil
// try type String?
print(spaceCraft)

var greeting = "Kia ora"
var alien: String? = "fellow humanoid" // amend to nil
print(alien!) // force unwrap

if let life = alien {
greeting = "Kia ora, \(life)" // amend to \(alien)
}
print(greeting)


let nickname: String? = "Anna"
let fullname: String = "Anastasia"
let welcome = "Hi \(nickname ?? fullname)!"
print(welcome)
12 changes: 12 additions & 0 deletions 067-swift/8-switch-case.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let body = "comet 67P"

switch body {
case "mars":
print("Mars orbiter reached the Mars orbit in Sept 2014.")
case "jupiter", "uranus", "pluto":
print("New Horizons reached Pluto in July 2015")
case let x where x.hasPrefix("comet"):
print("Rosetta mission reached in August 2014")
default:
print("Hello cosmic missions!")
}
9 changes: 9 additions & 0 deletions 067-swift/9-limits.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
for i in 0..<4 {
print(i)
}

print("----")

for i in 0...4 {
print(i)
}
3 changes: 3 additions & 0 deletions 067-swift/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
067 Swift

For explanatory notes, video file, tool version and other info, please refer to the [screencast link](http://build-podcast.com/swift/)
49 changes: 49 additions & 0 deletions _posts/2016-04-09-swift.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: 067 Swift
title_lowercase: 067 swift
layout: post
tags: tutorial, screencast, technology, development, ios, swiftnew programming language developed by Apple. In this episode, we will go through some of the basic language attributes.
permalink: /swift/
enclosure: http://video.build-podcast.com/067-swift.mp4
length: 502969366
version: 2.2
website: https://developer.apple.com/swift/
---

<div id="video"><iframe width="560" height="315" src="//www.youtube.com/embed/Ib1PkE1dgtc" frameborder="0" allowfullscreen></iframe></div>

[Swift](https://developer.apple.com/swift/) is a fairly new programming language developed by Apple. In this episode, we will go through some of the basic language attributes.

<p><strong>Download video</strong>: <a href="http://video.build-podcast.com/067-swift.mp4" download="build-podcast-067-swift.mp4">mp4</a></p>

**Sample code**: [Github](https://github.com/sayanee/build-podcast/tree/master/067-swift)

**Version**: 2.2

## Background on Swift

1. [Learn one new programming language per year](http://martinfowler.com/bliki/OneLanguage.html)
- [Swift overview](https://developer.apple.com/swift/)
- [Welcome to Swift](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/)
- [Swift resources](https://developer.apple.com/swift/resources/)
- [Swift tour](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-ID1)

## Things to learn with Swift

### Basics

1. Download the [latest xCode](https://developer.apple.com/xcode/download/)
- Check the version in your command line: `swift --version`
- Check the common commands: `swift --help`
- Check where the executable is hosted: `which swift`
- Create a file called `1-print.swift` with the code

```swift
print("hello world")
```
- Run it in the command line with `swift {FILENAME}` E.g. `swift 1-print.swift`
- Try out the code and run them in the command line

## Build Link of this episode

Learn [iOS with Swift](https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson1.html)
6 changes: 3 additions & 3 deletions start/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<img src="http://build-podcast.com/logo.svg" class="start">

<div class="content">
<h1>066 ES2015</h1>
<h4>the next JS standard</h4>
<h6>Version: 2015</h6>
<h1>067 Swift</h1>
<h4>A modern programming language that is safe, fast, and interactive.</h4>
<h6>Version: 2.2</h6>
<img src="logo.png" class="episode-logo">
<p class="credit"></p>
</div>
Expand Down
Binary file modified start/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8706482

Please sign in to comment.