๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

iOS/Swift

[1day - 6day TIL] 100 days of swift

 

 

1์ผ์ฐจ

 

Coursera ๊ฐ•์˜์™€ ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์ด ๋งŽ์•„์„œ ๊ณต๋ถ€ํ•˜๋Š” ๋ฐ์— ํฌ๊ฒŒ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ์ง€ ์•Š์•˜๋‹ค. ๊ทธ๋ž˜๋„ ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ๊ฐœ๋…๋„ ์žˆ์œผ๋‹ˆ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด์ž.

 

  • Multi-line strings
var str2 = """
This goes \
over multiple \
lines
"""

//output: This goes over multiple lines

=> / ๋ฅผ ํ™œ์šฉํ•ด ๋ฌธ์žฅ์ด ๋‚˜๋‰˜์ง€ ์•Š๊ณ  ํ•œ ๋ฌธ์žฅ์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ž์—ด์ด ๊ธธ ๋•Œ ๊น”๋”ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค. 

 

var str1 = """
This goes
over multiple
lines
"""

//output:
// This goes
// over multiple
// lines

=> ์ด ์•„์ด๋Š” ์ž…๋ ฅํ•œ ๊ทธ๋Œ€๋กœ ๋ฌธ์ž์—ด์ด ์‚ฐ์ถœ๋œ๋‹ค. 

 

 

 

2์ผ์ฐจ

 

์˜ค๋Š˜์€ tuple, Set, array ์˜ ๊ฐœ๋…์„ ์ •๋ฆฌํ•˜์˜€๊ณ  ์ด๋“ค์ด ์–ด๋–ค ์ƒํ™ฉ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ง€์— ๋Œ€ํ•ด ๋ฐฐ์› ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Enumeration(์—ด๊ฑฐ)๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ฐœ๋…์„ ๋ฐฐ์› ๋‹ค. 1์ผ์ฐจ์™€๋Š” ๋‹ฌ๋ฆฌ ์ƒˆ๋กญ๊ฒŒ ์ ‘ํ•œ ๊ฐœ๋…์ด ์žˆ์–ด์„œ ๋ฐ˜๋ณตํ•ด์„œ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ธฐ๋„ ํ•˜์˜€๋‹ค. 

 

  • Tuple 
let address = (house: 555, street: “Taylor”, city: “Mexico”)

=> ์„œ๋กœ ๊ด€๋ จ์žˆ๋Š” value๋ผ๋ฆฌ ๋ฌถ์„ ๋•Œ ์‚ฌ์šฉ. collection์ด ๊ณ ์ •๋˜๊ณ  ํŠน์ •๊ฐ’์ด ์ •ํ•ด์ ธ ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. 

 

  • Set
let colors = Set(["red", "blue", "green"]) // output: {"green", "blue", "red"} : unordered(random)
let colors2 = Set(["red", "blue", "green", "green", "blue", "red"]) // ๋ฐ˜๋ณต ์ œ๊ฑฐ๋˜๊ณ  random ํ•˜๊ฒŒ ๋ฐฐ์—ด

=> ํŠน์ • ๊ฐ’์ด ๋ฐฐ์—ด์— ์žˆ๋Š”์ง€ ๋น ๋ฅด๊ฒŒ ํ™•์ธํ•  ๋•Œ ์‚ฌ์šฉ

 

  • Array

=> ์ค‘๋ณต๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ฑฐ๋‚˜ ๋ฐฐ์—ด ๋‚ด ์ˆœ์„œ๊ฐ€ ์ƒ๊ด€์ด ์žˆ์„ ๋•Œ ์‚ฌ์šฉ. ๊ฐ€์žฅ ํ”ํ•˜๊ฒŒ ์‚ฌ์šฉํ•จ. 

 

  • Dictionaries
let heights = [
	// key : value
	“Taylor Swift”: 1.78,
	“Ed Sheeran”: 1.73
]

