Swift
-
Swift Tips & Tricks) Pattern matching operator ~=Swift/Tips & Tricks 2021. 10. 12. 00:45
Pattern matching operator ~= 어떤 값이 어떠한 범위에 포함되어있는지에 대해서 조건을 입력할 때는 보통 if 문 안에 conditions 두개를 && 로 연결해서 사용했었는데요, if number >= 0 && number Void) { guard let url = URL(string: "someURL") else { print("cannot create url") return } var request = URLRequest(url: url) request.httpMethod = "GET" URLSession.shared.dataTask(with: request) { data, response, error in guard error == nil else { print("Error: ..
-
Swift Language ) ClosureSwift/Swift Language 2021. 10. 6. 18:43
1. Closures 1. Closures - 1.1 Closure Expressions 1.1.1 The Sorted Method 1.1.2 Closure Expression Syntax 1.1.3 Inferring Type From Context 1.1.4 Implicit Returns from Single-Expression Closures 1.1.5 Shorthand Argument Names 1.1.6 Operator Methods 1.2 Trailing Closures 1.3 Capturing Values 1.4 Closures Are Reference Types 1.5 Escaping Closures 1.6 Autoclosures Closures 는 pass 가능하고 코드에서 사용가능한 se..
-
Swift Language) ARC ( Automatic Reference Counting)Swift/Swift Language 2021. 10. 4. 20:47
Swift uses Automatic Reference Counting (ARC) to track and manage your app’s memory usage. In most cases, this means that memory management “just works” in Swift, and you don’t need to think about memory management yourself. ARC automatically frees up the memory used by class instances when those instances are no longer needed. 예전에 위 bold 처리된 부분을 읽고 (메모리 관리가 Swift 에서는 그냥 되기 때문에 신경쓰지 않아도 된다. ) 해당..
-
Swift Language) sort, map, filter, reduceSwift/Tips & Tricks 2021. 9. 29. 13:57
sort, sorted sort: 자신 (array) 를 정렬 sorted: array 를 정렬해서 return (새로운 값에 할당, array 자체는 그대로 유지) var numArr = [1,3,2,1,5] let newArr = numArr.sorted() // newArr: [1,1,2,3,5], numArr: [1,3,2,1,5] numArr.sort() // numArr: [1,1,2,3,5] numArr.sort(by: >) // decending order. // numArr: [5,3,2,1,1] map array의 각 element 에 대하여 어떤 logic 을 수행, logic 이 적용된 각 element 를 새로운 array 로 묶어 return ( 순서 유지) let numArr ..
-
Swift Language ) GenericsSwift/Swift Language 2021. 9. 23. 13:46
Generics Generic 은 유연하고 재사용 가능하도록 함수와 타입을 다룰 수 있게 해주고, 어떠한 type, subject 와도 함께 사용될 수 있어요. 이를 통해 중복되는 코딩을 하지 않을 수 있습니다. 대부분의 Swift standard library 는 generic 으로 이루어져있고, Generic 은 Swift 의 가장 강력한 기능 중 하나에요. 예를 들면, Swift 에서 array, dictionary 는 모두 generic collections 이에요. 따라서 Int, String 뿐만 아니라 Swift 내에서 생성될 수 있는 어떠한 타입이든 array 의 element 로서 존재할 수 있습니다. (dictionary 내 element 도 마찬가지에요) 1.1 The Problem..
-
Swift Language ) Access ControlSwift/Swift Language 2021. 9. 16. 14:59
Swift 에서 Access Control 은 소스코드 간의 접근을 관리하는 거에요. Access Control 을 통해 detail 한 implementation 을 숨길 수 있고, protocol 에 대해서도 정해줄 수 있어요. 각 individual types (classes, structures, and enumerations), 이에 속하는 properties, methods, initializers, subscripts 에 대해 access level 을 지정해줄 수 있어요. (어디까지 허용할지) Swift 에서는 default access level 을 제공하기 때문에, single-target app 에 대해서는 따로 지정해주지 않아도 될거에요. ( properties, types, fun..
-
Swift Language ) ProtocolSwift/Swift Language 2021. 9. 16. 12:18
Protocol 은 어떤 목적을 달성하기 위해 필요한 것들 (Properties, Methods, ...) 을 미리 정해놓은 청사진과 같은 개념입니다. Enumeration, Struct, Class 는 Protocol 을 받으면서 실제 필요한 기능들을 상세히 정합니다. (Implementation) 그리고, 이러한 행위를 'Conform' 이라 부릅니다. (conform: 따르다) Protocol 을 Extend 하면서 필요한 기능들을 상세히 정할 수도 있고, Conform 하는 Type 들이 이용할 다른 기능들도 추가할 수 있어요. ( 이전 Extension 에서 언급은 했으나 아직 다루지 않은 부분이에요. ) Protocol을 선언하는 방식은 Struct, Class 등을 선언하는 방식과 같아요 ...
-
Swift Tips & Tricks - deferSwift/Tips & Tricks 2021. 9. 14. 13:36
Swift 에 defer 라는 keyword 가 있다. 사전적 의미는 연기하다, Xcode 내 의미는 Excutes a set of statements before execution leaves the current block of code. 즉, defer 가 속해있는 block 이 나가기 전에 (block 이 끝 라인까지 읽혀서 끝나거나, 중간에 guard let 등으로 코드를 벗어나게 될 때) 실행된다. 아래는 youtube 에서 가져온 예제 소스이다. func useFile() { let file = try! FileHandle(forReadingFrom: URL(string: "~/temp.txt")!) guard let data = try? file.readToEnd() else { retur..