[Swift] Array, Set, Tuple, Dictionary

[Swift] Array, Set, Tuple, Dictionary

Array

  • ๋ฐฐ์—ด ํƒ€์ž…
  • ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•˜๊ณ , ์ค‘๋ณต์„ ํ—ˆ์šฉํ•œ๋‹ค.

์„ ์–ธ ๋ฐฉ๋ฒ•

let john = "John Lennon"
let paul = "Paul McCartney"
let george = "George Harrison"
let ringo = "Ringo Starr"

let beatles = [john, paul, george, ringo]


ํƒ€์ž… ๋ช…์‹œ๋Š” [Int], [String]์ฒ˜๋Ÿผ ํ•œ๋‹ค

let numbers: [Int] = [0, 1, 2]

Set

  • ๋ฐ์ดํ„ฐ ์ €์žฅ ์ˆœ์„œ๊ฐ€ ์—†๊ณ , ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.

  • ๋‹จ์ˆœํ•œ exist ํ™•์ธ์ด๋ผ๋ฉด Set์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ(๋น ๋ฆ„!)

์„ ์–ธ ๋ฐฉ๋ฒ•

let colors = Set(["red", "green", "blue"])

ํƒ€์ž… ๋ช…์‹œ

let numbers: Set = Set([0, 1, 2])
//or
let numbers: Set<Int> = Set([0, 1, 2])

Tuple

  1. Tuple์— ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์—†์Œ (๊ณ ์ •๋œ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง)
  2. Tuple์˜ ์•„์ดํ…œ ํƒ€์ž…์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Œ (์ƒ์„ฑํ•˜๊ณ ๋‚˜๋ฉด ํ•ญ์ƒ ๋™์ผํ•œ ํƒ€์ž… ์œ ์ง€)
  3. Tuple์˜ ์•„์ดํ…œ์— ์ ‘๊ทผํ•  ๋•Œ๋Š” index, ์•„์ดํ…œ ์ด๋ฆ„ ๋ชจ๋‘ ๊ฐ€๋Šฅ (์กด์žฌํ•˜์ง€ ์•Š๋Š” index๋‚˜ ์ด๋ฆ„ ์ ‘๊ทผ์€ ๋ถˆ๊ฐ€๋Šฅ)

์„ ์–ธ ๋ฐฉ๋ฒ•

var name = (first: "Taylor", last: "Swift")

์ ‘๊ทผ ๋ฐฉ๋ฒ•

name.0
//or
name.first

Arrays vs sets vs tuples

  • Tuple: ๊ฐ ์•„์ดํ…œ์ด ๊ณ ์ •๋œ ํฌ์ง€์…˜ ๋˜๋Š” ์ด๋ฆ„์„ ๊ฐ€์ ธ์•ผ ํ•  ๋•Œ
let address = (house: 555, street: "Taylor Swift Avenue", city: "Nashville")


  • Set: ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š์•„์•ผ ํ•˜๊ณ , ํŠน์ • ์•„์ดํ…œ์ด ํฌํ•จ๋˜๋Š”์ง€ ๋น ๋ฅธ ํ™•์ธ์ด ํ•„์š”ํ•  ๋•Œ
let set = Set(["aardvark", "astronaut", "azalea"])


  • Array: ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜๊ณ , ์•„์ดํ…œ์˜ ์ˆœ์„œ๊ฐ€ ์ฃผ์š” ๋ชฉ์ ์ผ ๋•Œ (์…‹ ์ค‘ ๊ฐ€์žฅ ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” ํƒ€์ž…)
let pythons = ["Eric", "Graham", "John", "Michael", "Terry", "Terry"]

Dictionaries

  • integer position ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ
let heights = [
    "Taylor Swift": 1.78,
    "Ed Sheeran": 1.73
]


: ์˜ ์™ผ์ชฝ์ด ํ‚ค, ์˜ค๋ฅธ์ชฝ์ด ๊ฐ’
๊ฐ’์„ ์ฝ์„ ๋•Œ [ ] ์ด์šฉ

heights["Taylor Swift"]


Note: ํƒ€์ž… ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” [String: Double] , [String: String] ์ฒ˜๋Ÿผ ์ •์˜

Dictionary default values

์กด์žฌํ•˜์ง€ ์•Š๋Š” key์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ nil์„ ๋ฐ˜ํ™˜

let favoriteIceCream = [
    "Paul": "Chocolate",
    "Sophie": "Vanilla"
]

favoriteIceCream["Paul"] // == "Chocolate"

favoriteIceCream["Charlotte"] // == nil


default value๋ฅผ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ์Œ

favoriteIceCream["Charlotte", default: "Unknown"] // == Unknown

Creating empty collections

Array, Set, Dictionary ๋ชจ๋‘ ๊ฐ’์„ ํ•œ ๊ณณ์— ์ €์žฅํ•œ๋‹ค๋Š” ์˜๋ฏธ๋กœ collection์ด๋ผ๊ณ  ๋ถ€๋ฆ„

empty collection ์ƒ์„ฑ ๋ฐฉ๋ฒ•

var teams = [String: String]() //empty dictionary

var results = [Int]() //empty array


Empty Set ์ƒ์„ฑ ๋ฌธ๋ฒ•์€ ์กฐ๊ธˆ ๋‹ค๋ฆ„

var words = Set<String>()
var numbers = Set<Int>()


Array, Dictionary๋„ ๊ฐ™์€ ๋ฌธ๋ฒ•์œผ๋กœ ์ƒ์„ฑ ๊ฐ€๋Šฅ

var scores = Dictionary<String, Int>()
var results = Array<Int>()

Enumerations

enum Result {
    case success
    case failure
}

let result4 = Result.failure


๊ฐ case๋งˆ๋‹ค ๊ด€๋ จ๋œ ๊ฐ’์„ ์ถ”๊ฐ€๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Œ

enum Activity {
    case bored
    case running(destination: String)
    case talking(topic: String)
    case singing(volume: Int)
}

let talking = Activity.talking(topic: "football")

Enum Raw Value

Enum์ด ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž… ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ , ๊ทธ ๊ฐ’์œผ๋กœ Enum์˜ case๋ฅผ ์–ป์–ด์˜ฌ ์ˆ˜ ์žˆ์Œ

Int์ธ ๊ฒฝ์šฐ Swift๋Š” ์ž๋™์œผ๋กœ 0๋ถ€ํ„ฐ ๋ฒˆํ˜ธ๋ฅผ ๋งค๊น€

enum Planet: Int {
    case mercury
    case venus
    case earth
    case mars
}

let earth = Planet(rawValue: 2) //return Planet.earth


๋˜๋Š” ํŠน์ • ์ˆซ์ž๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋„๋ก ์ง€์ • ๊ฐ€๋Šฅ

enum Planet: Int {
    case mercury = 1
    case venus
    case earth
    case mars
}


๋‹ค๋ฅธ ํƒ€์ž… ์˜ˆ์‹œ

enum Planet: String {
    case mercury
    case venus
    case earth = "test"
    case mars
}

let earth = Planet(rawValue: "test")

Comments