我正在尝试以编程方式提取UIBlurEffect.Style的枚举案例的名称,它的rawValue为Int,而不是String。数组中的名称将为["extraLight","light","dark","regular",...]
执行print(UIBlurEffect.Style.systemChromeMaterialLight)不会打印systemChromeMaterialLight,而是打印UIBlurEffectStyle
我也尝试使用Mirror,但得到的名称是__C.UIBlurEffectStyle
我正在尝试做的示例代码:
let myStyles : [UIBlurEffect.Style] = [.light, .dark, .regular, .prominent]
for style in myStyles {
print(style) // does not work, produces "UIBlurEffectStyle"
myFunction(styleName: String(reflecting: style)) // does not work, produces "UIBlurEffectStyle"
myFunction(styleName: String(describing: style)) // does not work, produces "UIBlurEffectStyle"
myFunction(styleName: "\(style)") // does not work, produces "UIBlurEffectStyle"
}我使用的是Swift 5、iOS 14和Xcode12.3
作为参考,Apple对枚举的定义如下:
extension UIBlurEffect {
@available(iOS 8.0, *)
public enum Style : Int {
case extraLight = 0
case light = 1
case dark = 2
@available(iOS 10.0, *)
case regular = 4
...发布于 2021-01-23 05:43:20
你是否在做一些动态的事情,与你的应用程序上的名字相关,以便你可以根据选择显示正确的名字?如果是,我建议您创建自己的本地字符串枚举,然后添加一个var或函数来从中获取模糊效果,而不是试图反转它。
但是如果你真的真的因为其他原因需要它,有一个解决办法,我不推荐,但它在这里,以防你想要测试它:
let blurStyle = String(describing: UIBlurEffect(style: .systemMaterialDark))
let style = blurStyle.components(separatedBy: "style=").last?.replacingOccurrences(of: "UIBlurEffectStyle", with: "")
print(style) // SystemMaterialDark创建您自己的应用程序样式枚举:
enum AppBlurStyle: String {
case extraLight
case dark
case light
case regular
var blurEffectStyle: UIBlurEffect.Style {
switch self {
case .extraLight: UIBlurEffect.Style.extraLight
case .dark: UIBlurEffect.Style.dark
case .light: UIBlurEffect.Style.light
case .regular: UIBlurEffect.Style.regular
}
}
var blurEffect: UIBlurEffect {
switch self {
case .extraLight: UIBlurEffect(style: .extraLight)
case .dark: UIBlurEffect(style:.dark)
case .light: UIBlurEffect(style:.light)
case .regular: UIBlurEffect(style:.regular)
}
}
}或者,您甚至可以扩展UIBlurEffect.Style并添加一个name属性,逐个映射它们:
extension UIBlurEffect.Style {
var name: String {
switch self {
case .extraLight: "extraLight"
case .dark: "dark"
case .light: "light"
case .regular: "regular"
...
}
}
}https://stackoverflow.com/questions/65850173
复制相似问题