Search code examples
iosobjective-cnsstringnsurl

NSURL path getting CGPathRef instead of NSString


Currently I am getting CGPathRef for the path variable below.

In another project I am able to get NSString for the path variable while in this project I am able to get CGPathRef.

[[[NSURL URLWithString:fileName]path] lastPathComponents];

How do I specify it to get NSString instead of CGPathRef for the particular variable?


Solution

  • From The NSString reference, you can use :

    NSString* theFileName = [[string lastPathComponent] stringByDeletingPathExtension]
    

    The lastPathComponent call will return "thefile.ext", and the stringByDeletingPathExtension will remove the .ext from the end.

    src: Objective-C: Extract filename from path string