Swift/Tips & Tricks
-
Date FormatSwift/Tips & Tricks 2022. 4. 15. 16:55
import UIKit import Foundation extension Date { public func toString(dateFormat: String = "HH:mm") -> String { let formatter = DateFormatter.current formatter.dateFormat = dateFormat return formatter.string(from: self) } } extension DateFormatter { public static var current: DateFormatter { let formatter = DateFormatter() formatter.timeZone = TimeZone(identifier: "Asia/Seoul") formatter.locale =..
-
Cocoapods versionsSwift/Tips & Tricks 2022. 4. 15. 10:18
Swift 에서 다른 Dependency 를 이용할 때 CocoaPods 를 사용할 수 있다. Version 을 정하지 않고 Podfile 에 아래와 같이 쓸 수도 있겠지만, pod 'FBSDKLoginKit' pod 'FBSDKShareKit' 잘 되던 Project 가 어느순간 Package 의 업데이트로 코드가 달라지면 내 소중한 프로젝트가 망가질 수가 있다. 이때, Version 을 정해두면 안심하고 사용할 수 있다. (Swift Package Manager 에서도 가능) 특정 버전으로 정하기 pod 'AFNetworking', '1.2.0' 특정 버전 이상 또는 이하 등 Logical Operator 을 이용하는 경우 '> 0.1' Any version higher than 0.1 '>= 0.1..
-
FlatMap, CompactMapSwift/Tips & Tricks 2022. 4. 8. 16:02
Swift 를 쓰다보면 (다른 언어들도 마찬가지겠지만) Array 를 다룰 때 flatMap, compactMap 에 대해 다루게 된다. 먼저 정의부터 살펴보자. compactMap Summary Sequence 의 각 element 에 주어진 transform 을 시행했을 때 nil 이 아닌 결과들로 이루어진 array Discussion transformation 이 optional value 를 생성시킬 수 있을 때, optional 이 아닌 값들의 array 를 원할 때 사용하면 된다. 복잡도: O(m+n), n: sequence 의 크기, m: 결과의 크기 Example 예시에서 .map 은 optional 을 생성하고 nil 값을 생성할 수도 있지만, compactMap 을 사용할 경우 이때에..
-
Swift 개발 / 배포 코드 다르게 하기 (if DEBUG)Swift/Tips & Tricks 2022. 4. 8. 10:37
#if DEBUG // 개발할 때 테스트 하는 코드 입력 #else // release 될 때 실행하는 코드 입력 #endif 만약 Debug Mode 코드에 초기화 관련 내용이 들어가고, 본인의 데이터도 지키고 싶으면 #if targetEnvironment(simulator) 를 추가하여 'Simulator' 인 경우로 한정해서 테스트 할 수도 있다. #if DEBUG #if targetEnvironment(simulator) // TODO: Remove All #endif #endif 실행하기 전에 알아야 했던... 폰 데이터 초기화 방지 코드이다
-
Rx Swift Cocoa pods 로 설치하기, playground 연습 환경 구축Swift/Tips & Tricks 2022. 3. 23. 00:08
설치하기 !!! 1. Cocoapods 를 설치한다. (Terminal 에 입력) sudo gem install cocoapods 2. Current Folder 로 이동한다. 3. pod init -> podfile 이 생성된다. pod init 4. podfile 로 들어가서 수정 후 저장, 닫는다. # Uncomment the next line to define a global platform for your project # platform :ios, '9.0' target 'RxSwiftProj' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! # Pods for RxSwiftProj po..
-
Swift Tips and Tricks - .enumerated()Swift/Tips & Tricks 2021. 10. 12. 13:21
Swift 에서 for statement 내에 종종 .enumerated() 가 쓰일 때가 있어요. ( enumerate: 열거하다, 세다 ) 먼저, 예시부터 드릴게요 ! let names = ["james", "kelvin", "katherine"] for (idx, name) in names.enumerated() { print("idx: \(idx), name: \(name)") } //idx: 0, name: james //idx: 1, name: kelvin //idx: 2, name: katherine 주로 위 예시와 같이 index 와 함께 사용합니다. Syntax 는 위와 같이 (index, element) 로 써주시면 편해요! 이제, option + click 설명과 함께 알아보아요. S..
-
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) 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 ..