Incrementing paths

I've added a new feature to KSFileUtilities: -[NSString ks_stringByIncrementingPath]

What's this for? It helps with the fairly common pattern of building up a path, trying to find the best available. You don't want to overwrite an existing file if it exists, but instead find another path, as similar to the first as possible.

Perhaps best described by a simple example:

Let's say you passed in @"foo.png" as the filename, if that wasn't available -ks_stringByIncrementingPath will return @"foo-2.png". Followup calls will give @"foo-3.png", @"foo-4.png", and so on.

Isn't that great? No need to worry about tracking the base path and count in use. It's all handled internally for you.

How's this work? Internally -ks_stringByIncrementingPath performs a little magic. It returns a special NSString subclass that tracks the path number in use, so that future calls correctly increment it. Check out the source if you want to see how it works!

I imagine this won't meet everyone's needs. Perhaps you want a different separator string, or to apply the same logic to URLs. If so let me know on GitHub and I'll look into it.

© Mike Abdullah 2007-2013