Swift: add, adds, append, appends 속도 비교
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]을 배열에 더하는 연산을 다중 add, adds, 다중 append, appends로 각각 수행할 때의 속도 비교다중 add를 제외하면 평이하며, 다중 add의 경우 n이 100000000 일 때, 다른 API에 비해 3배의 속도 차이가 발생 import SwiftUIfunc measure(label: String, repeatCount: Int, function: () -> ()) { let time = ContinuousClock().measure { for _ in 1...repeatCount { function() } } print(label, ":", time)}struct ContentView: View { @State ..
2025. 3. 30.
Swift: async let 수행 순서
async let의 수행 순서는 await 순서에 따름func function(_ name: String, _ i: Int) async -> Int { print("sleep: ", name, i) sleep(UInt32(i)) print("wake up: ", name, i) return i}async let a = function("a", 10)async let b = function("b", 3)async let c = function("c", 2)async let d = function("d", 5)async let e = function("e", 3)async let f = function("f", 5)async let g = function("g", 3)async let ..
2025. 3. 30.
Swift 알고리즘 관련 문법
조건 splitlet str = "abd124kbksbjb62".split { !$0.isNumber }print(str) // ["124", "62"] dict 필터let s = [1, 2, 2, 3, 3, 3, 4, 4, 4]let dict = Dictionary(grouping: Array(s).map(String.init), by: { $0 }).filter { $0.value.count > 1 }print(dict) // ["3": ["3", "3", "3"], "2": ["2", "2"], "4": ["4", "4", "4"]] 여러 조건 splitlet str = "nabcvcvacbacac".split { $0 == "a" || $0 == "b" || $0 == "c" }.map { Str..
2025. 3. 30.
알쓸스잡 - 6
stridestride(from: , to: , by:)열린 범위 -> 0...5stride(from: , through: , by:)닫힌 범위 -> 0.. switch 연속 케이스fallthrough를 사용하면 C 처럼 케이스 이후 다음 케이스로 넘어감let integerToDescribe = 5var description = "The number \(integerToDescribe) is"switch integerToDescribe {case 2, 3, 5, 7, 11, 13, 17, 19: description += " a prime number, and also" fallthroughdefault: description += " an integer."}print(descriptio..
2025. 1. 21.
알쓸스잡 - 2
Swift에서 제공하는 데이터 타입기본 데이터 타입: Int, Double, Bool, ...컬렉션 타입: Array, Set, Dictionary 플랫폼에 따른 Int플랫폼에 따라 Int의 크기가 달라짐32 bit: Int -> Int3264 bit: Int -> Int64UInt 또한 동일 Double, Float 크기Double: 64 bit 부동 소수점, 15자리의 소수점 정확도Float: 32 bit 부동 소수점, 6자리의 소수점 정확도 타입 세이프티, 타입 추론 시점타입 세이프티, 타입 추론은 컴파일 단계에서 검사 숫자 리터럴0b 접두사: 2진수0o 접두사: 8진수0x 접두사: 16진수 아래는 모두 10진수 17let decimalInteger = 17let binaryInteger = 0b1..
2024. 12. 26.