简体   繁体   中英

IOS - How do I add segments to NSURL?

Suppose you have a URL that looks like http://localhost:5867 and your wanted to append '/something' to the url. How do I append segments to this URL? I am trying this:

//_baseURL is NSURL* and documentid is NSString*
NSURL* url = [_baseURL URLByAppendingPathComponent:documentid];

Oddly.. xcode(4.6.2) will not output the resulting URL to console.

 NSLog(@"%@", [url absoluteString]);//nothing output

Thanks!

Edit

I am working in a workspace. The code that I am debugging is a static lib which is in the workspace. Could this be the culprit of all this weirdness??

Fixed

I think the reason I was having problems was because my workspace was not configured correctly and I was actually executing an older version of my static lib. This explains why I wasn't getting NSLog'ing and why the result were unexpected. I found this post which helped me understand the workspace and how to configure it for a static library. Thanks a lot for everyone's time!

There is a class message,

+ (id)URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL

So,

NSURL *base  = [NSURL URLWithString: @"http://localhost:5867"];
NSURL *child = [NSURL URLWithString: @"something" relativeToURL:base];
NSURL *base  = [NSURL URLWithString:@"http://localhost:5867"];
NSURL *url = [base URLByAppendingPathComponent:@"something"];

NSLog(@"%@", [url absoluteString]);

This works for me and prints http://localhost:5867/something , so check your variables (maybe one of them is nil or an empty string?).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM