Swift Study Note No.1 - Basics

Just like Cheng Zhao said, quote:

“其实网上的大部分的教程都非高手写成,而是新手在学习过程中的总结。有大量原创入门教程的知名博客,并不意味着博主是一个绝世高手,而是说明他是一个孜孜不倦的学习者。”

I am writing this as a study note for myself and if it could help you in any way, I am glad.

This note will be structured similar to the Lynda online course Swift Programming Language First Look with Simon Allardice.

Introduction

Swift is a new language. I am not going to say more about its history or differences between Swift and other languages. Please Google. Or hey Siri.

Using Xcode

As a swiftly (ha…) updating language, Swift changes a lot (I guess) since its publication. You should always use the updated Xcode for programming. (Please ignore my naive advice if you are coding for production.)

Core Syntax

The structure of Swift

Go and find out yourself.

Writing Swift in playgrounds

Try it, please.

Declaring variables

1
var myVariable = 1

Creating constants

1
let myVariable = "this lable"

Printing values

1
print myVarialbe

Writing if statements

1
2
3
4
5
if score > 50 {
some codes
} else {
some other codes
}

Using the switch statement

1
2
3
4
5
6
7
8
9
10
let vegetable = "apple"

switch vegetable {
case "apple":
let comment = "Good."
case "pine":
let comment = "OK."
default:
let comment = "whats that?"
}

Creating loops in Swift

1
2
3
for score in scores {
some codes
}

Defining functions

1
2
3
func funcName (name : String) {
some codes
}

Creating and using arrays

1
var list = ["a","b"]

Using dictionaries

1
var member : [String : String] = ["Wang":"Shanghai", "Xia":"Guangzhou"]

Understanding tuples

1
(Int, Int)

Creating optional variables

1
2
3
(Int, Int)?
(Int?,Int?)
# Please note that they are different.

Defining and using enumerations

1
2
3
4
5
6
enum CompassPoint {
case North
case South
case East
case West
}

Writing closures

1
2
3
{
some lines of code
}

Creating classes and instantiating objects

1
2
3
4
5
6
7
8
9
10
11
class videoMode {
var resolution = Resolution()
var interlaced = false
var frameRate = 0.0
var name: String?
}

struct Resolution {
var width = 0
var height = 0
}

Conclusion

Thats it. Please correct me if any.