일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- optional
- type
- 스위프트
- UIKit
- init
- Method
- String
- interpace
- Swift
- IOS
- struct
- initalizer
- Protocol
- url
- enum
- 코딩테스트
- Xcode
- extension
- instance
- Git
- 디자인패턴
- Unicode
- initializer
- Foundation
- 이니셜라이저
- property
- Class
- tuist
- Terminal
- delegate
- Today
- Total
목록2021/08 (24)
아리의 iOS 탐구생활
Apple Developer Documentation developer.apple.com 아이폰 앱마다 자기만의 공간을 가지고 있는데, 이 공간을 관리하는 매니저라 생각하면 될 것 같다. 이 공간은 일반 맥, 윈도우처럼 Document 폴더, Download 폴더 등등 다양한 종류의 폴더가 있다. 경로(URL) 접근하기 다른 작업을 하기전 공통 사항인 해당 폴더로 접근하는 방법을 먼저 알아보자. // 파일매니저 인스턴스 생성 let fileManager = FileManager.default // 사용자의 문서 경로 let documentPath: URL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] urls라는 메소드는 요청된 ..
🔍 try do-catch Error Handling — The Swift Programming Language (Swift 5.5) Error Handling Error handling is the process of responding to and recovering from error conditions in your program. Swift provides first-class support for throwing, catching, propagating, and manipulating recoverable errors at runtime. Some operations aren docs.swift.org 오류처리(Error Handling)는 프로그램이 오류를 일으켰을 때 감지하고 복구하는 프로..
Date 관련 타입들은 앱 개발을 할때 굉장히 많이 사용하게 되는 타입중 하나이다. 시간과 날짜를 다루는 타입들은 swift 기본 프레임워크인 Foundation에 포함되어 있다. 🔍 Date Apple Developer Documentation developer.apple.com Foundation의 Date 타입은 전 세계적으로 정확히 같은 시간을 표현하기 위한 타입이다. init() 현재 시간을 나타내준다. init(timeIntervalSinceNow: TimeInterval) 현재 시각으로 부터 입력한 초 이후의 시점을 나타낸다. init(timeInterval: TimeInterval, since: Date) 다른 Date()객체로부터 입력한 초 이후의 시점을 나타낸다. init(timeInt..
✔️ Apple 공식문서 참고 Apple Developer Documentation developer.apple.com Strings and Characters — The Swift Programming Language (Swift 5.5) Strings and Characters A string is a series of characters, such as "hello, world" or "albatross". Swift strings are represented by the String type. The contents of a String can be accessed in various ways, including as a collection of Character values. Swi docs.s..
Apple Developer Documentation developer.apple.com 🔍 CaseInsensitive Option 스위프트에서는 대소문자를 구분하는데, 해당 옵션을 추가하면 대소문자 구분을 없앨 수 있다. "A" == "a" // false "A".caseInsensitiveCompare("a") == .orderedSame // true "A".compare("a", options: [.caseInsensitive]) == .orderedSame // true /* 원래 해당 옵션의 풀네임은 NSString.CompareOptions.caseInsensitive 이건데, 스위프트는 타입추론이 가능하여 보다 짧게 입력하여 편리하게 사용할 수 있다. */ 🔍 Literal Option ..
🔍 Codable이 뭘까? Codable은 Decodable와 Encodable를 준수하는 타입(프로토콜)이다. JSON 데이터를 간편하고 쉽게 인코딩 & 디코딩 할 수 있게 해준다. typealias Codable = Decodable & Encodable 구조체, 클래스, 열거형 모두 Codable을 채택할 수 있다. 먼저 JSON을 인코딩 및 디코딩을 하는데 이용할 타입을 만들어주고 Codable를 채택하자. struct Person: Codable { // Codable 프로토콜 채택 var name: String var age: Int } 👉🏻 JSON 만들기 (Encoding) JSONEncoder를 선언한다. JSONEncoder의 encode메소드를 사용하여 인스턴스를 Data타입으로 만든..
Protocol Extension extension은 타입을 확장한다. 프로토콜 역시 타입이기 때문에 extension을 활용하여 확장할 수 있다. 프로토콜에는 정의만 한다고 하였는데 extension을 통해 구현을 추가한다는 것이 조금 어색할 수도 있다. 정리하자면 문법적으로는 프로토콜의 구현을 추가하지만 실제로는 프로토콜을 채용한 타입의 구현이 추가된다고 보면 된다. 코드의 중복을 줄이면서 프로토콜을 확장시킬 수 있다는 장점이 있다. 예제를 통해 이해해보자. protocol SayText { var text: String { get set } func say() } extension SayText { func say() { print(text) } } struct A: SayText { var tex..
Protocol Types 프로토콜은 first-class Citizen이다. 즉 독립적인 타입이다. 변수나 상수를 사용할때 자료형으로 사용하거나 파라미터의 자료형으로 사용할 수 있다. 반환타입으로 선언하는 것도 가능하다. protocol TypeA { func say() } class A: TypeA { let text: String = "난 A야!" func say() { print(text) } } let a = A() let t: TypeA = A() // 타입을 프로토콜로 선언하여도 에러가 없다. 확인해보면 프로토콜 타입으로 클래스 인스턴스를 생성하여도 에러가 없다. 왜냐하면 클래스 A는 프로토콜 TypeA를 채택하고 있기 때문이다. 이것은 슈퍼클래스 타입을 저장하는 업캐스팅과 유사하다. 업캐스..
Initializer Requirements 🔍 구조체와 클래스에서 생성자 구현의 차이 구조체는 Memberwise Initializers를 통해 프로토콜 요구사항을 충족시킬 수 있다. 직접 이니셜라이저를 구현해줘도 상관없다. 클래스는 상속을 고려해야하고 모든 서브클래스에서 프로토콜의 요구사항을 충족시켜야 한다. 그래서 클래스에서는 required initializer로 선언해야 한다. protocol Test { var name: String { get } init(name: String) } //struct A: Test { // var name: String // Memberwise Initializers를 통해 프로토콜 요구를 충족시킨다. //} struct A: Test { var name: S..
Method Requirements 👉🏻 프로토콜에서의 메소드 정의하기 protocol Protocols { func A(param: Int) -> Int static func B(param: String) -> String mutating func C(param: Double) -> Double // 값타입 전용은 아니다. 참조타입에서도 채택가능하다. } 프로토콜은 메서드 이름, 파라미터 이름과 타입, 리턴 타입만 정의한다. 채택하여 메서드를 구현할때 구현부는 자유롭게 작성할 수 있다. protocol Talk { func say() } class A: Talk { func say() { print("난 클래스 A라고 해.") } } 만약 구조체안에 메서드가 프로퍼티의 값을 바꾸려한다면 mutating..