=> ์—ฌ๊ธฐ์„œ ์กด์žฌํ•˜์ง€ ์•Š๋Š” key๊ฐ’์— ๋Œ€ํ•œ value๋ฅผ ์ฐพ์„ ๊ฒฝ์šฐ nil ์ด ์‚ฐ์ถœ๋จ. ์ด๋ ‡๊ฒŒ ํ•˜๊ธฐ ์‹ซ์œผ๋ฉด favoriteIceCream[“Charlotte”, default:”Unknown”] ์ฒ˜๋Ÿผ ์กด์žฌํ•˜์ง€ ์•Š๋Š” key ๊ฐ’์— ๋Œ€ํ•œ default ๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋จ.

 

  • Enumerations(์—ด๊ฑฐ)
enum Result {
	case success
	case failure
}

let result4 = Result.failure // output: failure
let result = Result(rawValue: 1) // output: failure (์ˆœ์„œ ์ง€์ •๋„ ๊ฐ€๋Šฅ)

=> rawValue๋ฅผ ๋”ฐ๋กœ ์ง€์ •ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ 0๋ถ€ํ„ฐ ์‹œ์ž‘. 

 

 

3์ผ์ฐจ

 

  • Ternary Operator 
let firstCard = 11
let secondCard = 10
print(firstCard == secondCard ? “Cards are the same” : “Cards are different)

=>  print(์กฐ๊ฑด ? true value : false value)

 

 

  • Switch Case
let weather = "sunny"

switch weather {
case "rain":
    print("Bring an umbrella")
case "snow":
    print("Wrap up warm")
case "sunny":
    print("Wear sunscreen")
    fallthrough
default:
    print("Enjoy your day")
} 

// output: 
// Wear sunscreen
// Enjoy your day

=> switch case์—๋Š” default ๊ฐ’์ด ๋ฌด์กฐ๊ฑด ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

* fallthrough: ์ด์–ด์„œ ๋‹ค์Œ case๋ฌธ์˜ ์ฝ”๋“œ๊นŒ์ง€ ์‹คํ–‰๋œ๋‹ค. 

 

 

4์ผ์ฐจ

 

  • While loop ์™€ repeat ๋ฐ˜๋ณต๋ฌธ์˜ ์ฐจ์ด 

=> While loop์˜ ๊ฒฝ์šฐ ์กฐ๊ฑด์„ ๋จผ์ € ์ฒดํฌํ•œ ๋’ค ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•˜์ง€๋งŒ repeat์˜ ๊ฒฝ์šฐ ์ผ๋‹จ ๋จผ์ € ์‹คํ–‰ํ•œ๋‹ค. 

 

  • Continue 

=> skip ์˜ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. 

 

  • Infinite loop 

=> ๋ฌดํ•œ๋ฃจํ”„๋ผ๊ณ  ํ•˜๋ฉฐ while true ๋ฅผ ์ด์šฉํ•ด์„œ ๋งŒ๋“ ๋‹ค. 

 

  • break outer์˜ ์˜๋ฏธ 
outer: for i in 1...5 {
    for j in 1...5 {
        let product = i * j
        print("Product is \(product).")
        break outer
    }
} // output: Product is 1.

=> ๋ฐ”๊นฅ์ชฝ ๋ฐ˜๋ณต๋ฌธ์„ break ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. 

 

 

5์ผ์ฐจ

  • Accepting parameters
func square(number: Int) {
	print(number * number)
}

square(number: 8) //64

=> ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ •์ˆ˜ํ˜• ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. 

 

  • Returning values 
func square(number: Int) -> Int {
    return number * number
}

let result = square(number: 8)
print(result) // 64

=> ์ž…๋ ฅ๋ฐ›์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ value ๊ฐ’์„ return ํ•œ ํ›„ print ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

 

  • Parameter label 
func sayhello(to name: String) {
    print("Hello \(name)")
}

sayhello(to: "Gina") // output: Hello Gina

