Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- cs
- composable architecture
- Swift
- uikit
- 스위프트
- 백준
- 운영체제
- 알고리즘
- spritekit
- TCA
- 스유
- 네트워크
- 후기
- 대외활동
- Protocol
- 멋사
- SwiftUI
- 문법
- 위젯킷
- 영남대
- 멋쟁이사자처럼
- dispatchqueue
- 컴퓨터그래픽스
- 웹
- widgetkit
- c++
- widget
- 리액트
- ReactorKit
- swift concurrency
Archives
- Today
- Total
맛동산이
Swift) rethrows란 본문
rethrows란 함수를 파라미터로 받는 함수에서 에러가 발생할수 있음을 의미하는 키워드이다.
즉 최소 한개 이상의 에러를 throw하는 함수를 매개변수로 받아야 한다.
간단한 예제는 다음과 같다.
enum Errors: Error { case someError } func throwErrors(about value: Int) throws -> Int { if value == 0 { throw Errors.someError } return value } func checkThrow(completion: (Int) -> Int ) { do { try throwErrors(about: 0) } catch (let error) { print(error.localizedDescription) } } checkThrow(completion: throwErrors)
이렇게 컴파일 에러가 발생한다.
하지만 여기에서
enum Errors: Error { case someError } func throwErrors(about value: Int) throws -> Int { if value == 0 { throw Errors.someError } return value } func checkThrow(completion: (Int) throws -> Int ) rethrows { do { try throwErrors(about: 0) } catch (let error) { print(error.localizedDescription) } } do { try checkThrow(completion: throwErrors) } catch(let error) { print(error.localizedDescription) }
이러한 코드는 작성이 가능하게 된다.
반응형
'앱 > Swift' 카테고리의 다른 글
Swift) Dispatch Group이란, 사용하는 방법 (0) | 2023.08.28 |
---|---|
Swift) Swift concurrency가 나온 배경에 대해서, thread와 GCD의 한계점 (2) | 2023.08.28 |
Swift) Appstorage와 userDefault (0) | 2023.08.28 |
Swift) Collection과 iteratorProtocol, Sequence, AsyncSequence을 왜 채택하는가?(feat. AsyncStream) (0) | 2023.08.28 |
Swift) 소수점 다루기 (0) | 2023.08.18 |