NSURL 文件扩展名添加等

发布于 2024-11-25 12:10:46 字数 516 浏览 1 评论 0原文

我从 savePanel 工作表收到了一个 URL,我想执行以下操作:

  • 检查它是否有扩展名
  • (如果有),将其删除,
  • 则添加自定义扩展名
  • 如果没有,

,然后添加自定义扩展名任何简单的方法这..我尝试了类似以下的方法,但它不起作用

if ( [tmp pathExtension] != @"xxx" )
   path = [tmp stringByAppendingFormat:@"xxx"];

……可能的解决方案如下

NSString *path;
NSURL *filepath;
fileurl = [sheet URL];
fileurl = [fileurl URLByDeletingPathExtension];
fileurl = [fileurl URLByAppendingPathExtension:@"yyy"];
path = [fileurl path];

I have received a URL from the savePanel sheet and I would like to do following:

  • Check to see if it has an extension
  • if it does, remove it
  • add a custom extension
  • if it does not, then add a custom extension

Any simple way to do this.. I tried something like following but it does not work

if ( [tmp pathExtension] != @"xxx" )
   path = [tmp stringByAppendingFormat:@"xxx"];

OK... A possible solution as follows

NSString *path;
NSURL *filepath;
fileurl = [sheet URL];
fileurl = [fileurl URLByDeletingPathExtension];
fileurl = [fileurl URLByAppendingPathExtension:@"yyy"];
path = [fileurl path];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦等七年七年为一梦 2024-12-02 12:10:46

这可以使用 NSString 的方法来实现。请注意,对于字符串比较,您必须使用 isEqualToString:,而不是 ==,它测试指针是否相等。

关于扩展使用:-(NSString *)pathExtension;。要删除扩展,请使用 -(NSString *)stringByDeletingPathExtension;

在所有情况下,要添加扩展名,请使用以下示例组成新字符串:+(NSString *)stringWithFormat:

所以 :

NSString *finalString;
if([[tmp pathExtension] isEqualToString:@"xxx"]) {
    finalString = [tmp stringByDeletingPathExtension];
}
finalString = [NSString stringWithFormat:@"%@.yyy", finalString];

This can be achieved using NSString's methods. Note that for string comparison, you have to use isEqualToString:, not ==, which tests for pointers equality.

About the extension use : -(NSString *)pathExtension;. To remove the extension use -(NSString *)stringByDeletingPathExtension;.

In all cases to add an extension, compose a new string using for example : +(NSString *)stringWithFormat:.

So :

NSString *finalString;
if([[tmp pathExtension] isEqualToString:@"xxx"]) {
    finalString = [tmp stringByDeletingPathExtension];
}
finalString = [NSString stringWithFormat:@"%@.yyy", finalString];
我是男神闪亮亮 2024-12-02 12:10:46

@user756245 在同一页面上给出的可行解决方案的替代方法,但使用不同的 NSString 方法:

NSString *finalString;
if([[tmp pathExtension] isEqualToString:@"xxx"]) {
    finalString = [tmp stringByDeletingPathExtension];
}

finalString = [finalString stringByAppendingPathExtension:@"yyy"];

An alternate approach to the workable solution given on this same page by @user756245 but using different NSString methods:

NSString *finalString;
if([[tmp pathExtension] isEqualToString:@"xxx"]) {
    finalString = [tmp stringByDeletingPathExtension];
}

finalString = [finalString stringByAppendingPathExtension:@"yyy"];
生来就爱笑 2024-12-02 12:10:46

这是使用 URL 的 Swift 4.1 更新

// assuming you are building an URL from string
let url = URL(string: "file.abc")!
let finalUrl = url.deletingPathExtension().appendingPathExtension("mp3")
let finalString = finalUrl.path // output "file.mp3"

Here is an update for Swift 4.1 using URL

// assuming you are building an URL from string
let url = URL(string: "file.abc")!
let finalUrl = url.deletingPathExtension().appendingPathExtension("mp3")
let finalString = finalUrl.path // output "file.mp3"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文