본문 바로가기

# 02/Swift

[Swift] 기초 입문 Study 몇가지 스위프트 언어의 특징 (공식 소개 링크) Modern 최근 개발언어에 대해 연구해서 좋은것만 넣어서 업데이트 하고 있음 코드를 깔끔하게 작성할수 있게 설계함 개발자들이 실수 덜 할수 있게 고도화를 지속적으로 하고 있음 메모리 관리도 자동으로 됨 Safety 스위프트는 안전하지 않은 형태의 클래스를 제거함 문법과 시스템을 통해서, 발생할수 있는 위험성을 제거함 Fast 스위프트는 최초부터 빠르게 설계되었음 C, Objective-C 등과 호환도 되지만, 기본적으로 더 빠른 속도로 기존 언어들을 대체하려고 나옴 func printNames(_ names: String...) { for name in names { print("name is \\(name)") } } printNames("James",..
[Swift] RxSwift RxSwift Every Observable instance is just a sequence 구성 요소 Observable Operator - 연산자/결과 출력/다양한 값으로 변환 가능 Scheduler - 커스텀 할 일 거의 없다고. Observable - 관찰자 Observable Rx 코드의 기반 T 형태의 데이터 snapshot 을 ‘전달’ 할 수 있는 일련의 이벤트를 비동기적으로 생성하는 기능 하나 이상의 observers가 실시간으로 어떤 이벤트에 반응 세 가지 유형의 이벤트만 방출 enum Event { case next(Element) // next element of a sequence case error(Swift.Error) // sequence failed with error ca..
[Swift] DispatchQueue / Serial / Concurrent DispatchQueue 작업 항목의 실행을 관리하는 클래스 애플리케이션에서 작업을 비동기 및 동시에 수행할 수 있게 해주는 GCD 대기열 위에 있는 추상화 계층 일반 스레드 보다 쉽고 효율적으로 코드를 작성할 수 있다. 보통 서버에서 데이터를 받고 이미지 동영상을 외부에서 다운로드 및 처리할 때 CPU 사용량이 많아 처리를 메인 스레드가 아닌 별도의 스레드에서 처리한 뒤 메인 스레드로 결과만을 전달하여 화면에 표시하도록 하여 CPU를 관리할 수 있다. DispatchQueue 의 종류 - Serial / Concurrent Serial Dispatch Queue - 비동기 이전 작업이 끝나면 다음 작업을 순차적으로 실행하는 직렬 형태의 큐. 하나의 작업을 실행하고 그 실행이 끝날 때까지 대기열에 있는 ..
[Swift] DFS/BFS DFS - 깊이 우선 탐색 let graph = [ [], // 0 [2,3], // 1 [1,4,5], // 2 [1,6,7], // 3 [2], // 4 [2], // 5 [3], // 6 [3,8], // 7 [7] // 8 ] var visited = Array.init(repeating: false, count: graph.count) func dfs(start: Int) { visited[start] = true print(start, terminator: " ") for i in graph[start] { if !visited[i] { dfs(start: i) } } } dfs(start: 1) BFS - 너비 우선 탐색 let graph = [ [], // 0 [2,3], // 1 [1,4..
[Swift] MaxHeap struct MaxHeap { var heap: [T] = [] var isEmpty: Bool { heap.count Bool { if insertIndex heap[parentIndex] ? true : false } var insertIndex: Int = heap.count - 1 while isMoveUp(insertIndex) { let parentIndex = insertIndex / 2 heap.swapAt(insertIndex, parentIndex) insertIndex = parentIndex } } enum moveDownStatus {case left, right, none} mutating func pop() -> T? { if heap.count moveDownStatus { ..
[Swift] MinHeap import Foundation struct MinHeap { var heap: [T] = [] var isEmpty: Bool { return heap.count Bool { if insertIndex T? { if heap.count moveDownStatus { let leftChildIndex = poppedIndex * 2 let rightChildIndex = leftChildIndex + 1 // case1. 모든(왼쪽) 자식 노드가 없는 경우(완전이진트리는 왼쪽부터 채워지므로) if leftChildIndex >= heap.count { return .none } // case2. 왼쪽 자식 노드만 있는 경우 if rightChildIndex >= heap.count { return hea..
[Swift] Factorial / Permutations / Combinations func factorial(_ n: Int) -> Int { var n = n var result = 1 while n > 1 { result *= n n -= 1 } return result } factorial(5) // 5*4*3*2*1 = 120 func permutations(_ n: Int, _ k: Int) -> Int { var n = n var answer = n for _ in 1..
[Swift] substring let s = "hello" String(Array(s)[0.. String { let start = index(startIndex, offsetBy: max(0, range.lowerBound)) let end = index(start, offsetBy: min(self.count - range.lowerBound, range.upperBound - range.lowerBound)) return String(self[start.. String { let start = index(startIndex, offsetBy: max(0, range.lowerBound)) return String(self[start...]) } } let s = "hello" s[0.. Index { return self.ind..