The Foundation Framework includes many different methods related to handling URLs; however, most API's related to manipulating these URLs are based on NSString
because NSURL
is an immutable class.
In order to fix this issue, Apple has introduced NSURLComponents
to allow for manipulation of URL objects. With NSURLComponents
, NSURL
can be treated as a mutable object that allows direct manipulation. The following code snippet is an example use case:
NSURLComponents *components = [NSURLComponents componentsWithString:@"http://somewebsite.com"]; components.path = @"/somepath"; components.query = @"queryParameter=parameterValue"; NSLog(@"%@", [components URL]);
Running this code will output the following to the console:
http://somewebsite.com/somepath?queryParameter=parameterValue
Using NSURLComponents
, you may now directly manipulate NSURL
values without the use of NSString
.