---
# System prepended metadata

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

---

---

title: Swift 成長之路#3
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 成長之路 #3

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

---

### if

好高階喔，超多用法的

#### 最常見的 if-else
```swift=
var score = 10
if score > 500{
    // do something
} else if score > 250{
    // do something
} else {
    // do something
}
```

#### 三元運算子
```swift=
var teamScore = 11
let scoreDecoration = if teamScore > 10 {
    "🎉"
} else {
    ""
}
print("Score:", teamScore, scoreDecoration)
// Prints "Score: 11 🎉"
```

#### 我還看不懂的東西
```swift=
var optionalString: String? = "Hello"
print(optionalString == nil)
// Prints "false"


var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
    greeting = "Hello, \(name)"
}
```
之後再用懂，這個功能之前沒看過喔

#### for loop

```swift=
let allStars = ["James", "Davis", "Harden", "Doncic", "Leonard"]

for player in allStars {
    print(player)
}
```

```swift=
for i in 0...25{
    // 幹 真的超好讀啊！媽的，就是很確實 0 到 25
    // i 會從 0 跑到 25 跟 Python 的 range(0, 25) 不一樣
}
```

```swift=
//一個空的 Int Array 拿來存等一下的隨機數
var randomNumArray: [Int] = []

// 存 25 個隨機數進去 randomNumArray  
for i in 0..<25{ // i 會從 0 跑到 24
    let randNum = Int.random(in: 0...100) // Int 的 random Function
    randomNumArray.append(randNum) // Array 的 append
}

print(randomNumArray)
```

但是 `i` 不會用到啊（Dummy Variable），所以 swift 可以用底線 `_`  來表示 dummy variable。

```swift=
for _ in 0..<25{ // i 會從 0 跑到 24
    let randNum = Int.random(in: 0...100) // Int 的 random Function
    randomNumArray.append(randNum) // Array 的 append
}
```