stringByDeletingLastPathComponent

Discussion in 'iPhone/iPad Programming' started by idelovski, Apr 13, 2011.

  1. macrumors regular

    Joined:
    Sep 11, 2008
    Messages:
    231
    #1
    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?

    Code:
    - (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);
    }
    
     
  2. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    Messages:
    2,675
    #2
    Code:
    URLByDeletingLastPathComponent
    URLByAppendingPathComponent
     
  3. macrumors regular

    Joined:
    Sep 11, 2008
    Messages:
    231
    #3
    Thx,

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

    robbieduncan

    Staff Member

    Joined:
    Jul 24, 2002
    Messages:
    24,480
    Location:
    London
    #4
    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?
     
  5. macrumors regular

    Joined:
    Sep 11, 2008
    Messages:
    231
    #5
    Yes, this version looks so much cleaner - the old code seems almost brain-dead in comparison now.

    :)

    Code:
    - (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);
    }
    
     
  6. idelovski, May 1, 2011
    Last edited: May 1, 2011

    macrumors regular

    Joined:
    Sep 11, 2008
    Messages:
    231
    #6
    Turns out -URLByDeletingLastPathComponent and -URLByAppendingPathComponent: exist only on 10.6 and 4.0 so, it's back to square one.

    1. General methods:

    Code:
    - (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:

    Code:
    - (NSString *)stringByDeletingLastURLStringComponent
    {
       return ([self stringByDeletingLastComponentSeparatedByString:@"/"]);
    }
    
    - (NSString *)stringByAppendingURLStringComponent:(NSString *)str
    {
       return ([self stringByAppendingStringComponent:str separatedByString:@"/"]);
    }
    
    I have all four in a NSString category. Any suggestions?
     
  7. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    Messages:
    1,896
    #7
    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).
     
  8. macrumors regular

    Joined:
    Sep 11, 2008
    Messages:
    231
    #8
    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.

    :(
     
  9. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    Messages:
    1,896
    #9
    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.
     

Share This Page