0

I am trying to extract programmatically the names of the enum cases of UIBlurEffect.Style which have a rawValue of Int not String. The names in an array would be ["extraLight","light","dark","regular",...]

Doing print(UIBlurEffect.Style.systemChromeMaterialLight) doesn't print systemChromeMaterialLight instead it prints UIBlurEffectStyle

I tried also using Mirror but this yields a name of __C.UIBlurEffectStyle

Example code of what I am trying to do:

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"
}

I am using Swift 5, iOS 14, and Xcode 12.3

For reference, the enum is defined as follows by 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

    ...

andrewz
  • 4,729
  • 5
  • 49
  • 67

1 Answers1

0

Are you doing something dynamic that is related to the name on your app so you can show the correct one based on the selection? If you are, I suggest you create your own local String enum and then add a var or function to get the blur from it instead of trying to reverse this.

But if you really, really need this for some other reason, there is a workaround, which I do not recommend, but it's here in case you want to test it out:

let blurStyle = String(describing: UIBlurEffect(style: .systemMaterialDark))
let style = blurStyle.components(separatedBy: "style=").last?.replacingOccurrences(of: "UIBlurEffectStyle", with: "")
print(style) // SystemMaterialDark

Creating your own app Style enum:

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)
        }
    }
}

Or you can even just extend UIBlurEffect.Style and add a name property, mapping them individually:

extension UIBlurEffect.Style {
    var name: String {
        switch self {
            case .extraLight: "extraLight"
            case .dark: "dark"
            case .light: "light"
            case .regular: "regular"
            ...
        }
    }
}
gmogames
  • 2,993
  • 1
  • 28
  • 40