I have a bunch of files, either NSStrings or NSURLs (it doesn’t matter; they are, for the most part, interchangeable), and I need a way to find the common ancestor directory. Does anyone know how to do this?
I have a bunch of files, either NSString s or NSURL s (it doesn’t
Share
I could have sworn there was a
pathByJoiningPathComponentsmethod somewhere, or at least one like it, but I must be thinking of something else. This does the trick for paths, it may work for URLs too if you’re on 10.6 (I only tested it with paths):I don’t think there is a “built-in” way to do it, unfortunately.
If you have an array of file paths that you want to find the common ancestor, you could do something like this:
You may want to explicitly allocate and release some of these objects if you want to keep the autorelease pool clean, especially if you have a large array of paths.