Skip to content

Instantly share code, notes, and snippets.

@FGtatsuro
Last active December 14, 2015 03:49
Show Gist options
  • Save FGtatsuro/5023762 to your computer and use it in GitHub Desktop.
Save FGtatsuro/5023762 to your computer and use it in GitHub Desktop.
Confirm the behavior of NSRL
#import <Foundation/Foundation.h>
int main(void)
{
NSURL *baseWithSlash = [NSURL URLWithString:@"http://example.com/v1/"];
NSURL *baseWithoutSlash = [NSURL URLWithString:@"http://example.com/v1"];
NSString *relativeWithSlash = @"/foo";
NSString *relativeWithoutSlash = @"foo";
// 1.baseWithSlash + relativeWithSlash
NSURL *url = [NSURL URLWithString:relativeWithSlash relativeToURL:baseWithSlash];
NSLog(@"%@", [url absoluteString]);
// 2.baseWithSlash + relativeWithoutSlash
url = [NSURL URLWithString:relativeWithoutSlash relativeToURL:baseWithSlash];
NSLog(@"%@", [url absoluteString]);
// 3.baseWithoutSlash + relativeWithSlash
url = [NSURL URLWithString:relativeWithSlash relativeToURL:baseWithoutSlash];
NSLog(@"%@", [url absoluteString]);
// 3.baseWithoutSlash + relativeWithoutSlash
url = [NSURL URLWithString:relativeWithoutSlash relativeToURL:baseWithoutSlash];
NSLog(@"%@", [url absoluteString]);
}
% clang NSURLTest.m -framework Foundation
% ./a.out
2013-02-24 22:10:39.211 a.out[28322:707] http://example.com/foo
2013-02-24 22:10:39.213 a.out[28322:707] http://example.com/v1/foo
2013-02-24 22:10:39.213 a.out[28322:707] http://example.com/foo
2013-02-24 22:10:39.214 a.out[28322:707] http://example.com/foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment