Swift那些協(xié)議 (一)

CustomDebugStringConvertible

//standard description
public protocol CustomDebugStringConvertible {

    /// A textual representation of this instance, suitable for debugging.
    ///
    /// Calling this property directly is discouraged. Instead, convert an
    /// instance of any type to a string by using the `String(reflecting:)`
    /// initializer. This initializer works with any type, and uses the custom
    /// `debugDescription` property for types that conform to
    /// `CustomDebugStringConvertible`:
    ///
    ///     struct Point: CustomDebugStringConvertible {
    ///         let x: Int, y: Int
    ///
    ///         var debugDescription: String {
    ///             return "(\(x), \(y))"
    ///         }
    ///     }
    ///
    ///     let p = Point(x: 21, y: 30)
    ///     let s = String(reflecting: p)
    ///     print(s)
    ///     // Prints "(21, 30)"
    ///
    /// The conversion of `p` to a string in the assignment to `s` uses the
    /// `Point` type's `debugDescription` property.
    public var debugDescription: String { get }
}

//使用例子 這里借鑒的是PromiseKit 框架的源碼
extension PMKError: CustomDebugStringConvertible {
    public var debugDescription: String {
        switch self {
        case .flatMap(let obj, let type):
            return "Could not `flatMap<\(type)>`: \(obj)"
        case .compactMap(let obj, let type):
            return "Could not `compactMap<\(type)>`: \(obj)"
        case .invalidCallingConvention:
            return "A closure was called with an invalid calling convention, probably (nil, nil)"
        case .returnedSelf:
            return "A promise handler returned itself"
        case .badInput:
            return "Bad input was provided to a PromiseKit function"
        case .cancelled:
            return "The asynchronous sequence was cancelled"
        case .emptySequence:
            return "The first or last element was requested for an empty sequence"
        }
    }
}

LocalizedError

/// Describes an error that provides localized messages describing why
/// an error occurred and provides more information about the error.
public protocol LocalizedError : Error {

    /// A localized message describing what error occurred.
    public var errorDescription: String? { get }

    /// A localized message describing the reason for the failure.
    public var failureReason: String? { get }

    /// A localized message describing how one might recover from the failure.
    public var recoverySuggestion: String? { get }

    /// A localized message providing "help" text if the user requests help.
    public var helpAnchor: String? { get }
}

//Usage(用法)
extension PMKError: LocalizedError {
    public var errorDescription: String? {
        return debugDescription
    }
}

總結:

  1. 什么情況下使用客蹋?

當我們寫一個框架的時候碰逸,我們自定義一個錯誤的時候,我們需要自定義缺谴。CustomDebugStringConvertible 是對錯誤的描述梭灿,我們需要拋出錯誤還是需要用Error拋出,Error的拋出錯誤描述如果需要用到上面的自定義錯誤描述篓足,我們就可以如上面一般使用鸟缕。conform localizedError protol 晶框,and then rewrite errorDescription.

  1. 怎么使用?

如上懂从,兩個代碼塊連起來就OK了

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末授段,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子番甩,更是在濱河造成了極大的恐慌侵贵,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件缘薛,死亡現(xiàn)場離奇詭異窍育,居然都是意外死亡,警方通過查閱死者的電腦和手機宴胧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門漱抓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人恕齐,你說我怎么就攤上這事乞娄。” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵仪或,是天一觀的道長确镊。 經(jīng)常有香客問我,道長溶其,這世上最難降的妖魔是什么骚腥? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮瓶逃,結果婚禮上束铭,老公的妹妹穿的比我還像新娘。我一直安慰自己厢绝,他們只是感情好契沫,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著昔汉,像睡著了一般懈万。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上靶病,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天会通,我揣著相機與錄音,去河邊找鬼娄周。 笑死涕侈,一個胖子當著我的面吹牛,可吹牛的內容都是我干的煤辨。 我是一名探鬼主播裳涛,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼众辨!你這毒婦竟也來了端三?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤鹃彻,失蹤者是張志新(化名)和其女友劉穎郊闯,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蛛株,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡虚婿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了泳挥。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片然痊。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖屉符,靈堂內的尸體忽然破棺而出剧浸,到底是詐尸還是另有隱情锹引,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布唆香,位于F島的核電站嫌变,受9級特大地震影響,放射性物質發(fā)生泄漏躬它。R本人自食惡果不足惜腾啥,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望冯吓。 院中可真熱鬧倘待,春花似錦、人聲如沸组贺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽失尖。三九已至啊奄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間掀潮,已是汗流浹背菇夸。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留仪吧,地道東北人庄新。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像邑商,于是被迫代替她去往敵國和親摄咆。 傳聞我的和親對象是個殘疾皇子凡蚜,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理人断,服務發(fā)現(xiàn),斷路器朝蜘,智...
    卡卡羅2017閱讀 134,662評論 18 139
  • 0. 寫在前面 當你開始工作時恶迈,你不是在給你自己寫代碼,而是為后來人寫代碼谱醇。 —— Nichloas C. Zak...
    康斌閱讀 5,326評論 1 42
  • 平時工作忙的沒有閑暇陪女兒暇仲,每到周末,我的時間是全給家人的副渴。昨天下了一天的雨奈附,雨急風驟,今天清晨起來煮剧,打開...
    華麗麗寶閱讀 174評論 1 3
  • 每天最主要的事情就是做對身體恢復有益的事情
    植物我愛的生命閱讀 117評論 0 0
  • 忘了吧斥滤,這所有的 就當我們只是孩子 嘴巴愛笑将鸵,眼睛會哭 想要的東西不一定得到 得不到的,有恃無恐 至于結果已經(jīng)不重...
    醉懂你的那個人閱讀 306評論 0 1