๋ฐ์ํ
1. Array
๋ฐฐ์ด์ ์ ์
- swift์์ ๊ฐ์ฅ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ์๋ฃ๊ตฌ์กฐ ์ค ํ๋
- ๋์ผํ ์ ํ์ ๊ฐ์ ์์๊ฐ ์๋ ์ฝ๋ ์ ํ์
- ๊ฐ ์์๋ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํด ์ ๊ทผ ๊ฐ๋ฅ
๋ฐฐ์ด์ ์ ์ธ๊ณผ ์ด๊ธฐํ
- ๋๊ดํธ([]) ๊ธฐํธ๋ฅผ ์ฌ์ฉํ์ฌ ๋น ๋ฐฐ์ด์ ์ ์ธ
var numbers: [Int] = []
var numbers = [Int]()
- ๋๊ดํธ([]) ์์ ๊ฐ์ ์ผํ๋ก ๊ตฌ๋ถํ์ฌ ๋์ด
var numbers = [1, 2, 3, 4, 5]
- ๋ฐฐ์ด์ ์ด๊ธฐํ ํ ๋, ํ์ ์ด๋ ธํ ์ด์ (type annotation)์ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ํ์ ์ง์ ๊ฐ๋ฅ
var numbers: [Int] = [1, 2, 3, 4, 5]
- Swift๋ ํ์
์ถ๋ก (type inference)์ ์ง์ํ๊ธฐ ๋๋ฌธ์, ํ์
์ ์๋ตํ ์ ์์
- ์ด๊ธฐ๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ๋ฐฐ์ด์ ํ์ ์ ์ถ๋ก
var numbers = [1, 2, 3, 4, 5] // numbers์ ํ์
์ [Int]๋ก ์ถ๋ก ๋ฉ๋๋ค.
- ๋ฐฐ์ด์ ๋ค๋ฅธ ํ์
์ ์์๋ค์ ์ ์ฅํ๋ ํผํฉ๋ฐฐ์ด ๋๋ Any ๋ฐฐ์ด์ ์์ฑ ๊ฐ๋ฅ
- ๊ฐ๋ฅํ ๋ชจ๋ ์์๊ฐ ๋์ผํ ์ ํ์ ๋ฐฐ์ด์ ์ฌ์ฉํ๋๊ฒ์ด ์ข์
var mixedArray = [1, "two", 3.0]
๋ฐฐ์ด ์์ ์ ๊ทผ
- ์ธ๋ฑ์ค(index)๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ์์์ ์ ๊ทผ
- ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ 0๋ถํฐ ์์
var numbers = [1, 2, 3, 4, 5]
let firstNumber = numbers[0] // 1 let secondNumber =numbers[1] // 2
- ๋ฐฐ์ด์ ๋ง์ง๋ง ์์์ ์ ๊ทผํ๋ ค๋ฉด count ์์ฑ์ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ๊ธธ์ด๋ฅผ ๊ฐ์ ธ์จ ํ, ์ธ๋ฑ์ค๋ count-1๋ก ์ค์
let lastNumber = numbers[numbers.count-1]
let n = [1, 2, 3]
print(n[1..<2]) //2
print(n[1...2]) //2, 3
- ํน์ ์ธ๋ฑ์ค๋ฅผ ์ฐพ๊ธฐ ์ํด์๋ firstIndex(of: ) ๋ฉ์๋๋ฅผ ์ฌ์ฉ
- ๋ฐฐ์ด์ ๋์ผํ ์์๊ฐ ์๋ ๊ฒฝ์ฐ, ๋ ์์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ
- optional ๊ฐ์ผ๋ก ๋ฐํ
var arr = [1,2,3,4,5]
arr.firstIndex(of: 1) //Optional(0)
๋ฐฐ์ด์ ์์ ์ถ๊ฐ
- ์๋ก์ด ์์๋ฅผ ๋ฐฐ์ด์ ์ถ๊ฐํ๊ธฐ ์ํด์๋, append() ๋ฉ์๋๋ฅผ ์ฌ์ฉ
var numbers = [1, 2, 3, 4, 5]
numbers.append(6) // 6์ ๋ฐฐ์ด์ ๋ง์ง๋ง ์์๋ก ์ถ๊ฐํฉ๋๋ค.
- ํน์ ์์น์ ์์๋ฅผ ์ถ๊ฐํ๊ธฐ ์ํด์๋, insert() ๋ฉ์๋๋ฅผ ์ฌ์ฉ
- insert๋ฅผ ํ๋ ์์น๋ถํฐ ๋ฐฐ์ด์ ์ฌ๋ฐฐ์น ํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ค๋ฒํค๋๊ฐ ๋ฐ์
- ๊ฐ๋ฅํ append ์ฌ์ฉ
var arr = [1, 2, 3, 4, 5]
arr.insert(10, at: 3) //10์ ๋ฐฐ์ด์ 3๋ฒ์งธ์ ์ถ๊ฐํฉ๋๋ค.
print(arr) //[1, 2, 3, 10, 4, 5]
๋ฐฐ์ด์ ์์
- ๋ฐฐ์ด์ ์์๋ฅผ ์์ ํ๊ธฐ ์ํด์๋ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ์์์ ์ ๊ทผํ ํ, ๊ฐ์ ๋ณ๊ฒฝ
var numbers = [1, 2, 3, 4, 5]
numbers[2] = 30 // ์ธ ๋ฒ์งธ ์์๋ฅผ 30์ผ๋ก ๋ณ๊ฒฝํฉ๋๋ค.ใฑใท๋ฐฐ
- ๋ฐฐ์ด์ ํน์ ๋ฒ์์ ๊ฐ์ ์์ ํ๊ณ ์ถ์ ๋ replaceSubrange ๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ๋ณ๊ฒฝ ๊ฐ๋ฅ
var array2 = [1, 2, 3]
array2.replaceSubrange(0...2, with: [10, 20, 30]) // [10, 20, 30]
array2.replaceSubrange(0...2, with: [0]) // [0]
array2.replaceSubrange(0..<1, with: []) // []
๋ฐฐ์ด์ ์ญ์
- ๋ฐฐ์ด์ ์์๋ฅผ ์ญ์ ํ๊ธฐ ์ํด์๋, remove() ๋ฉ์๋๋ฅผ ์ฌ์ฉ
var numbers = [1, 2, 3, 4, 5]
numbers.remove(at: 2) // ์ธ ๋ฒ์งธ ์์๋ฅผ ์ญ์ ํฉ๋๋ค.
numbers.removeFirst() //๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ์ญ์ ํฉ๋๋ค.
numbers.removeLast() //๋ฐฐ์ด์ ๋ง์ง๋ง ์์๋ฅผ ์ญ์ ํฉ๋๋ค.
numbers.removeAll() //๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ์ญ์ ํฉ๋๋ค.
ํจ์ ์ด๋ฆ | ์ฉ๋ | ๋ฆฌํด ํ์ |
remove(at:) | ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ index์ ํด๋นํ๋ ๊ฐ ์ญ์ | ์ญ์ ๋ ๊ฐ ๋ฆฌํดNon-Optional Type |
removeFirst | ์ฒซ ๋ฒ์งธ ์์ ์ญ์ | ์ญ์ ๋ ๊ฐ ๋ฆฌํดNon-Optional Type |
removeFirst(_:) | ์ฒซ ๋ฒ์งธ ์์๋ถํฐ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ ๊ฐฏ์ ๋งํผ ์ญ์ | X |
removeLast | ๋ง์ง๋ง ์์ ์ญ์ | ์ญ์ ๋ ๊ฐ ๋ฆฌํดNon-Optional Type |
popLast | ๋ง์ง๋ง ์์ ์ญ์ | ์ญ์ ๋ ๊ฐ ๋ฆฌํดNon-Optional Type |
removeLast(_:) | ๋ง์ง๋ง ์์๋ถํฐ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ ๊ฐฏ์ ๋งํผ ์ญ์ | X |
removeAll | ์ ์ฒด ์์ ์ญ์ | X |
removeSubrange(_:) | ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ ๋ฒ์๋งํผ index ์์ ์ญ์ | X |
[n...m] = [] |
Subscript ๋ฌธ๋ฒ์ผ๋ก n ~ m๊น์ง index ์์ ์ญ์ | X |
๋ฐฐ์ด์ ๊ธฐํ ๊ธฐ๋ฅ
max() | ๋ฐฐ์ด์ ์ต๋๊ฐ ๋ฐํ |
min() | ๋ฐฐ์ด์ ์ต์๊ฐ ๋ฐํ |
sort() | ๋ฐฐ์ด์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ |
sort(by: >) | ๋ฐฐ์ด์ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ |
isEmpty | ๋ฐฐ์ด์ด ๋น์ด์๋์ง ํ์ธ |
elementsEqual(arr) | ๋ค๋ฅธ ๋ฐฐ์ด๊ณผ ๊ฐ์ ๋ฐฐ์ด์ธ์ง ๋น๊ต |
contains(n) | ๋ฐฐ์ด์ n์ด ์กด์ฌํ๋์ง ํ์ธ |
swapAt(a, b) | ์ธ๋ฑ์ค a์ ์์์ ์ธ๋ฑ์ค b์ ์์๋ฅผ ๋ณ๊ฒฝ |
2. Dictionary
Dictionary์ ์ ์
- ๋์ ๋๋ฆฌ๋ ํค(Key) ์ ๊ฐ(Value)์ ์์ผ๋ก ๊ตฌ์ฑ๋๋ ์์๊ฐ ์๋ ์๋ฃ๊ตฌ์กฐ
- ๋์
๋๋ฆฌ์์ key๋ value๋ฅผ ๋๋ณํ๋ ์ ์ผํ ์๋ณ์๊ฐ ๋จ
- ๋์ ๋๋ฆฌ ์์๋ ๊ฐ์ ์ด๋ฆ์ ๊ฐ์ง key๊ฐ ์กด์ฌํ์ง ์์(์ ์ผ์ฑ)
- ๋์ ๋๋ฆฌ ์์์ value๋ ์ ์ผํ์ง ์์
- ๊ฐ value๋ key๋ก ์ ๊ทผ์ด ๊ฐ๋ฅ
- ๋์ ๋๋ฆฌ ๋ด๋ถ์ ์กด์ฌํ์ง ์๋ key๋ก ์ ๊ทผํ๊ฒ ๋๋ฉด nil ๊ฐ์ ๋ฐํ
Dictionary์ ์ ์ธ
- ๋๊ดํธ([]) ์์ key์ value์ ์๋ค์ ๋ฃ์ด ์ด๊ธฐํ
/*
var [๋์
๋๋ฆฌ ์ด๋ฆ]: Dictionary<ํค ํ์
, ๊ฐ ํ์
> = Dictionary<ํค ํ์
, ๊ฐ ํ์
>()
*/
// Dictionary ์ ์ธ
var anyDictionary: Dictionary<String, Int> = [String: Int]()
// ๋์ผ ํํ - ๋ฆฌํฐ๋ด ์ด์ฉ
var anyDictionary: Dictionary<String, Any> = Dictionary<String, Any>()
var anyDictionary: Dictionary<String, Any> = [:]
var anyDictionary: [String: Any] = Dictionary<String, Any>()
var anyDictionary: [String: Any] = [String: Any]()
// ๋์ผ ํํ - ๋น ๋์
๋๋ฆฌ ์์ฑ
var anyDictionary: [String: Any] = [:]
var anyDictionary = [String: Any]()
// ์ด๊ธฐํ
var anyDictionary: [String: Int] = ["extramilejin": 28, "korea": 2021]
Dictionary์ value ์ค์
- ๋์ ๋๋ฆฌ์ value์ ์ ๊ทผํ๋ ๊ฐ์ฅ ํํ ๋ฐฉ๋ฒ์ key๋ฅผ subscript๋ก์จ ์ฌ์ฉ
- ๋์
๋๋ฆฌ์์ key์ ํด๋นํ๋ ๊ฐ์ด ์์ ์ ์๊ธฐ ๋๋ฌธ์, ๋ฐํ ๊ฐ์ optional ์ธ์คํด์ค
- subscript
var responseMessages = [200: "OK",
403: "Access forbidden",
404: "File not found",
500: "Internal server error"]
print(responseMessages[200])
// Optional("OK")
- if let ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ ๋์ ๋๋ฆฌ์์ key์ ํด๋นํ๋ ๊ฐ์ ์ ๋ฌด์ ๋ฐ๋ผ ๋ค๋ฅธ ๋์์ ์ํ
let httpResponseCodes = [200, 403, 301]
for code in httpResponseCodes {
//key์ ํด๋นํ๋ ๊ฐ ์กด์ฌ
if let message = responseMessages[code] {
print("Response \\(code): \\(message)")
}
//key์ ํด๋นํ๋ ๊ฐ ์์
else {
print("Unknown response \\(code)")
}
}
// Prints "Response 200: OK"
// Prints "Response 403: Access forbidden"
// Prints "Unknown response 301"
Dictionary ์ถ๊ฐ, ์ญ์ , ์์
- subscripting์ ์ฌ์ฉํ์ฌ key์ value๋ฅผ ์ถ๊ฐ, ์ญ์ , ์์ ์ ํ ์ ์์
- ์๋ก ์ถ๊ฐํ๋ key-value pair์ dictionary์ ์กด์ฌํ์ง ์๋ key-value๋ก ์ถ๊ฐํด์ผํจ (ํค๋ ์ ์ผ์ฑ ๋ณด์ฅ)
- key-value pair ์ญ์ ๋ ์ญ์ ํ๊ณ ์ํ๋ key์ ํด๋นํ๋ value๋ฅผ nil๋ก ์์ ํ๋ฉด ์๊ตฌ์ ์ผ๋ก ์ญ์ ๋จ
// add key-value pair
responseMessages[301] = "Moved permaently"
print(responseMessages[301])
// Prints Optional("Moved permaently")
// update key-value pair
responseMessages[301] = "Deleted permanently"
print(responseMessages[301])
// Prints Optional("Deleted permanently")
// remove key-value pair
responseMessages[500] = nil
print(responseMessages)
// Prints [200: "OK", 301: "Deleted permanently", 403: "Access forbidden", 404: "File not found"]
responseMessages.removeValue(forKey: 404)
responseMessages.removeAll()
Dictionary์ ํ๋กํผํฐ์ ๋ฉ์๋
- isEmpty: ๋์ ๋๋ฆฌ๊ฐ ๋น์ด์๋ค๋ฉด true, ๋น์ด์์ง ์๋ค๋ฉด false ๋ฐํ
- count : ๋์ ๋๋ฆฌ์ ์์์ ๊ฐ์๋ฅผ ๋ฐํ
- removeValue(forKey: ) : key์ ํด๋นํ๋ value๋ฅผ ์ฐพ์ ์ ๊ฑฐํ๊ณ ๊ทธ ๊ฐ์ ๋ฐํ
- ๋ง์ฝ ๋์ ๋๋ฆฌ ์์ key์ ํด๋นํ๋ value๊ฐ ์๋ค๋ฉด nil์ ๋ฐํ
- ์ด ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์ ๋๋ ค์ฃผ๋ ๊ฒ๋ ๊ฐ๋ฅ
- keys: ๋์ ๋๋ฆฌ ์์ ์๋ ๋ชจ๋ key๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํ
- values: ๋์ ๋๋ฆฌ ์์ ์๋ ๋ชจ๋ value๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํ
- contains(where:): ๋์ ๋๋ฆฌ์ ์์๊ฐ ์กด์ฌํ๋ฉด true, ์๋๋ผ๋ฉด false ๋ฐํ
- firstIndex(where:): ๋์ ๋๋ฆฌ์ ํด๋น ์์์ ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค๋ฅผ optional๋ก ๋ฐํ
- ๋์ ๋๋ฆฌ์ด๋ฆ [ ํค ] = ๊ฐ : ๋์ ๋๋ฆฌ์ ํค ์ ๊ฐ์ ์์ ์ถ๊ฐ
- ๋์ ๋๋ฆฌ์ด๋ฆ [ ํค ] = nil : ๋์ ๋๋ฆฌ์์ ํค์ ํด๋นํ๋ ๊ฐ์ nil๋ก ํ ๋นํ์ฌ ์ ๊ฑฐ
var intDictionary: [String: Int] = ["extramilejin": 28, "korea": 2021]
print(intDictionary.isEmpty) // false
print(intDictionary.count) // 2
print(intDictionary.removeValue(forKey: "korea")) // Optional(2021)
print(intDictionary.count) // 1
print(intDictionary.removeValue(forKey: "korea")) // nil: ๊ฐ์ด ์์์ ๋ปํ๋ค.
print(intDictionary["korea", default: 0]) // 0: ๊ฐ์ด ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ 0
let countryCodes = ["BR": "Brazil", "GH": "Ghana", "JP": "Japan"]
print(countryCodes.keys) //["GH", "JP", "BR"]
print(countryCodes.values) //["Ghana", "Japan", "Brazil"]
print(countryCodes.contains(where: {$0.value == "Brazil"})) //true
Dictionary์ ๊ธฐ๋ณธ ๊ฐ
- ์กด์ฌํ์ง ์๋ key๋ฅผ ์ฌ์ฉํ์ฌ ๋์ ๋๋ฆฌ์์ value๋ฅผ ์ฝ์ผ๋ ค๊ณ ํ๋ฉด nil์ ๋ฐํ
- ์กด์ฌํ์ง ์๋ key์ ๋ํ value๋ฅผ ์์ฒญํ๋ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์ ๋ฏธ๋ฆฌ ํ ๋นํ ์ ์์
- ๋์ ๋๋ฆฌ์์ value์ ์ฆ๊ฐ ์ฐ์ฐ(+= 1, -= 1 … )์ ํ๋ ๊ฒฝ์ฐ ์ฌ์ฉ
let favoriteIceCream = [ "์๊ทผ": "์ด์ฝ๋ฆฟ", "ํฌ๋จ": "๋ฐ๋๋ผ" ]
favoriteIceCream["์๊ทผ"]
favoriteIceCream["ํ์ธ"] // nil์ด ๋ฐํ๋จ -> ์ด๋ ํด๋น ํค์ ๋ํ ๊ฐ์ด ์์์ ์๋ฏธํจ
//๋์
๋๋ฆฌ์ "Unknown"์ **๊ธฐ๋ณธ๊ฐ**์ ์ง์ ํ์ฌ์ด nil์ด ๋ฐํ๋๋ ๊ฒ์ ๋ฐฉ์ง
favoriteIceCream["ํ์ธ", default: "Unknown"]
Dictionary์ ๋ฐ๋ณต
- ๋์ ๋๋ฆฌ ๋ด๋ถ๋ฅผ ์ํํ๊ธฐ ์ํด, for-in loop๋ฅผ ์ฌ์ฉํด, key-value ์์ ์ป์ด ์ํ
- key-value์์ ํํ ํํ๋ก ๋ฐํ
let imagePaths = ["star": "/glyphs/star.png",
"portrait": "/images/content/portrait.jpg",
"spacer": "/images/shared/spacer.gif"]
for (name, path) in imagePaths{
print("The path to '\\(name)' is '\\(path)'.")
}
// The path to 'spacer' is '/images/shared/spacer.gif'.
// The path to 'star' is '/glyphs/star.png'.
// The path to 'portrait' is '/images/content/portrait.jpg'.
3. enum
enum์ ์ ์
- ์ด๊ฑฐํ์ ๊ด๋ จ๋ ๊ฐ ๋ค์ ๊ทธ๋ฃน์ ์ ์ํ ์ ์๋๋ก ํด์ฃผ๋ ๋ฐ์ดํฐ ํ์
- ์ฝ๋์ ๊ฐ๋ ์ฑ๊ณผ ์์ ์ฑ์ ๋์ฌ์ค
- ๊ฐ๊ธฐ ๋ค๋ฅธ ์ํ์ ๊ฐ๊ธฐ ๋ค๋ฅธ ํ์ ์ ์ ๋ ฅ๊ฐ์ ๋ํ๋ด๊ธฐ ์ํด ์ฌ์ฉ
enum Direction {
case north
case south
case east
case west
}
- ๊ฐ case์ ๋ํ ์ถ๊ฐ์ ์ธ ์ ๋ณด ๋ถ์ฌ ๊ฐ๋ฅ
enum HTTPResponse {
case success(Int)
case error(Int, String)
}
enum์ ์ญํ ๋ฐ ํ์์ฑ
- ๊ฐ๋
์ฑ
- ๊ด๋ จ ๊ฐ์ ์๋ฏธ ์๋ ์ด๋ฆ์ ์ง์ ํ ์ ์์ผ๋ฏ๋ก ์ฝ๋๋ฅผ ๋ ์ฝ๊ธฐ ์ฝ๊ณ ์ฝ๊ฒ ์ดํดํ ์ ์์
- ์์ ์ฑ
- ์๋ก ๋ค๋ฅธ ํ์ ์ ๊ฐ์ด ์๋ชป ๋ณ๊ฒฝ๋์ง ์๋๋กํจ์ผ๋ก ํ ์์ ์ฑ(type-safety)์ ์ ๊ณตํจ
- ex- Monday ํ์ ์ ๊ฐ์ด String ํ์ ์ด ์์๋๋ ๊ณณ์ ์๋ชป ์ฌ์ฉ๋์ง ์๋๋ก ํ ์ ์์
- ๋จ์ํ
- ์ด๊ฑฐํ์ ์ฌ์ฉํ๋ฉด ์์ฑํด์ผ ํ๋ ์ฝ๋์ ์์ ์ค์ด๊ณ ์ฝ๋๋ฅผ ๋ณด๋ค ๊ฐ๊ฒฐํ๊ฒ ๋ง๋ค ์ ์์
- ์ฌ๋ฌ ๊ฐ์ ์์ ๋๋ ๋ณ์ ์ ์ธ์ ๋จ์ผ ์ด๊ฑฐํ ์ ์ธ์ผ๋ก ๋์ฒด ํ ์ ์์
- ์กฐ์ง
- ๊ด๋ จ ๊ฐ๋ค์ ์ด๊ฑฐํ์ผ๋ก ๊ทธ๋ฃนํํ๋ฉด ์ฝ๋๋ฅผ ๋ณด๋ค ํจ์จ์ ์ผ๋ก ๊ตฌ์ฑํ ์ ์๊ณ ์ ์ง ๊ด๋ฆฌ๋ ์ฝ๊ฒ ํ ์ ์์
- ๊ฐ ๋งค์นญ
- Swift์ ์ค์์น ๊ตฌ๋ฌธ์ ์ด๊ฑฐํ๊ณผ์ ๊ฐ ๋งค์นญ์ ์ง์ํ๋ฏ๋ก ํน์ ๊ฐ์ ๋ค์ํ ์ผ์ด์ค๋ฅผ ์ฝ๊ฒ ์ฒ๋ฆฌํ ์ ์์
- ๊ฐ๋ฅํ ๋ง์ ์์ ์ฌ๋ก๋ฅผ ์ฒ๋ฆฌํด์ผ ํ ๋ ํนํ ์ ์ฉํ ์ ์์
enum HTTPResponse {
case success(Int)
case error(Int, String)
}
let response = HTTPResponse.success(200)
switch response {
case .success(let statusCode):
print("Success with status code \\(statusCode)")
case .error(let statusCode, let message):
print("Error with status code \\(statusCode) and message \\(message)")
}
enum์ ์ฌ์ฉ๋ฒ
enum Direction {
case north
case south
case east
case west
}
// To use an enum value, you simply reference its name:
let direction = Direction.north
switch direction {
case .north:
print("Heading north")
case .south:
print("Heading south")
case .east:
print("Heading east")
case .west:
print("Heading west")
}
- enum ํค์๋๋ฅผ ์ฌ์ฉํด ์ด๊ฑฐํ ์ ์
- ์ฌ๋ฌ ์ผ์ด์ค๋ฅผ ์ฝค๋ง(,)๋ก ๊ตฌ๋ถํ์ฌ ํ ์ค์ ์ ์ ์ ์์
- ์์ ์๋ก์ด ํ ์ ์ํด์ผ ํจ
- Planet
- ๋๋ฌธ์๋ก ์์ํด์ผ ํจ
enum Planet { case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune }
- ํ์ด ์ด๋ฏธ ํ ๋ฒ ์ ์๋ ๊ฒฝ์ฐ, ๋ค์์ ๊ฐ์ ํ ๋นํ ๋์๋ ํ์ ์๋ตํ ์ ๋ฌธ๋ฒ(dot syntax)์ ์ด์ฉํด ๊ฐ์ ํ ๋นํ ์ ์์
var whereToGo = Planet.earth
whereToGo = .mercury
4. Set
Set์ ์ ์
- ๊ฐ์ ๋ฐ์ดํฐ ํ์ ์ ๊ฐ์ ์์ ์์ด ์ ์ฅํ๋ ๋ฆฌ์คํธ
- ์ค๋ณต์ ํ์ฉํ์ง ์์
var setName: Set = Set<Int>()
Set์ ์ฌ์ฉ
count | set์ ํญ๋ชฉ์ ๊ฐ์ ๋ฐํ |
isEmpty | set์ด ๋น์ด์๋์ง ํ์ธ |
insert(value) | set์ ๊ฐ ์ถ๊ฐ |
remove(value) | set์์ ๊ฐ ์ญ์ |
contains(value) | set์ ๊ฐ์ด ์กด์ฌํ๋์ง ํ์ธ |
intersection(b) | set b์ ๊ฐ์ ์์๋ค๋ง ๋ฐํ, ๊ต์งํฉ |
union(b) | set b์ ์์์ set์ ์์ ๋ชจ๋ ๋ถ๋ถ ๋ฐํ, ํฉ์งํฉ |
symmetricDifference(b) | set b์ ์์์ set์ ์์์์ ๊ฐ์ ์์๋ง ๋นผ๊ณ ๋ฐํ |
subtracting(b) | set์ ์์์์ set b์ ๊ฐ์ ์์๋ง ๋นผ๊ณ ๋ฐํ |
๋ฐ์ํ
'โจ๏ธ Language > swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ์์ธ ์ฒ๋ฆฌ์ ํจ์ (4) | 2024.03.17 |
---|---|
[Swift] ์กฐ๊ฑด/๋ฐ๋ณต๋ฌธ (1) | 2024.03.17 |
[Swift] ๋ณ์์ ํ๋กํผํฐ2 (1) | 2024.03.15 |
[Swift] ๋ณ์์ ํ๋กํผํฐ1 (0) | 2024.03.15 |
[์ฝ๋ํธ๋ฆฌ ์ฑ๋ฆฐ์ง] 6์ฃผ์ฐจ - ๊ทธ๋ํ ํ์ (0) | 2023.10.16 |