Remove NSUserDefaults for keys starting with

Viewed 1810

I want to remove strings that are stored in the NSUserDefaults for the keys beginning with "NavView".
I thought about using the hasPrefix() method, but I just can't seem to figure it out.

I know that other programming languages have features like taking every string with a certain beginning by passing the prefix they want it to have like: find all strings with "NavView*" or something. (using signs like the star to indicate that)

Any ideas how I could do that except storing all the objects in an array and saving that?
Thanks in advance!

3 Answers

Swift 5 is the same as Swift 4

for key in UserDefaults.standard.dictionaryRepresentation().keys {
            if key.hasPrefix("NavView") {
                UserDefaults.standard.removeObject(forKey: key)
            }
        }
Related