-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
257 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
print("hello world") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.