1. Array
์์๊ฐ ์๋ ์ปฌ๋ ์
์ ์ธํ๋ ๋ฐฉ๋ฒ
var intArray : Array<Int> = Array<Int>()
var intArray : Array<Int> = []
var intArray : [Int] = Array<Int>()
var intArray : [Int] = []
var intArray = [Int]()
ํ์ฉ
var intArray : Array<Int> = []
intArray.append(1) // 1์ ์ฝ์
ํ๋ค.
intArray.contains(1) // 1์ด ์กด์ฌํ๋์ง ํ์ธ => true
intArray.remove(at:0) // 0๋ฒ์งธ index ๊ฐ ์ ๊ฑฐ
intArray.removeLast()
intArray.removeAll()
intArray.count // ๊ธธ์ด ๋ฐํ
intArray[0] // 0๋ฒ์งธ index ์ ๊ทผ
2. Dictionary
key์ value์ ์์ผ๋ก ์ด๋ฃจ์ด์ง ์ปฌ๋ ์
์ ์ธํ๋ ๋ฐฉ๋ฒ
// key : String , value : Int ์ธ ๋์
๋๋ฆฌ
var anyDictionary : Dictionary<String, Int> = Dictionary<String, Int>()
var anyDictionary : Dictionary<String, Int> = [String: Int]()
var anyDictionary : Dictionary<String, Int> = [:]
var anyDictionary : [String: Int] = [:]
var anyDictionary = [String: Int]()
ํ์ฉ
var anyDictionary : Dictionary<String, Int> = [:]
//์ฝ์
anyDictionary["KeyA"] = 10 // ["KeyA" : 10]
anyDictionary["KeyB"] = 1 // ["KeyA" : 10 , "KeyB" : 1]
//๋ณ๊ฒฝ (์ด๋ฏธ ์๋ key์ value๋ฅผ ๋ณ๊ฒฝ)
anyDictionary["KeyA"] = 100 // ["KeyA" : 100 , "KeyB" : 1]
//์ญ์
anyDictionary.removeValue(forKey: "KeyA") // ["KeyB" : 1]
anyDictionary["KeyB"] = nil // [] ๐
3. Set
์์๊ฐ ์๊ณ , ์ค๋ณต์ด ์๋ ์ปฌ๋ ์
์ ์ธํ๋ ๋ฐฉ๋ฒ
var intSet : Set<Int> = Set<Int>()
ํ์ฉ
var intSet : Set<Int> = Set<Int>()
intSet.insert(1) // 1์ ์ฝ์
{1}
intSet.insert(1) // ์ค๋ณต X {1}
intSet.contains(1) // 1์ด ์๋๊ฐ -> true
intSet.remove(1) // 1์ ์ญ์
intSet.count // ๊ธธ์ด๋ฅผ ๋ฐํ
ํ์ฉ2 (์งํฉ์ ์ฐ์ฐ)
let SetA : Set<Int> = [1,2,3,4,5]
let SetB : Set<Int> = [3,4,5,6,7]
//ํฉ์งํฉ
let union : Set<Int> = SetA.union(SetB) // {1,2,6,4,3,7,5}
//์ค๋ฆ์ฐจ์ ์ ๋ ฌ -> array
let union_sort : Array<Int> = union.sorted() // [1,2,3,4,5,6,7]
//๊ต์งํฉ
let intersection : Set<Int> = SetA.intersection(SetB) // {3,4,5}
//์ฐจ์งํฉ (A-B)
let subtracting : Set<Int> = SetA.subtracting(SetB) // {1,2}
'๐ Programming > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ์ ์ด๋ฌธ (์กฐ๊ฑด๋ฌธ / ๋ฐ๋ณต๋ฌธ) (0) | 2022.01.18 |
---|---|
[Swift] function / ํจ์ (0) | 2022.01.15 |
[Swift] Optional๊ณผ unwrapping (0) | 2021.08.15 |
[Swift] ๋ณ์์ ์์ ๊ทธ๋ฆฌ๊ณ type (0) | 2021.08.15 |
[Swift] Xcode๋ก ์์ํ๊ธฐ(feat.MacBook) (0) | 2021.08.15 |