美文网首页
Swift枚举高级用法(Enum)

Swift枚举高级用法(Enum)

作者: Smile_Later | 来源:发表于2019-04-09 16:24 被阅读0次

关于swift的枚举

一 swift对于枚举的扩展性(Enum)

  • 枚举的继承(继承任何类和协议,目前除了swift协议中@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"
        }
    }   
}  
  • 枚举实现 Comparable协议
// 关于(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
    }
}
  • 枚举的高级用法(使用枚举来解析数据源)
    private enum KeyType: String {
        case content; case image; case chooseType; case none
    }
    
    /// 这里面content中可以继续通过枚举值解析
    /// 例子
    /// 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的内部结构,通过显示cell的逻辑分化代码结构
   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
}  

*代码地址代码

相关文章

  • Swift enum(枚举)使用范例

    来源:Swift 中枚举高级用法及实践Advanced & Practical Enum usage in Swift

  • Swift探索(五): Enum & Optional

    一:枚举(Enum) 1. 枚举的基本用法 Swift 中通过 enum 关键字来声明一个枚举 Swift 中的枚...

  • Swift枚举高级用法(Enum)

    关于swift的枚举 一 swift对于枚举的扩展性(Enum) 枚举的继承(继承任何类和协议,目前除了swift...

  • Swift 中 Enum 及 Optional 介绍

    Enum 枚举的基本用法 swift 中通过 enum 关键字来声明一个枚举 而在 C 或者 OC 中默认受整数支...

  • swift的Enum和Optional

    1、Enum 1.1、基本用法 swift中通过enum关键字来声明一个枚举。 1.2、原始值(rawValue)...

  • Swift 枚举(enum)详解

    Swift 枚举(enum)详解 [TOC] 本文将介绍Swift中枚举的一些用法和其底层原理的一些探索,以及探索...

  • Swift 四、Enum & Optional

    一、Enum 1.1 枚举的基本用法 Swift语言中使用enum关键字来进行枚举的创建。 上面的代码创建了一个姓...

  • Swift 枚举

    Swift的枚举在功能上比OC强了很多,在Swift中枚举更倾向于类了 常规用法 成员变量,方法及协议 enum有...

  • Swift枚举高级用法文章

    [Swift 中枚举高级用法及实践]http://swift.gg/2015/11/20/advanced-pra...

  • Swift中rawValue的作用

    rawValue 用于swift中的enum(枚举),用于取枚举项的原始值,例如: enum Category: ...

网友评论

      本文标题:Swift枚举高级用法(Enum)

      本文链接:https://www.haomeiwen.com/subject/bafiiqtx.html