Easy Filename from a Path in Objective-C [#iosdev]

Every once in a while, there’s a feature in Objective-C that I really appreciate — the kind of method that makes you think, “Yes! Of course that should be in there!”, but yet at the same time it is surprising, because because in the past I would pretty much always have to roll my own algorithm for dealing with the particular problem.

The instance of this that I had in mind is with regard to the lastPathComponent method of NSString.

It is so convenient to be able to just get the name of a file from a path just by doing the following:

NSString* fileName = [myPath lastPathComponent];

In the past, on other platforms and languages, I typically had to write a routine that would loop through and find the last path delimiter and grab the final part of the string based on that location.


One method. Awesome.