Why not use NSURL's baseURL
method:
NSString * baseURL = [URLContainer baseURL];
質問
I'm trying to cut a URL down from this:
http://www.huffingtonpost.com/2013/08/01/edward-snowden-russia_n_3688225.html
to this:
http://www.huffingtonpost.com/
using NSMakeRange(Location, Length)
My code:
if ([URLContainer hasPrefix:@"http://www."]) {
NSLog(@"URL Detected");
NSUInteger Location = 0;
NSUInteger Length = 29;
NSString *URLCutDown = [URLContainer substringWithRange: NSMakeRange(Location, Length)];
Label = URLCutDown;
}
My problem is that I don't quite know how to make the end of Length
stop at the forward slash (/
).
解決
Why not use NSURL's baseURL
method:
NSString * baseURL = [URLContainer baseURL];
他のヒント
Instead of splitting with a range you should use the methods of NSURL
such as host
, scheme
, ...
Here's a slightly cunning solution:
NSURL *startingPoint = [NSURL URLWithString:@"http://www.huffingtonpost.com/2013/08/01/edward-snowden-russia_n_3688225.html"];
NSURL *result = [[NSURL URLWithString:@"/" relativeToURL:startingPoint] absoluteURL];