小编Ho *_*uan的帖子

将 switch case 语句转换为 Swift 中的值

也许这是一个愚蠢的问题

我有一个这样的 switch case 语句:

        self.text = type.rawValue
        switch type {
        case .teuro:
            self.backgroundColor =  UIColor.sapphireColor()
        case .lesson:
            self.backgroundColor = UIColor.orangeColor()
        case .profession:
            self.backgroundColor = UIColor.pinkyPurpleColor()
        }
Run Code Online (Sandbox Code Playgroud)

有什么办法可以将其写成类似以下示例的内容:

        self.backgroundColor = {
            switch type {
            case .teuro:
                return  UIColor.sapphireColor()
            case .lesson:
                return  UIColor.orangeColor()
            case .profession:
                return UIColor.pinkyPurpleColor()
            }
        }
Run Code Online (Sandbox Code Playgroud)

任何评论或回答表示赞赏。谢谢。

enums case switch-statement swift

1
推荐指数
1
解决办法
1705
查看次数

标签 统计

case ×1

enums ×1

swift ×1

switch-statement ×1