PDA

View Full Version : stringByDeletingLastPathComponent




idelovski
Apr 13, 2011, 10:38 AM
Looks like -stringByDeletingLastPathComponent is not supposed to be used with url strings. The method would replaced double slashes with the single slash and -stringByAppendingPathComponent: seem to be doing the same.

I have created NSString category with these two methods to solve my immediate problem but now I'm curious if there is a better way to handle url addresses? Any suggestions?

- (NSString *)stringByDeletingLastPathOrURLComponent
{
NSString *tmpString = [self stringByDeletingLastPathComponent];

if (![tmpString hasPrefix:@"http://"] && [tmpString hasPrefix:@"http:/"])
tmpString = [tmpString stringByReplacingOccurrencesOfString:@"http:/" withString:@"http://"];

return (tmpString);
}

- (NSString *)stringByAppendingPathOrURLComponent:(NSString *)str
{
NSString *tmpString = [self stringByAppendingPathComponent:str];

if (![tmpString hasPrefix:@"http://"] && [tmpString hasPrefix:@"http:/"])
tmpString = [tmpString stringByReplacingOccurrencesOfString:@"http:/" withString:@"http://"];

return (tmpString);
}



PhoneyDeveloper
Apr 13, 2011, 10:47 AM
URLByDeletingLastPathComponent
URLByAppendingPathComponent

idelovski
Apr 13, 2011, 11:27 AM
Thx,

although I need these URLs as strings, before and after adding or removing components so I can put them in database.

robbieduncan
Apr 13, 2011, 11:39 AM
Thx,

although I need these URLs as strings, before and after adding or removing components so I can put them in database.

Manipulate them as NSURL and turn them into NSString objects to store to the database. Or is there some reason this simple operation cannot be used?

idelovski
Apr 13, 2011, 03:16 PM
Yes, this version looks so much cleaner - the old code seems almost brain-dead in comparison now.

:)

- (NSString *)stringByDeletingLastURLStringComponent
{
NSURL *tmpUrl = [NSURL URLWithString:self];

if (tmpUrl) {
tmpUrl = [tmpUrl URLByDeletingLastPathComponent];

return ([tmpUrl absoluteString]);
}

return (nil);
}

- (NSString *)stringByAppendingURLStringComponent:(NSString *)str
{
NSURL *tmpUrl = [NSURL URLWithString:self];

if (tmpUrl) {
tmpUrl = [tmpUrl URLByAppendingPathComponent:str];

return ([tmpUrl absoluteString]);
}

return (nil);
}

idelovski
May 1, 2011, 04:37 PM
Turns out -URLByDeletingLastPathComponent and -URLByAppendingPathComponent: exist only on 10.6 and 4.0 so, it's back to square one.

1. General methods:

- (NSString *)stringByDeletingLastComponentSeparatedByString:(NSString *)sepString
{
NSArray *split = [self componentsSeparatedByString:sepString];
NSString *tmpStr = self;
NSUInteger i, arrayCnt = [split count] - 1;

for (i=0; i<arrayCnt; i++) {
if (i)
tmpStr = [tmpStr stringByAppendingFormat:@"%@%@", sepString, [split objectAtIndex:i]];
else
tmpStr = [split objectAtIndex:0];
}

return (tmpStr);
}

- (NSString *)stringByAppendingStringComponent:(NSString *)str
separatedByString:(NSString *)sepString
{
if ([self hasSuffix:sepString])
return ([self stringByAppendingString:str]);

return ([self stringByAppendingFormat:@"%@%@", sepString, str]);
}

2. URL related methods:

- (NSString *)stringByDeletingLastURLStringComponent
{
return ([self stringByDeletingLastComponentSeparatedByString:@"/"]);
}

- (NSString *)stringByAppendingURLStringComponent:(NSString *)str
{
return ([self stringByAppendingStringComponent:str separatedByString:@"/"]);
}


I have all four in a NSString category. Any suggestions?

Sydde
May 2, 2011, 05:33 PM
I would propose this:

Create a base URL for the domain (e.g., NSURL *aDomain = [[NSURL alloc] initWithString: @" http://www.webserver.com"]; )
mess around with your path string till it is what you want
use that NSString method for massaging the string for URL use (something to do the "percent escape encoding")
put the two together: usefulURL = [NSURL URLWithString:pathString relativeTo:aDomain];


Now you can use usefulURL where you need to. If you want to manipulate pathString, do it separately and reconnect the two. To make it simpler, just create a MyFlexibleURL class that has these two parts as ivars and puts them together for you properly (perhaps even with specific method for modifying the path string).

idelovski
May 3, 2011, 03:33 PM
Interesting, but I am mostly decomposing urls, not composing them.

I start with "http://www.site.com/images/image_name.png" and then I need to break it so I can change it into something like: "http://www.site.com/images/image_name_small.png" or "http://www.site.com/thumbs/image_name.png".

I need base url (http://www.site.com) or path (http://www.site.com/images) or file name (image_name.png) and sometimes I even split a file name into pure name and extension separately so I can check if extension is valid. In the end, just string manipulation, even though these strings represent urls and I know I'll stumble upon problems sooner or later.

:(

Sydde
May 3, 2011, 06:39 PM
Then I suggest you monkey about a bit with NSURL. See what it does. I believe the method -baseURL will give you your "http://www.site.com" part and the -fragment or -relativePath method will give you your "images/image_name.png" string. Experiment with it and see what you can do with its methods.