Swift枚舉高級用法(Enum)

關(guān)于swift的枚舉

一 swift對于枚舉的擴展性(Enum)

  • 枚舉的繼承(繼承任何類和協(xié)議浦箱,目前除了swift協(xié)議中@objc無法繼承,oc和swift不兼容的原因之一)
@objc protocol LabelStatus {
    @objc func labelStatus()
}

//Non-class type 'LabelState' cannot conform to class protocol 'LabelStatus'
//extension LabelState: LabelStatus {
//
}    
  • 枚舉繼承String(使用rawValue可以獲取值)
    case none    = ""
    case loading = "loading"
    case success = "success"
    case failure = "failure"
}

extension LabelState: Hashable {
    var stateStr: String {
        switch self {
        case .failure:
            return "failure"
        default:
            return "default"
        }
    }   
}  
  • 枚舉實現(xiàn) Comparable協(xié)議
// 關(guān)于(Comparable) 比較====> 相當于比較hashValue,
extension LabelState: Comparable {
    static func < (lhs: LabelState, rhs: LabelState) -> Bool {
        return lhs.hashValue < rhs.hashValue
    }
    
   static func ==(lhs: LabelState, rhs: LabelState) -> Bool {
        return lhs.hashValue == rhs.hashValue
    }
}
  • 枚舉的高級用法(使用枚舉來解析數(shù)據(jù)源)
    private enum KeyType: String {
        case content; case image; case chooseType; case none
    }
    
    /// 這里面content中可以繼續(xù)通過枚舉值解析
    /// 例子
    /// content(JsonType)
    case content(JSON)
    case image(LINK)
    case type(ChooseType)
    case none
    
    init?(json: JSON) {
        guard let conentType = json["type"] as? String else {fatalError("error json key value")}
        let keyType = KeyType(rawValue: conentType)
        switch keyType {
        case .content?:
            self = .content(json)
        case .image?:
            guard let link = json["link"] as? String else {fatalError("don't contain key image")}
            self = .image(link)
        case .chooseType?:
            guard let chooseType = json["value"] as? Bool else {fatalError("don't contain key value")}
            self = .type(chooseType ? .select : .deselect)
        default:
            return nil
        }
         return nil
    }
}

  • 使用枚舉類細化JSON的內(nèi)部結(jié)構(gòu)滑凉,通過顯示cell的邏輯分化代碼結(jié)構(gòu)
   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataParser?.cellTypes.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let type = dataParser!.cellTypes[indexPath.row]
        let cell: UITableViewCell
        switch type {
        case .content(let json):
            cell = tableView.dequeueReusableCell(withIdentifier: ContentCell.identifier, for: indexPath)
            (cell as! ContentCell).title.text = (json["msg"] as? String)
            (cell as! ContentCell).title.sizeToFit()
        case .image(let link):
            cell = tableView.dequeueReusableCell(withIdentifier: ImageCell.identifier, for: indexPath)
            let data = try? Data(contentsOf: URL(string: link)!)
            (cell as! ImageCell).icon.image = UIImage(data: data!)
        case .type(let type):
            cell = tableView.dequeueReusableCell(withIdentifier: CommonCell.identifier, for: indexPath)
            (cell as! CommonCell).mySwitch.isOn = (type == .select) ? true : false
        default:
            cell = UITableViewCell()
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 150
}  

*代碼地址代碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末谓谦,一起剝皮案震驚了整個濱河市贫橙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌反粥,老刑警劉巖卢肃,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異才顿,居然都是意外死亡莫湘,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門郑气,熙熙樓的掌柜王于貴愁眉苦臉地迎上來幅垮,“玉大人,你說我怎么就攤上這事尾组∶γⅲ” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵讳侨,是天一觀的道長匕争。 經(jīng)常有香客問我,道長爷耀,這世上最難降的妖魔是什么甘桑? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮歹叮,結(jié)果婚禮上跑杭,老公的妹妹穿的比我還像新娘。我一直安慰自己咆耿,他們只是感情好德谅,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著萨螺,像睡著了一般窄做。 火紅的嫁衣襯著肌膚如雪愧驱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天椭盏,我揣著相機與錄音组砚,去河邊找鬼。 笑死掏颊,一個胖子當著我的面吹牛糟红,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播乌叶,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼盆偿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了准浴?” 一聲冷哼從身側(cè)響起事扭,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎乐横,沒想到半個月后句旱,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡晰奖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年谈撒,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片匾南。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡啃匿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蛆楞,到底是詐尸還是另有隱情溯乒,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布豹爹,位于F島的核電站裆悄,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏臂聋。R本人自食惡果不足惜光稼,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望孩等。 院中可真熱鬧艾君,春花似錦、人聲如沸肄方。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽权她。三九已至虹茶,卻和暖如春逝薪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蝴罪。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工董济, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人洲炊。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓感局,卻偏偏與公主長得像尼啡,于是被迫代替她去往敵國和親暂衡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355