返回一个向下转型

时间:2017-10-02 22:39:01

标签: ios swift cocoa-touch

我已经创建了一个枚举来管理我的应用程序的样式,我希望枚举能够返回UIColor类型或CAGradientLayer,具体取决于具体情况。这是我的代码,实际上它工作正常,但每次我使用它,我都要垂头丧气。有没有办法可以让价值降低?

import UIKit

enum Theme {
    case darkBackground
    case lightBackground
    case gradientBackground
    case darkText
    case lightText
}

extension Theme {

    var value: Any {
        var instanceColor: UIColor = .clear
        var gradient: CAGradientLayer?
        switch self {
        case .darkBackground:
            instanceColor = .black
        case .lightBackground:
            instanceColor = UIColor(white: 0.9, alpha: 1)
        case .darkText:
            instanceColor = .darkText
        case .lightText:
            instanceColor = UIColor(white: 0.9, alpha: 1)
        case .gradientBackground:
            gradient = CAGradientLayer()
            gradient?.colors = [UIColor.init(hexString: "#516395").cgColor, UIColor.init(hexString: "#614385").cgColor]

        }

        return gradient != nil ? gradient : instanceColor
    }
}

使用示例

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = Theme.lightBackground.value as? UIColor
}

1 个答案:

答案 0 :(得分:0)

如果您创建的enum可以是UIColorCAGradientLayer,则可以摆脱Any。看看这有帮助......

import UIKit

enum ColourOrGradient {
    case colour(UIColor)
    case gradient(CAGradientLayer)
    var colourGradient: (UIColor?, CAGradientLayer?) {
        switch self {
        case .colour(let c):
            return (c, nil)
        case .gradient(let g):
            return (nil, g)
        }
    }
}

enum Theme {
    case darkBackground
    case lightBackground
    case gradientBackground
    case darkText
    case lightText
}

extension Theme {

    var value: ColourOrGradient {
        switch self {
        case .darkBackground:
            return ColourOrGradient.colour(.black)
        case .lightBackground:
            return ColourOrGradient.colour(UIColor(white: 0.9, alpha: 1))
        case .darkText:
            return ColourOrGradient.colour(.darkText)
        case .lightText:
            return ColourOrGradient.colour(UIColor(white: 0.9, alpha: 1))
        case .gradientBackground:
            let gradient = CAGradientLayer()
            gradient.colors = [UIColor.blue.cgColor, UIColor.green.cgColor] // Your `.hexString` not supplied
            return ColourOrGradient.gradient(gradient)
        }
    }
}

var gradient: CAGradientLayer?
var colour: UIColor?
(colour, gradient) = Theme.lightBackground.value.colourGradient

当然,您也可以这样做:

import UIKit

enum Theme {
    case darkBackground
    case lightBackground
    case gradientBackground
    case darkText
    case lightText
}

extension Theme {

    var value: (UIColor?, CAGradientLayer?) {
        switch self {
        case .darkBackground:
            return (.black, nil)
        case .lightBackground:
            return (UIColor(white: 0.9, alpha: 1), nil)
        case .darkText:
            return (.darkText, nil)
        case .lightText:
            return (UIColor(white: 0.9, alpha: 1), nil)
        case .gradientBackground:
            let gradient = CAGradientLayer()
            gradient.colors = [UIColor.blue.cgColor, UIColor.green.cgColor] // Your `.hexString` not supplied
            return (nil, gradient)
        }
    }
}

var gradient: CAGradientLayer?
var colour: UIColor?
(colour, gradient) = Theme.lightBackground.value