Ant*_*ton 251 cocoa objective-c
当我拥有时NSString
,/Users/user/Projects/thefile.ext
我想thefile
用Objective-C方法提取.
最简单的方法是什么?
Pet*_*ter 598
从NSString引用中获取,您可以使用:
NSString *theFileName = [[string lastPathComponent] stringByDeletingPathExtension];
Run Code Online (Sandbox Code Playgroud)
该lastPathComponent
调用将返回thefile.ext
,并将stringByDeletingPathExtension
从末尾删除扩展名后缀.
Mar*_*eau 37
如果你显示用户可读的文件名,你不希望使用lastPathComponent
.相反,将完整路径传递给NSFileManager的displayNameAtPath:
方法.这基本上做了同样的事情,只是它正确地本地化文件名并根据用户的偏好删除扩展名.
冒着迟到多年和偏离主题的风险 - 尽管 @Marc 具有出色的洞察力,但在 Swift 中它看起来像:
let basename = NSURL(string: "path/to/file.ext")?.URLByDeletingPathExtension?.lastPathComponent
Run Code Online (Sandbox Code Playgroud)