macos 将 NSString 附加到 NSURL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5303411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:50:32  来源:igfitidea点击:

Append NSString to NSURL?

objective-cmacosnsstringappendnsurl

提问by Josh Kahane

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.

我有一个 NSURL,一个文件路径,我想在它的末尾添加一个 NSString(文件名),我该怎么做?但在此之后,我不希望整个事情成为 NSURL。

Thanks.

谢谢。

采纳答案by Andy Milburn

I think it's as simple as:

我认为这很简单:

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

回答by lyzkov

I think it's good solution:

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

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

In Swift you could do the following,

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

var bURL = aURL.URLByAppendingPathComponent( "newString" )

You can also state whether the URL is a directory,

您还可以说明 URL 是否是目录,

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

回答by Jim Luther

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 到一个目录,并且你想最终得到一个包含 NSURL 路径的 NSString 并附加一个文件名,使用这个:

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

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

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

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