---
# System prepended metadata

title: Swift 成長之路#2
tags: [' iOS', ' 初學者', ' Dev', Swift, ' beginer']

---

---

title: Swift 成長之路#2
tags: Swift, beginer, 初學者, iOS, Dev

---
<style>

html, body, .ui-content {
    background-color: #333;
    color: #ddd;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
    color: #ddd;
}

.markdown-body h1,
.markdown-body h2 {
    border-bottom-color: #ffffff69;
}

.markdown-body h1 .octicon-link,
.markdown-body h2 .octicon-link,
.markdown-body h3 .octicon-link,
.markdown-body h4 .octicon-link,
.markdown-body h5 .octicon-link,
.markdown-body h6 .octicon-link {
    color: #fff;
}

.markdown-body img {
    background-color: transparent;
}

.ui-toc-dropdown .nav>.active:focus>a, .ui-toc-dropdown .nav>.active:hover>a, .ui-toc-dropdown .nav>.active>a {
    color: white;
    border-left: 2px solid white;
}

.expand-toggle:hover, 
.expand-toggle:focus, 
.back-to-top:hover, 
.back-to-top:focus, 
.go-to-bottom:hover, 
.go-to-bottom:focus {
    color: white;
}


.ui-toc-dropdown {
    background-color: #333;
}

.ui-toc-label.btn {
    background-color: #191919;
    color: white;
}

.ui-toc-dropdown .nav>li>a:focus, 
.ui-toc-dropdown .nav>li>a:hover {
    color: white;
    border-left: 1px solid white;
}

.markdown-body blockquote {
    color: #bcbcbc;
}

.markdown-body table tr {
    background-color: #5f5f5f;
}

.markdown-body table tr:nth-child(2n) {
    background-color: #4f4f4f;
}

.markdown-body code,
.markdown-body tt {
    color: #eee;
    background-color: rgba(230, 230, 230, 0.36);
}

a,
.open-files-container li.selected a {
    color: #5EB7E0;
}

</style>

# Swift 成長之路 #2

選擇這當作我的第一堂課
[Swift Programming Tutorial | FULL COURSE | Absolute Beginner](https://www.youtube.com/watch?v=CwA1VWP0Ldw)
影片開頭提到，他不會教太深，我猜應該是很新手向的教學。

---

## Type

用 `var` 宣告變數、用 `let` 宣告<font color=lightgreen>常數</font>
```swift=
var myVariable = 42
myVariable = 50
let myConstant = 42
```

Swift 有 Type Inference，Compiler 會自己猜 Data Type
```swift=
var what = 4.1
print(type(of: what)) //Double
print(what) // 4.1
```
小數預設為 Double
若要指定 Data Type，例如 Float
```swift=
var what: Float = 4.1
print(type(of: what)) //Float
print(what) // 4.1
```
Data Type 不會隱性轉型（可以稱作 Strongly Type）
[關於強弱型別 或 Dynamic/Static Type 的解釋](https://medium.com/@cpave3/understanding-types-static-vs-dynamic-strong-vs-weak-88a4e1f0ed5f)

```swift=
let label = "The width is "
let width = 94
let widthLabel = label + String(width)
```

如果不轉

![image](https://hackmd.io/_uploads/r17DS9ui6.png)

也可以用 String Format 啦

```swift=
let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit."
```

三個 Double quote 也有支援

```swift=
let quotation = """
        Even though there's whitespace to the left,
        the actual lines aren't indented.
            Except for this line.
        Double quotes (") can appear without being escaped.

        I still have \(apples + oranges) pieces of fruit.
        """
```
這個 multi-line string 設計的比 Python、Java 好
Python、Java 都會正常照搬 indentation 但視覺上就不好讀

![image](https://hackmd.io/_uploads/rktezoOsT.png)

![image](https://hackmd.io/_uploads/r1q57jdip.png)

有效的 indentation 從 2 次 Tab 開始

### Array
Array 的型別宣告長醬
```swift=
var a: [Int] = []
```
如果是 empty Array 一定要宣告型別

Array 中的 element 不能不同 Type

![image](https://hackmd.io/_uploads/SkMBEjds6.png)

很讚😎👍🏻，Python 不知道在衝三小

### Set 

```swift=
var ages = [1, 2, 3, 4, 5]
var agesSet = Set(ages)
print(agesSet)
```

實作用 Hash Table

### Dictionary

```swift=
let devices: [String: Int] = [
    "iPhone": 2000,
    "Macbook": 3999,
    "iPad": 4359
]
print(devices)
print(devices["iPhone"])
```

### Function
```swift=
func add(firstNumber: Int, to secondNumber: Int) -> Int{
    let sum = firstNumber + secondNumber
    return sum
}

print(add(firstNumber: 43, to: 70))
```

那個 `->` 是 output 什麼東西的意思
我好喜歡這種會把 type 講清楚的語言
Python 太自由了啦😣 什麼東西都要自己心裡想

為了增加程式的可讀性，Swift 還有 argument label 跟 parameter label 

`add(firstNumber: 43, 70)` 跟 `add(firstNumber: 43, to: 70)`

有一個介系詞就變得更好讀了，那個介系詞就是 argument label 可以設定成任意詞，但只能一個，本意也是希望你增加可讀性，所以你亂寫就是 Bad。

---

我發現有基礎的話，看[官網的教學](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/guidedtour)很快欸
