Adding components to path - Objective C Adding components to path - Objective C xcode xcode

Adding components to path - Objective C


If you are trying to use a URL for later use in retrieving data, etc., you're going to want an NSURL object when you're done. You can certainly create a URL by appending @"/%@" with the next element, but you should look at doing the following:

NSURL *originalURL = [NSURL URLWithString: @"http://www.gmail.com/"];NSURL *aaNextURL = [originalURL URLByAppendingPathComponent: @"aa"];NSURL *bbNextURL = [aaNextURL URLByAppendingPathComponent: @"bb"];NSURL *ccNextURL = [bbNextURL URLByAppendingPathComponent: @"cc"];

This is a bit long-winded if you are adding static path elements, but if you are adding computed information it solves the problem of making sure there are appropriate slashes and allows you to use the URL construct directly.