μμ±μΌ: 2020.09.06
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
- Tupleμ μμ΄ν μ μΆκ°νκ±°λ μμ ν μ μμ (κ³ μ λ ν¬κΈ°λ₯Ό κ°μ§)
- Tupleμ μμ΄ν νμ μ λ³κ²½ν μ μμ (μμ±νκ³ λλ©΄ νμ λμΌν νμ μ μ§)
- 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")