Swift 2에서 사용자 정의 메시지로 오류 / 예외를 발생시키는 가장 간단한 방법은 무엇입니까?
Swift 2에서 다른 언어로 익숙한 작업을하고 싶습니다. 사용자 정의 메시지로 실행 예외를 사용합니다. 예를 들어 (자바) :
throw new RuntimeException("A custom message here")
ErrorType 프로토콜을 준수하는 열거 형 유형을 던질 수 있습니다. 이상적으로는 위의 예를 가능한 한 유사하게 모방하고 싶습니다. ErrorType 프로토콜을 구현하는 사용자 정의 클래스를 만들려고 시도했지만 해당 프로토콜에 필요한 것이 무엇인지 알 수 없습니다 ( documentation 참조 ). 아이디어?
가장 간단한 방법은 정의 아마 하나의 정의를 enum
단지 하나 case
있다 String
첨부 :
enum MyError: ErrorType {
case runtimeError(String)
}
또는 Swift 4 기준 :
enum MyError: Error {
case runtimeError(String)
}
사용 예는 다음과 가변합니다.
func someFunction() throws {
throw MyError.runtimeError("some message")
}
do {
try someFunction()
} catch MyError.runtimeError(let errorMessage) {
print(errorMessage)
}
기존 Error
유형 을 사용하려는 경우 가장 일반적인 유형 NSError
은이며 사용자 정의 메시지로 유형을 작성하고 던지는 팩토리 메소드를 사용할 수 있습니다.
가장 간단한 방법은 다음을 준수 String
하는 것입니다 Error
.
extension String: Error {}
그런 다음 같은 곳을 던질 수 있습니다.
throw "Some Error"
고급 자체를 localizedString
오류 로 만들기 위해 대신 확장 할 수 있습니다 LocalizedError
.
extension String: LocalizedError {
public var errorDescription: String? { return self }
}
@ nick-keets의 솔루션은 가장 우아하지만 다음 시간 오류로 테스트 대상에서 나에게 고장났습니다.
Redundant conformance of 'String' to protocol 'Error'
다른 접근 방식이 있습니다.
struct RuntimeError: Error {
let message: String
init(_ message: String) {
self.message = message
}
public var localizedDescription: String {
return message
}
}
그리고 사용하는 비용 :
throw RuntimeError("Error message.")
이 멋진 버전을 확인하십시오. 아이디어는 String 및 ErrorType 프로토콜을 모두 구현하고 오류의 rawValue를 사용하는 것입니다.
enum UserValidationError: String, Error {
case noFirstNameProvided = "Please insert your first name."
case noLastNameProvided = "Please insert your last name."
case noAgeProvided = "Please insert your age."
case noEmailProvided = "Please insert your email."
}
용법 :
do {
try User.define(firstName,
lastName: lastName,
age: age,
email: email,
gender: gender,
location: location,
phone: phone)
}
catch let error as User.UserValidationError {
print(error.rawValue)
return
}
스위프트 4 :
에 따라 :
https://developer.apple.com/documentation/foundation/nserror
사용자 지정 예외를 정의하지면 다음과 같이 표준 NSError 개체를 사용할 수 있습니다.
import Foundation
do {
throw NSError(domain: "my error description", code: 42, userInfo: ["ui1":12, "ui2":"val2"] )
}
catch let error as NSError {
print("Caught NSError: \(error.localizedDescription), \(error.domain), \(error.code)")
let uis = error.userInfo
print("\tUser info:")
for (key,value) in uis {
print("\t\tkey=\(key), value=\(value)")
}
}
인쇄물 :
Caught NSError: The operation could not be completed, my error description, 42
User info:
key=ui1, value=12
key=ui2, value=val2
이를 통해 사용자 정의, 숫자 코드 및 필요한 모든 유형의 추가 데이터가 포함 된 사전을 제공 할 수 있습니다.
주의 :이 OS = Linux (Ubuntu 16.04 LTS)에서 테스트되었습니다.
추가 확장, 열거 형, 클래스 등이없는 가장 간단한 솔루션 :
NSException(name:NSExceptionName(rawValue: "name"), reason:"reason", userInfo:nil).raise()
@Nick keets 답변을 기반으로 한 더 완전한 예는 다음과 달라집니다.
extension String: Error {}/*Enables you to throw a string*/
extension String: LocalizedError {/*Adds error.localizedDescription to Error instances*/
public var errorDescription: String? { return self }
}
func test(color:NSColor) throws{
if color == .red {
throw "I don't like red"
}else if color == .green {
throw "I'm not into green"
}else {
throw "I like all other colors"
}
}
do {
try test(color:.green)
} catch let error where error.localizedDescription == "I don't like red"{
Swift.print ("Error: \(error)")//"I don't like red"
}catch let error {
Swift.print ("Other cases: Error: \(error.localizedDescription)")/*I like all other colors*/
}
내 재해 블로그에 처음 게시 : http://eon.codes/blog/2017/09/01/throwing-simple-errors/
@ Alexander-Borisenko의 답변이 마음에 들지만 오류로 잡히면 현지화 된 설명이 반환되지 않습니다. 대신 LocalizedError를 사용하는 것입니다.
struct RuntimeError: LocalizedError
{
let message: String
init(_ message: String)
{
self.message = message
}
public var errorDescription: String?
{
return message
}
}
자세한 내용은 이 답변 을 참조하십시오.
fatalError를 사용하십시오. fatalError ("Custom message here")
'IT' 카테고리의 다른 글
react.js에서 상태 변경을 듣는 방법은 무엇입니까? (0) | 2020.07.11 |
---|---|
NSNotificationCenter로 객체를 전달하는 방법 (0) | 2020.07.11 |
ASP.NET을 사용하여 .json 파일을 다운로드하는 방법 (0) | 2020.07.11 |
vimdiff를 사용할 때 다른 색 구성표로드 (0) | 2020.07.11 |
자바 확장을 사용하여 입력 요소에 강화 된 속성 추가 (0) | 2020.07.11 |