Skip to content

Instantly share code, notes, and snippets.

@stevestreza
Created September 26, 2011 19:52
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stevestreza/1243216 to your computer and use it in GitHub Desktop.
Save stevestreza/1243216 to your computer and use it in GitHub Desktop.
A method for debugging NSURLs to find where the different pieces of your URL are
#import <Foundation/Foundation.h>
@interface NSURL (Pieces)
-(NSDictionary *)piecesDictionary;
@end
#import "NSURL+Pieces.h"
@implementation NSURL (Pieces)
-(NSDictionary *)piecesDictionary{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
#define AddValueForSelector(__selector) do{ id obj = [self performSelector:NSSelectorFromString(__selector)]; if(obj) [dictionary setObject:obj forKey:__selector]; }while(0)
AddValueForSelector(@"absoluteString");
AddValueForSelector(@"absoluteURL");
AddValueForSelector(@"baseURL");
AddValueForSelector(@"fragment");
AddValueForSelector(@"host");
AddValueForSelector(@"lastPathComponent");
AddValueForSelector(@"parameterString");
AddValueForSelector(@"password");
AddValueForSelector(@"path");
AddValueForSelector(@"pathComponents");
AddValueForSelector(@"pathExtension");
AddValueForSelector(@"port");
AddValueForSelector(@"query");
AddValueForSelector(@"relativePath");
AddValueForSelector(@"relativeString");
AddValueForSelector(@"resourceSpecifier");
AddValueForSelector(@"scheme");
AddValueForSelector(@"standardizedURL");
AddValueForSelector(@"user");
#undef AddValueForSelector
return dictionary;
}
@end
@stevestreza
Copy link
Author

Sample output by NSLogging this call for "http://apple.com/imac/":

{ absoluteString = "http://apple.com/imac/"; absoluteURL = "http://apple.com/imac/"; host = "apple.com"; lastPathComponent = imac; path = "/imac"; pathComponents = ( "/", imac ); pathExtension = ""; relativePath = "/imac"; relativeString = "http://apple.com/imac/"; resourceSpecifier = "//apple.com/imac/"; scheme = http; standardizedURL = "http://apple.com/imac/"; }

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