gpt4 book ai didi

swift - 在 switch 语句中匹配 ImplicitlyUnwrappedOptional

转载 作者:行者123 更新时间:2023-11-30 11:57:33 24 4
gpt4 key购买 nike

我正在尝试使用 switch case 来匹配泛型类型,但我不确定如何匹配 ImplicitlyUnwrappedOptional的。

想象一下这个功能:

enum AttributeName: String {
case someFloat = "someFloat"
case someTint = "SomeTint"
}

func defaultValue<T>(_ name: AttributeName) -> T {
switch name {
case .someFloat:
return CGFloat(8.0) as! T
case .someTint:
return UIColor.blue as! T
}
}

func attribute<T>(_ name: AttributeName) -> T? {

let attributes: [AttributeName: Any] = [AttributeName.someFloat: 10.0,
AttributeName.someTint: UIColor.red]

switch T.self {
case is CGFloat.Type:
guard let aFloat = attributes[name] as? Float
else { return nil }
return CGFloat(aFloat) as? T
case is CGColor.Type:
guard let color = attributes[name] as? UIColor
else { return nil }
return color as? T
default:
guard let attribute = attributes[name] as? T
else { return nil }
return attribute
}
}


let button = UIButton()
button.layer.cornerRadius = attribute(AttributeName.someFloat) ?? defaultValue(AttributeName.someFloat)
button.backgroundColor = attribute(AttributeName.someTint) ?? defaultValue(AttributeName.someTint)
button.tintColor = attribute(AttributeName.someTint)

如果您尝试调用以上内容,则效果很好:

let button = UIButton()
button.layer.cornerRadius = attribute("float") // will be 10.0
button.backgroundColor = attribute("tint") // will be red

但尝试分配按钮的 tintColor将返回 nil因为 T 的类型将是 ImplicitlyUnwrappedOptional<UIColor> 。即:

button.tintColor = attribute("tint") // will be nil

我想我知道 ImplicitlyUnwrappedOptional 是什么以及 Apple 为什么使用它们 ( by reading this question )。但我该如何匹配tintColor在这种情况下?

我可以这样做:let tint: UIColor = attribute("tint")并分配 tint到按钮的 tintColor 。但正在手动创建UIColor并将其分配给 tintColor真的是唯一的方法吗?

最佳答案

如何使用更好的类型来表示您的数据,从而回避您的问题?也许是这样的:

struct ButtonStyle {
// If some of these proprties need to be optional (i.e. they're not always
// set as part of a style) then comment below and I'll modify this accordingly
cornerRadius: CGFloat
backgroundColor: UIColor

init(cornerRadius: CGFloat, backgroundColor: UIColor) {
self.cornerRadius = cornerRadius
self.backgroundColor = backgroundColor
}

// If you need to initialize ButtonStyle objects from dictionaries
// (e.g. from JSON), you can use this.
// However, using `Codable` instead is ***greatly*** preferred.
init?(fromDict dict: [String: Any] {
guard let cornerRadius = dict["float"] as? CGFloat,
let backgroundColor = dict["color"] as? UIColor {
return nil
}
self.init(cornerRadius: cornerRadius, backgroundColor: backgroundColor)
}
}

extension UIButton {
var style: ButtonStyle {
get {
ButtonStyle(cornerRadius: self.layer.cornerRadius,
backgroundColor: self.backgroundColor)

}
set {
self.layer.cornerRadius = newValue.cornerRadius
self.backgroundColor = newValue.backgroundColor
}
}
}

let buttonStyle = ButtonStyle(cornerRadius: 10, backgroundColor: UIColor.red)
let button = UIButton()
button.style = buttonStyle

关于swift - 在 switch 语句中匹配 ImplicitlyUnwrappedOptional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47634601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com