Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

How to extract name of enum case of `UIBlurEffect.Style` in Swift

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

    ...

question from:https://stackoverflow.com/questions/65850173/how-to-extract-name-of-enum-case-of-uiblureffect-style-in-swift

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...