Swift枚举高级用法(Enum)

关于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
}  

*代码地址代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。