将 NSString 附加到 NSURL?

发布于 2024-10-22 04:54:37 字数 94 浏览 7 评论 0原文

我有一个 NSURL、一个文件路径,我想在其末尾添加一个 NSString(文件名),我该怎么做?但在这之后我不希望整个事情都是一个 NSURL 吗?

谢谢。

I have an NSURL, a file path, and I want to add an NSString to the end of it (the file name) how can I do this? But after this is don't I want the entire thing to be an NSURL.

Thanks.

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

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

发布评论

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

评论(3

日久见人心 2024-10-29 04:54:37

我认为这是一个很好的解决方案:

NSURL *bUrl = [aUrl URLByAppendingPathComponent:@"newString"];

在 Swift 中你可以执行以下操作,

var bURL = aURL.URLByAppendingPathComponent( "newString" )

你还可以声明 URL 是否是一个目录,

var bURL = aURL.URLByAppendingPathComponent( "newString", isDirectory: true )

I think it's good solution:

NSURL *bUrl = [aUrl URLByAppendingPathComponent:@"newString"];

In Swift you could do the following,

var bURL = aURL.URLByAppendingPathComponent( "newString" )

You can also state whether the URL is a directory,

var bURL = aURL.URLByAppendingPathComponent( "newString", isDirectory: true )
断肠人 2024-10-29 04:54:37

我认为这很简单:

    NSString *s = [aUrl.path stringByAppendingString:@"newString"];

I think it's as simple as:

    NSString *s = [aUrl.path stringByAppendingString:@"newString"];
耳钉梦 2024-10-29 04:54:37

如果您有一个指向目录的文件 NSURL,并且您希望最终得到一个包含 NSURL 路径并附加文件名的 NSString,请使用:

NSURL *url = [NSURL fileURLWithPath:@"/System" isDirectory:YES];
NSString *filename = @"foo";
NSString *result = [url.path stringByAppendingPathComponent:filename];

您还可以使用 URLByAppendingPathComponent 但这会增加一个额外的内容创建不需要的额外 NSURL 对象的步骤。

NSURL *url = [NSURL fileURLWithPath:@"/System" isDirectory:YES];
NSString *filename = @"foo";
NSURL *newURL = [url URLByAppendingPathComponent:filename];
NSString *result = newURL.path;

If you have a file NSURL to a directory and you want to end up with a NSString containing the NSURL's path with a file name appended to it, use this:

NSURL *url = [NSURL fileURLWithPath:@"/System" isDirectory:YES];
NSString *filename = @"foo";
NSString *result = [url.path stringByAppendingPathComponent:filename];

You can also use URLByAppendingPathComponent but that adds an extra step which creates an extra NSURL object that isn't needed.

NSURL *url = [NSURL fileURLWithPath:@"/System" isDirectory:YES];
NSString *filename = @"foo";
NSURL *newURL = [url URLByAppendingPathComponent:filename];
NSString *result = newURL.path;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文