UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Cycle through enum - checking if that value exists an object

Forums > Swift

Hi all,

So I have an enum with elements

 enum UserElements: String, CaseIterable {

        case profile    = "UserProfile"
        case settings   = "UserSettings"
        case favorites  = "UserFavorites"
        case likes      = "UserLikes"
        case security   = "UserSecurity"

        enum Keys: String {
            case profile   = "userProfile"
            case settings  = "userSettings"
            case favorites = "userFavorites"
            case likes     = "userLikes"
            case security  = "userSecurity"
        }
   }

but I'd like to cycle through the enums and check if the user object I get back from an API call has a value and is not nil.

How do I go about checking the object dynamically withouth having to hard code each case ?

ie.

   for element in User.UserElements.allCases {

           // userObject struct has each element
            // check here to make sure object we get back has a value

            if let elementExists = userObject.<what can I do here?> {

                // do something when nil
            }

        }

normally we could just use userObject.userProfile but I don't want to hard code each element. I can't seem to find a way in Swift to access a property using a variable.

thanks in advance!

1      

Use subscripts for that. Example below.

struct User {
    var profile: String?
    var settings: String?
    var favorites: String?
    var likes: String?
    var security: String?
}

extension User {
    subscript(key: String) -> String? {
        switch key {
        case "UserProfile": return profile
        case "UserSettings": return settings
        case "UserFavorites": return favorites
        case "UserLikes": return likes
        case "UserSecurity": return security
        default: return nil
        }
    }
}

enum UserElements: String, CaseIterable {
    case profile    = "UserProfile"
    case settings   = "UserSettings"
    case favorites  = "UserFavorites"
    case likes      = "UserLikes"
    case security   = "UserSecurity"

    enum Keys: String {
        case profile   = "userProfile"
        case settings  = "userSettings"
        case favorites = "userFavorites"
        case likes     = "userLikes"
        case security  = "userSecurity"
    }
}

let userObject = User(
    profile: "Profile",
    settings: nil,
    favorites: "Favorites",
    likes: "Many Likes",
    security: "No Security"
)

for element in UserElements.allCases {
    if let elementExists = userObject[element.rawValue] {
        print(elementExists)
    }
}

2      

that's awesome. thanks for this. I was looking into subscripts!

1      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.