Skip to content

Latest commit

ย 

History

History
418 lines (308 loc) ยท 8.3 KB

swiftBasice-4.md

File metadata and controls

418 lines (308 loc) ยท 8.3 KB

Swift Basics - 4

์กฐ๊ฑด๋ฌธ

  • if
  • swhich
  • guard

if Statements

if์˜ ๊ธฐ๋ณธํ˜•

  • if๋Š” ๋ฐ˜๋“œ์‹œ Bool ๊ฐ’ (true, false)์„ ์‚ฌ์šฉํ•œ๋‹ค.
   ์‹คํ–‰๊ตฌ๋ฌธ
}

if ์กฐ๊ฑด  {
   ์‹คํ–‰๊ตฌ๋ฌธ
  } else {
   ์‹คํ–‰๊ตฌ๋ฌธ
}
// if

var temperatureInFahrenheit = 30

if temperatureInFahrenheit <= 32 {
  print("It's very cold. Consider wearing a scarf.")
}


// if - else

temperatureInFahrenheit = 40

if temperatureInFahrenheit <= 32 {
  print("It's very cold. Consider wearing a scarf.")
} else {
  print("It's not that cold. Wear a t-shirt.")
}


// if - else if - else

temperatureInFahrenheit = 90

if temperatureInFahrenheit <= 32 {
  print("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
  print("It's really warm. Don't forget to wear sunscreen.")
} else {
  print("It's not that cold. Wear a t-shirt.")
}


// if - else if

temperatureInFahrenheit = 72
if temperatureInFahrenheit <= 32 {
  print("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
  print("It's really warm. Don't forget to wear sunscreen.")
}

if ~ else if / if ๋ฌธ์„ 2๊ฐœ ํ•˜๋Š” ๊ฒƒ๊ณผ ์ฐจ์ด์ ?

var number = 9
if number < 10 {
  print("10๋ณด๋‹ค ์ž‘๋‹ค")
} else if number < 20 {
  print("20๋ณด๋‹ค ์ž‘๋‹ค")
}
// ์•ž์— ๊ฒƒ๋งŒ ์‹คํ–‰๋จ(์„ ํ–‰)

if number < 10 {
  print("10๋ณด๋‹ค ์ž‘๋‹ค")
}
if number < 20 {
  print("20๋ณด๋‹ค ์ž‘๋‹ค")
}

//๋‘๊ฐœ ๊ตฌ๋ถ„์ง€์–ด ๋†“์œผ๋ฉด ๋‘˜ ๋‹ค ์‹คํ–‰๋จ

switch Statements

switch ๊ธฐ๋ณธํ˜•

  • switch ๋ฌธ์€ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์‚ฌ๋ก€๋ฅผ ๋ฐ˜๋“œ์‹œ ๋‹ค๋ฃจ์–ด์•ผ ํ•œ๋‹ค. (Switch must be exhaustive)
switch ๊ฐ’ {
case ๋น„๊ต๊ฐ’ 1: 
     ์‹คํ–‰๊ตฌ๋ฌธ
case ๋น„๊ต๊ฐ’ 2:
     ์‹คํ–‰๊ตฌ๋ฌธ 
fallthrough // ๋‹ค์Œ case๊นŒ์ง€๋งŒ ๋งˆ์น˜๊ณ  ์ข…๋ฃŒ. 
case ๋น„๊ต๊ฐ’1, ๋น„๊ต๊ฐ’2, ๋น„๊ต๊ฐ’3 : //๋‹ค์ค‘์˜ ๊ฒฝ์šฐ  
     ์‹คํ–‰๊ตฌ๋ฌธ
default: //ํ•ด๋‹น๋˜์ง€ ์•Š๋Š” ๊ฐ’ ๋ฐ˜ํ™˜(else) 

Without default case

default์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

let isTrue = true
type(of: isTrue)

switch isTrue {
case true:
  print("true")
case false:
  print("false")
}

Interval Matching

๋ฒ”์œ„์™€ ๋งค์นญ

let approximateCount = 30

switch approximateCount {
case 0...50:
  print("0 ~ 50")
case 51...100:
  print("51 ~ 100")
default:
  print("Something else")
}```
    
```swift
switch temperatureInFahrenheit {
case 0...32 :
    print("It's very cold. Consider wearing a scarf.")
case 86... :
    print("It's really warm. Don't forget to wear sunscreen.")
default:
    print("It's not that cold. Wear a t-shirt.")
}

Compound Cases

  • if๋ฌธ์€ ์ฝค๋งˆ(,)๊ฐ€ And(&&)์—ฐ์‚ฐ์ธ ๊ฒƒ๊ณผ ๋ฐ˜๋Œ€๋กœ switch๋ฌธ์€ ์ฝค๋งˆ(,)๊ฐ€ Or(||) ์—ฐ์‚ฐ
let someCharacter: Character = "e"

switch someCharacter {
case "a", "e", "i", "o", "u": //์ด๊ฑฐ๋‚˜(OR)
  print("\(someCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
     "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
  print("\(someCharacter) is a consonant")
default:
  print("\(someCharacter) is not a vowel or a consonant")
}

value binding

x, y ์ขŒํ‘œ(x์ถ•, y์ถ• ํ™œ์šฉ)

let somePoint = (9, 0)

switch somePoint {
case (let distance, 0), (0, let distance): //x์ขŒํ‘œ๊ฐ€ 0์ด๊ฑฐ๋‚˜ y์ขŒํ‘œ๊ฐ€ 0
  print("On an axis, \(distance) from the origin") //distance๋งŒํผ ๋–จ์–ด์ง„ ์ขŒํ‘œ์— ์žˆ๋‹ค.
default:
  print("Not on an axis") //์ถ•์— ํ•ด๋‹นํ•˜์ง€ ์•Š๋Š”๋‹ค
}

### where clause

```swift 
let anotherPoint = (1, -1)
switch anotherPoint {
case let (x, y) where x == y: //x๊ฐ€ y๋ž‘ ๊ฐ™์„๋•Œ
    //(let x, let y, let z)์™€ ๋™์ผํ•œ ์˜๋ฏธ.
  print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y: // x๊ฐ€ -y์™€ ๊ฐ™์„๋•Œ
  print("(\(x), \(y)) is on the line x == -y")
case let (x, y)://ํ•ด๋‹น์—†์Œ
  print("(\(x), \(y)) is just some arbitrary point")
}

Question

  • ์–ด๋–ค ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์ง์ˆ˜์ธ์ง€ ํ™€์ˆ˜์ธ์ง€ ์ถœ๋ ฅ (switch์˜ where ์ ˆ ์ด์šฉ)
let even = 4
switch even {
case let x where x.isMultiple(of: 2):
                  //2 == 0 ์™€ ๋™์ผํ•œ ์˜๋ฏธ
  print("\(x)๋Š” ์ง์ˆ˜")
default:
  print("ํ™€์ˆ˜")
}
  • fallthrough ์ž‘๋™๋ฐฉ์‹
let integerToDescribe = 3
var description = "The number \(integerToDescribe) is"

switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
  description += " a prime number, and also" //
//  fallthrough
default:
  description += " an integer."
}
print(description)

//fallrhrough ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ํ•œ๋‹จ๊ณ„ ์•„๋ž˜๊นŒ์ง€๋งŒ ์‹คํ–‰๋จ

Early Exit

guard statement

  • _if_๋ฌธ์˜ ๊ฒฝ์šฐ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด ํ•ด๋‹น ๋ถ„๊ธฐ๋ฌธ์„ ์‹คํ–‰ํ•˜์˜€์œผ๋‚˜, _guard_๊ตฌ๋ฌธ์˜ ๊ฒฝ์šฐ ์กฐ๊ฑด์— ๋งž์ง€ ์•Š์œผ๋ฉด ๋ฐ”๋กœ ์ข…๋ฃŒ๋จ.
func someFunction() {
  // ...
  // ...
  
  // if ๋ฌธ์˜ ์กฐ๊ฑด์ด ๋งž์œผ๋ฉด ์–ด๋–ค ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•  ๊ฒƒ
  if Bool.random() { //๋ถˆ๊ฐ’์ค‘. ์ฐธ,๊ฑฐ์ง“์ค‘ ํ•˜๋‚˜๋ฉด,
    // ...
  }
//print(1) ์ถœ๋ ฅ
    
  // gaurd๋ฌธ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ๋งŒ ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐˆ ๊ฒƒ
  guard Bool.random() else { return }

    //print(1) //๊ฐ€๋“œ๋ฌธ์˜ ๊ฒฝ์šฐ , ํŠธ๋ฃจ๋ฉด ์ถœ๋ ฅ , ํŽ„์Šค๋ฉด ์ถœ๋ ฅ ์•ˆ๋จ
  • ๊ฐ€๋“œ๋ฌธ์€ ๋“ค์—ฌ์“ฐ๊ธฐ๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋‹ค. (์ผ๋ ฌ๋กœ ์ญ‰ ๋‚˜์—ด)

guard๋ฌธ ์˜ˆ์‹œ

func update(age: Int) {
  guard 1...100 ~= age else { return }//์ข…๋ฃŒ์ฝ”๋“œ
  print("Update")
}

update(age: -1)
update(age: 2)
update(age: 100)

Tuples

Unnamed Tuple

let number: Int = 10

let threeNumbers: (Int, Int, Int) = (1, 2, 5)
type(of: threeNumbers)

threeNumbers

threeNumbers.0//1
threeNumbers.1//2
threeNumbers.2//5


var threeValues: (Int, Double, String) = (10,  100.0,  "์ด๋ฆ„")
type(of: threeValues)//ํƒ€์ž…์ถ”์ถœ

threeValues

threeValues.0 = 5
threeValues
  • Tuples์˜ ์žฅ์ 

    • ์ž„์˜์˜ ์ด๋ฆ„๊ณผ ์ˆœ์„œ ํ™œ์šฉ
    • ํƒ€์ž… ํ˜ผํ•ฉํ•˜์—ฌ ์ด์šฉ๊ฐ€๋Šฅ

Decomposition

let numbers = threeNumbers
numbers
numbers.0 //1
numbers.1 //2


let (first, second, third) = threeNumbers
first//1
second//2
third//3


// wildcard pattern

let (_, second1, third1) = threeNumbers //_์™€์ผ๋“œ์นด๋“œํŒจํ„ด : ์ฒซ๋ฒˆ์งธ ๊ฒƒ์€ ๋ฐ›๊ณ  ์‹ถ์ง€. ์•Š๊ณ  ์ƒ๋žตํ•˜๊ณ  ์‹ถ๋‹ค.
second1 //2
third1 //5

Named Tuple

๊ธฐ๋ณธ๊ตฌ์กฐ

let iOS: (language: String, version: String) = (language: "Swift", version: "5")
let iOS: (language: String, version: String) = ("Swift", "5")
let iOS = (language: "Swift", version: "5")

ํ™œ์šฉ

let iOS = (language: "Swift", version: "5")

iOS.0//swift
iOS.1//5

iOS.language
iOS.version


func ํŠœํ”Œ(a: Int, b: (Int, Int)) -> (first: Int, second: Int) {
  return (a + b.0,  a + b.1)
}//10+20 , 10+30

let result = ํŠœํ”Œ(a: 10, b: (20, 30))
result.first //30
result.second //40

result.0 //30
result.1 //40

Comparison Operators

Tuple ์€ 7๊ฐœ ๋ฏธ๋งŒ ์š”์†Œ์— ๋Œ€ํ•œ ๋น„๊ต ์—ฐ์‚ฐ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Œ(6๊ฐœ๊นŒ์ง€๋งŒ ์“ธ ์ˆ˜ ์žˆ์Œ).7๊ฐœ ์ด์ƒ์˜ ์š”์†Œ๋ฅผ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ

var something1: (Int, Int, Int, Int, Int, Int) = (1,2,3,4,5,6)
var something2: (Int, Int, Int, Int, Int, Int) = (1,2,3,4,5,6)
something1 == something2
  • ํŠœํ”Œ ๋น„๊ต ์—ฐ์‚ฐ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋Š”?
(1, "zebra") < (2, "apple") //true
(3, "apple") > (3, "bird")  //false ์•„์Šคํ‚ค
("blue", 1) > ("bluesky", -1) //false
("์ผ", 1) > ("์ด", 2.0) //true //ํ•œ๊ธ€ : ์ดˆ์„ฑ,์ค‘์„ฑ,์ข…์„ฑ ใ…‡+ใ…ฃ+ใ„น > ใ…‡+ใ…ฃ

//(1, "zebra") < ("2", "apple")// ๋น„๊ต๋ถˆ๊ฐ€ -> ํƒ€์ž…์ด ๋‹ฌ๋ผ์„œ error
//("blue", false) < ("purple", true)// ํŠธ๋ฃจ,ํŽ„์Šค๋Š” ๋Œ€์†Œ๋น„๊ต๋ถˆ๊ฐ€ error

Tuple Matching

let somePoint = (1, 0)

switch somePoint {
case (0, 0):
  print("\(somePoint) is at the origin") //x, y๊ฐ€ 0์ผ ๋•Œ
case (_, 0):
  print("\(somePoint) is on the x-axis") //y๊ฐ€ 0์ผ ๋•Œ
case (0, _):
  print("\(somePoint) is on the y-axis") //x๊ฐ€ 0์ผ ๋•Œ
case (-2...2, -2...2):
  print("\(somePoint) is inside the box")//๊ฐ๊ฐ์˜ ๊ฐ’ ๋ชจ๋‘ ์ถฉ์กฑ
default:
  print("\(somePoint) is outside of the box") //์™ธ
}

// ๋งจ ์œ„๋ถ€ํ„ฐ ๊ฒ€์‚ฌํ•˜๋ฏ€๋กœ ์ˆœ์„œ์ค‘์š”

Dictionary Enumeration

๊ธฐ๋ณธํ˜•

let ํ•จ์ˆ˜ = ["ํ‚ค":"๊ฐ’"] ```

```swift
let fruits = ["A": "Apple", "B": "Banana", "C": "Cherry"]

for (key, value) in fruits {
  print(key, value)
}
print()


for element in fruits {
    // ์–ด๋–ค ์‹์œผ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ• ๊นŒ์š”?
  //print(element.0,element.1) //ํŠœํ”Œ๋„˜๋ฒ„ ์ด์šฉ
    //prein(element.key, element.value) //ํŠœํ”Œ๋„ค์ž„ ์ด์šฉ
}