๐ก ์ค์ํํธ์์ ์ ๊ณตํ๋ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๊ธฐ ์ํ ์๋ฃํ(ํ์ )์ด๋ค.
์ค์ํํธ์์ ์ปฌ๋ ์ ์๋ฃํ์ ์ด ์ธ๊ฐ์ง๊ฐ ์๋ค.
- Array(๋ฐฐ์ด): ๋ฐ์ดํฐ๋ฅผ ์์๋๋ก ์ ์ฅํ๋ ์ปฌ๋ ์
- Dictionary(์ฌ์ ): ๋ฐ์ดํฐ๋ฅผ ํค์ ๊ฐ์ผ๋ก ์ด๋ฃจ์ด์ง ํ๋์ ์์ผ๋ก ๋ง๋ค์ด ๊ด๋ฆฌํ๋ฉฐ ์์๊ฐ ์๋ ์ปฌ๋ ์
- Set(์งํฉ): ์ํ์์ ์งํฉ๊ณผ ๋น์ทํ ์ฐ์ฐ์ ์ ๊ณตํ๋ ์์๊ฐ ์๋ ์ปฌ๋ ์
- ํ๋์ ๋ฐฐ์ด์๋ ๋์ผํ ํ์ ์ ๋ฐ์ดํฐ๋ง ๋ด์ ์ ์๋ค.
- ์์๊ฐ ์๊ธฐ ๋๋ฌธ์ ๊ฐ์ ์ค๋ณต์ด ๊ฐ๋ฅํ๋ค.
var numsArray = [1, 2, 3, 4, 5]
// ์ ์
let strArray1: Array<String> = []
// ๋จ์ถ
let strArray2: [String] = []๋น ๋ฐฐ์ด์ผ ๊ฒฝ์ฐ ํ์ ์ถ๋ก ์ด ๋ถ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ ๊ผญ ํด๋น ๋ณ์๋ ์์๊ฐ ๋ฐฐ์ด์์ ๋ช ์ํ์!
- ๋ฐฐ์ด์ ๊ธฐ๋ณธ ๋ฉ์๋
numsArray.count // 5
numsArray.isEmpty // false
numsArray.contains(1) // true
numsArray.randomelement()
numsArray.swapAt(0, 1) // 0๊ณผ 1 ์ธ๋ฑ์ค์ ํด๋นํ๋ ์์๋ฅผ ์๋ก ๋ฐ๊ฟ
numsArray.last // Optional(5)
numsArray.first // Optional(1)
numsArray.endIndex // 4
numsArray.index(1, offsetBy: 2) // 4, index 1๋ก๋ถํฐ 2๋งํผ ๋จ์ด์ง ์์ ์ถ๋ ฅ
numsArray.firstIndex(of: 3) // 3, ์์์ ๋ถํฐ ํ์๋ ํด๋น ์์๊ฐ ๋ช๋ฒ์งธ์ธ์ง
numsArray.insert(1, at: 1) // [1, 1, 2, 3, 4, 5]
numsArray.insert(contentsOf: [1, 2, 3], at: 0) // [1, 2, 3, 1, 2, 3, 4, 5]- ๋ฐ์ดํฐ๋ฅผ ํค์ ๊ฐ์ผ๋ก ํ๋์ ์์ผ๋ก ๋ง๋ค์ด ๊ด๋ฆฌํ๋ ์ปฌ๋ ์ ์ด๋ค.
key์ ์ ์ผํด์ผํ๋ฉฐvalue๋ ์ค๋ณต์ด ๊ฐ๋ฅํ๋ค.key๊ฐ์ Hashable ํด์ผํ๋ค.- ์์๊ฐ ์๋ค.
๐ก Hashable
์ด๋ค ํ์ ์ด Hashable์ด๋ผ๋ ๋ป์ ํด๋น ํ์ ์ ํด์ํจ์์ input๊ฐ์ผ๋ก ์ฌ์ฉ๊ฐ๋ฅํ๋ค๋ ๋ป์ด๋ค. Hashable์ด ๋๋ฉด ๊ฐ์ ์ ์ผ์ฑ์ ๋ณด์ฅํ๋ฉฐ ๊ฒ์ ์๋๊ฐ ๋น ๋ฅด๋ค.
var dict = ["A": "Appple", "B": "Banana", "C": "Candy"]
// ๋จ์ถ ๋ฌธ๋ฒ
var words: [String: String] = [:]
// ์ ์ ๋ฌธ๋ฒ
let words: Dictionary<Int, String>- ๋ฐ๋ณต๋ฌธ๊ณผ์ ๊ฒฐํฉ
let dict = ["A": "Appple", "B": "Banana", "C": "Candy"]
for (key, value) in dict {
print("\(key): \(value)")
}
for (_, value) in dict {
print("\(key): \(value)")
}
for (key, _) in dict {
print("\(key): \(value)")
}key๋ value ์ค ํ๋๋ง ์ฌ์ฉ์์๋ ์ฌ์ฉํ์ง ์๋ ๊ฒ์ ์์ผ๋์นด๋ ํจํด์ผ๋ก ๋์ฒดํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
์ฐธ๊ณ ) KeyValuePairs
ํค๊ฐ ์๊ณ ์์๊ฐ ์๋ ์ปฌ๋ ์ ์ด๋ค.
let pairs: KeyValuePairs = ["A": "Appple", "B": "Banana", "C": "Candy"]- ์ํ์์์ ์งํฉ๊ณผ ๋น์ทํ ์ฐ์ฐ์ ์ ๊ณตํ๋ ์์๊ฐ ์๋ ์ปฌ๋ ์ ์ด๋ค.
- ๊ฐ ์์๋ Hashable ํด์ผํ๋ค.
- ์์๊ฐ ์๋ค.
var set: Set = [1, 2, 3, 4, 5]
var set: Set<Int> = [1, 2, 3, 4, 5]NSArray,NSDictionary,NSSet๋ฑ Objective-C ์ปฌ๋ ์ ์ด๋ค.- ๊ตณ์ด ๊ณต๋ถํ ํ์ ์๊ณ ํ์ํ ๋๋ง๋ค ๊ฒ์ํด์ ์ฐ๋ฉด๋๋ค.