How to return an entity with enum

0

How about everyone. I'm trying to return an entity with an enum, not a tuple.

Currently I have already returned exceptions but my intention is to have entities by defects, because they are few.

I do this to return an exception with enum.

enum EXCEPTION: String, Error {
            case REGISTER_TYPE = "You need register other type, example BANCA"
            case LIST_ERROR = "You list index not exists"
}

I hope you can help me, regards.

    
asked by marlonpya 12.09.2017 в 19:57
source

1 answer

0

Simply by having your enum implement the Error protocol, you can already use it to throw exceptions.

For example:

func someFunction() throws {
    if someCondition { 
        throw EXCEPTION.REGISTER_TYPE
    }
}

This function could then be used with a try catch block like this:

do {
    try someFunc()
}
catch let error as EXCEPTION {
   print(error) //REGISTER_TYPE
   print(error.rawValue) //You need register other type, example BANCA
}
    
answered by 13.09.2017 в 11:23