从filepath以编程方式获取文件名和扩展名cocoa?

19 cocoa filenames filepath nsstring nsbundle

我知道你可以使用NSBundle:

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"rtf"];
Run Code Online (Sandbox Code Playgroud)

获取文件的文件路径,但是如何使用NSBundle以编程方式从文件路径获取文件名和扩展名(单独).

例如,我可能有:/Users/theuser/Documents/yourdocument.txt

很明显,文件是'yourdocument',扩展名'txt'

但是我需要从文件路径中获取文件名和扩展名以用作NSString.

非常感谢您的帮助!

chm*_*eee 44

有方法NSString可以做到这一点.看-[NSString pathExtension]-[NSString lastPathComponent],在这两个定义NSPathUtilities.h.


Fab*_*ano 25

要获取没有扩展名的文件名,请尝试 [NSString stringByDeletingPathExtension];