Monday, June 30, 2014



//Type inference 
var fruitList = ["apple", "banana"]

//Explicit format: 
var fruitList: Array<String> = ["apple", "banana"]
//Explicit shortcut format: 
var fruitList: [String] = ["apple", "banana"]

fruitList[1] = "orange"
//fruitList =["apple", "orange"]

Number of Items

println("The fruitList contains \(fruitList.count) items.")
//Console output: The fruitList contains 2 items.

Item Addition

fruitList[2] = "strawberry"
//Wrong: fatal error: Array index out of range

//Correct Method1: Use "append" to add item:
//fruitList = ["apple", "orange", "strawberry"]

//Correct Method2: Use "+=":
fruitList += ("kiwifruit")
//fruitList = ["apple", "orange", "strawberry", "kiwifruit"]

//Insert an Item to a specific location
fruitList.insert("mango", atIndex: 2)
//fruitList = ["apple", "orange", "mango", "strawberry", "kiwifruit"]

Item Removal

//fruitList = ["apple", "orange", "mango", "kiwifruit"]

var removedItem = fruitList.removeLast()
//fruitList = ["apple", "orange", "mango"]
//removedItem = "kiwifruit"

Check if the array is empty:

if fruitList.isEmpty {


for loop: Iterating over the array

for itemCount in fruitList {

//Console output:


Initialize an empty array
var intArray = [Int]()
//intArray is of type Int[]

println("intArray has \(intArray.count) items.")

//Console output: intArray has 0 items.

Add new item
//intArray = [2]

Reset the array

intArray = []

Other method for creating arrays
var oneArray = Int[](count: 2, repeatedValue: 1)  //[1, 1]
var zeroArray = Int[](count: 3, repeatedValue: 0) //[0, 0, 0]
var combinedArray = oneArray + zeroArray          //[1, 1, 0, 0, 0]

Saturday, June 28, 2014

if condition / switch case

Swift prevents any non-Boolean value in an if condition that requires a Bool.

let myCondition = 1
if myCondition {


let myCondition = 1
if myCondition == 1 {


let myCondition = true
if myCondition {


The switch case is as below:

var myCondition = 5 //Change this for different values

switch myCondition {
case 0:
    print("This is zero.")
case 1:
    print("myCondition is one.")
case 2,3:
    print("I like number two or three.")
case 4...10:
    print("Four to ten.\nfallthrough")
    print("The default case.")
    //print("Print more")

//Console output: myCondition is one.

//No "break" is required for each case.
//Default condition is essential.
//Use comma if the cases share the same codes, e.g. case 2, 3:
//Use ... to represent consecutive values
//Write fallthrough if the case below needs to be run.

//Change this:

let myCondition = 7

//Console output: 
Four to ten.
The default case.

//"Print more" is not printed because of the break.

IBM Swift Sandbox Example

Integers and Floating-Point Numbers

let implicitInteger = 50
let implicitDouble = 50.0
//Do not declare the type

let explicitDouble: Double = 50

let explicitFloat: Float = 5
//Floating-point number 5.0 is created.
//Declare the type

Type Casting
let x = Float(implicitInteger) * 1.5
//x = 75.0

Extra Underscores for readability
let oneThousand = 1_000
//oneThousand = 1,000

var y = 3 //OK
var y = 4 //Error
//Each constant / variable can be declared once only.

More on println calculation:

let chicken = 2
let rabbit = 4
println("One chicken and two rabbits have \(chicken*1 + rabbit*2) legs.")

//Console output: One chicken and two rabbits have 10 legs.

Hello World for non-programmers 新手程式設計師的第一段Swift程式

Update: January 10, 2016 - Add the print commands for Swift 2 and add a link to IBM Swift Sandbox.

Hello World - The first step in learning a new programming language. It usually displays a simple string on the terminal / console.

Execute the code below in IBM Swift Sandbox with the RUN button and try to modify the constants and variables in the code.
IBM Swift Sandbox中按RUN按鈕並嘗試改改程式中的常數及變數

//Swift 1:
//println("Hello World!")
//Swift 2:
print("Hello World!")
//Console output: Hello World!
//No ";" is required

let temperature = 20
let 我的常數 = 50
//Constant 常數
//Support Unicode 支援Unicode編碼 
//Type: Int  //Int整數型態(Integer)
//Initial value is required. 需要初始值
// \n standards for a new line character(return to a new line)
// \n 表示換行字元
print("Add a new line character here.\n")

let helloStr = "Hello"

//Characters included in two quotation marks are known as a string. For example, "Yes" is a string.

var mystr = "Hi ABC"
//Variable 變數
//Type: String //字串型態(Integer)
//Initial value is required. //需要初始值

//Swift 1:
//println("I want to say: \(mystr)")
//Swift 2:
print("I want to say: \(mystr)")
//Console output: I want to say: Hi ABC

mystr += "DE"
//mystr becomes "Hi ABCDE"
//Strings may be "added"

//Swift 1:
//println("I want to say: \(mystr)")
//Swift 2:
print("I want to say: \(mystr)")

//Comments will not be executed by the compiler.

//Single-line comment 

/* Multiple-line
   Comment */

/* Nested
/* Multiline */
   Comments  */
//This is different from C. 巢狀註解
//Good design for quick debugging

//Strings may be joined together with the + sign
let word1 = "The"
let word2 = "End"
print(word1 + " " + word2)

Swift Information 關於Swift的資訊

The Swift Programming Language

採用共筆翻譯的方式,整個翻譯團隊在 9 天內完成了近 670 頁的 Swift 語言文檔翻譯工作

Swift guide - chinese version

用Apple全新App開發語言Swift,4小時做出Flappy Bird遊戲

Flappy Bird source code

Chris Lattner:Swift 编程语言首席架构师


程式語言進化鏈的頂端:為什麼說蘋果的 Swift 正在顛覆整個網路生態?


Swift Resources

Update 2015/12/5
Apple Releases Swift as Open Source - December 3, 2015

Swift Programming Language Evolution says the upcoming release dates:
Swift 2.2 - Spring 2016

Swift 3.0 - Fall 2016

Apple 兌現承諾,Swift 語言已成開源

Update 2016/9

App Development with Swift