官术网_书友最值得收藏!

Getting ready

Before starting to learn how to handle errors in Swift, you first have to be familiar with how to represent in errors that are going to happen in your program. Swift provides you with a protocol called ErrorType that your errors types should adopt. Then, to represent errors, here comes the role of enumerations to help you. You create a new enum, which lists all error cases, and this enum should conform to the ErrorType protocol. The syntax of using enum with ErrorType will be something like this:

enum DBConnectionError: ErrorType{ 
  case ConnectionClosed 
  case DBNotExist 
  case DBNotWritable 
} 

As we see it's pretty straightforward. You create enum representing the error that conforms to ErrorType protocol, and then list all errors as cases in the enum.

主站蜘蛛池模板: 镇坪县| 定襄县| 伊通| 睢宁县| 宁明县| 平谷区| 来安县| 平度市| 石城县| 九龙县| 陆良县| 上思县| 榕江县| 安宁市| 通山县| 张北县| 神池县| 龙井市| 东至县| 喀什市| 屏山县| 黎川县| 高邑县| 精河县| 惠州市| 农安县| 改则县| 黄平县| 文化| 广丰县| 绥棱县| 岚皋县| 杭州市| 米泉市| 临猗县| 永州市| 阿城市| 崇州市| 藁城市| 扶沟县| 周至县|