=> ์—ฌ๊ธฐ์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” to name์ด๊ณ  ์™ธ๋ถ€์ ์œผ๋กœ๋Š” to ๋ผ๊ณ  ๋ถˆ๋ฆฌ์ง€๋งŒ ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” name์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์ด๋‹ค. 

 

  • Default Parameter
func greet(_ person: String, nicely: Bool = true) {
    if nicely == true {
        print("Hello,\(person)!")
    } else {
        print("Oh no, it's \(person)!")
    }
}

greet("Taylor") // Hello, Taylor!
greet("Taylor", nicely: false) // Oh no, it's Taylor!

=> ํŒŒ๋ผ๋ฏธํ„ฐ ์•ž์— ๋ถ™์€ _๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด 'ํŒŒ๋ผ๋ฏธํ„ฐ:'๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. 

 

  • variadic functions 
func square(numbers: Int...){
    for number in numbers {
        print("\(number) squared is \(number * number)")
    }
}

square(numbers: 1, 2, 3, 4, 5) 
// output: 
// 1 squared is 1
// 2 squared is 4
// 3 squared is 9
// 4 squared is 16
// 5 squared is 25

=> Int... ๊ฐ™์€ ๊ฒƒ์ธ variadic functions์ธ๋ฐ ์ด๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฐ’์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. 

 

  • throw, do, try, catch
// enumํ˜•์œผ๋กœ error ํƒ€์ž…๋ช… ์ •์˜
enum PasswordError: Error {
    case obvious
}

// ์˜ค๋ฅ˜ ๋‚˜๋Š” ์กฐ๊ฑด์„ throws ์™€ ํ•จ๊ป˜ ๋ฐฐ์น˜ 
func checkPassword(_ password: String) throws ->
    Bool {
    if password == "password" {
        throw PasswordError.obvious
    }
    
    return true
}

do {
    try checkPassword("password")
    print("That password is good!")
} catch {
    print("You can't use them")
} // output: You can't use them

=> throws ๋ฅผ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ๋งŒ throw๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„์˜ ๊ฒฝ์šฐ ์˜ค๋ฅ˜๊ฐ’์„ throwํ•˜๊ธฐ ๋•Œ๋ฌธ์— "You can't use them"์ด ์ถœ๋ ฅ๋œ๋‹ค. ์ด์™€ ๊ฐ™์ด ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ์ด์œ ๋Š” optional ํƒ€์ž…์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์˜ค๋ฅ˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์™ธ๋ถ€๋กœ ์ „๋‹ฌํ•  ๋ฐฉ๋ฒ•์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

  • inout ๋งค๊ฐœ๋ณ€์ˆ˜ 
func doubleInPlace(number: inout Int){
    number *= 2
}

var myNum = 10
doubleInPlace(number: &myNum) // 20

=> swift์—์„œ ํ•จ์ˆ˜์™€ ๋ฉ”์†Œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์ˆ˜๋กœ ์ „๋‹ฌ์ด ๋˜๊ณ , ํ•ด๋‹น ๊ฐ’์„ ์ง์ ‘ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค. ์ˆ˜์ •์„ ํ•˜๋ ค๋ฉด ๋‚ด๋ถ€์—์„œ ๋ณ„๋„์˜ ์ง€์—ญ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ ๋Œ€์ž…ํ•˜๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•˜๋‹ค. inout ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ๋ณ€๊ฒฝ๋˜๋ฉด ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ์›๋ณธ ๋ณ€์ˆ˜๊ฐ’์—๋„ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค. 

=> ์ด๋ฆ„ ์•ž์— &(์•ฐํผ์ƒŒ๋“œ)๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค

 

 

 

6์ผ์ฐจ

 

  • Closure (์ต๋ช…ํ•จ์ˆ˜)

-  ํด๋กœ์ €๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. 

let driving = { (place: String) in 
	print(“I’m going to \(place) In my car”)
}

driving(“London”)