Instantly share code, notes, and snippets.

What would you like to do?
Building NSURL Queries with NSURLQueryItems and NSURLComponents
NSString *urlString = @"";
NSURL *url = [NSURL URLWithString:urlString];
urlString = [urlString stringByAppendingString:@"&include_rts=true"];
url = [NSURL URLWithString:urlString];
NSString *screenName = @"joemasilotti";
NSString *includeRTs = @"true";
urlString = [NSString stringWithFormat:@"", screenName, includeRTs];
url = [NSURL URLWithString:urlString];
NSURLComponents *components = [[NSURLComponents alloc] init];
components.scheme = @"http"; = @"";
components.path = @"/1.1/statuses/user_timeline.json";
components.query = @"screen_name=joemasilotti&include_rts=true";
url = components.URL;
NSLog(@"%@", components.queryItems);
"<NSURLQueryItem 0x7fbdbb4281b0> {name = screen_name, value = joemasilotti}",
"<NSURLQueryItem 0x7fbdbb428250> {name = include_rts, value = true}"
NSURLQueryItem *screenNameItem = [NSURLQueryItem queryItemWithName:@"screen_name"
NSURLQueryItem *includeRTsItem = [NSURLQueryItem queryItemWithName:@"include_rts"
components.queryItems = @[ screenNameItem, includeRTsItem ];
NSLog(@"%@", components.URL);
for (NSURLQueryItem *item in components.queryItems) {
if ([ isEqualToString:@"screen_name"]) {
if ([item.value isEqualToString:@"joemasilotti"]) {
return YES;
return NO;
NSURLQueryItem *item = [NSURLQueryItem queryItemWithName:@"screen_name"
[components.queryItems containsObject:item] should be_truthy;

This comment has been minimized.

Copy link

singhamit089 commented Jun 12, 2015

While I tried your method, I got following exception on line no 31

-[__NSCFNumber stringByAddingPercentEncodingWithAllowedCharacters:]: unrecognized selector sent to instance 0xb0000000000000a2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment