Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append NSString to 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.

like image 911
Josh Kahane Avatar asked Mar 14 '11 19:03

Josh Kahane


3 Answers

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 )
like image 169
lyzkov Avatar answered Nov 29 '22 03:11

lyzkov


I think it's as simple as:

    NSString *s = [aUrl.path stringByAppendingString:@"newString"];
like image 40
Andy Milburn Avatar answered Nov 29 '22 03:11

Andy Milburn


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;
like image 39
Jim Luther Avatar answered Nov 29 '22 04:11

Jim Luther