Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

iphone - iOS: parse a URL into segments

What's an efficient way to take an NSURL object such as the following:

foo://name/12345 

and break it up into one string and one unsigned integer, where the string val is 'name' and the unsigned int is 12345?

I'm assuming the algorithm involves converting NSURL to an NSString and then using some components of NSScanner to finish the rest?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I can only add an example here, the NSURL class is the one to go. This is not complete but will give you a hint on how to use NSURL:

NSString *url_ = @"foo://name.com:8080/12345;param?foo=1&baa=2#fragment";
NSURL *url = [NSURL URLWithString:url_];

NSLog(@"scheme: %@", [url scheme]); 
NSLog(@"host: %@", [url host]); 
NSLog(@"port: %@", [url port]);     
NSLog(@"path: %@", [url path]);     
NSLog(@"path components: %@", [url pathComponents]);        
NSLog(@"parameterString: %@", [url parameterString]);   
NSLog(@"query: %@", [url query]);       
NSLog(@"fragment: %@", [url fragment]);

output:

scheme: foo
host: name.com
port: 8080
path: /12345
path components: (
    "/",
    12345
)
parameterString: param
query: foo=1&baa=2
fragment: fragment

This Q&A NSURL's parameterString confusion with use of ';' vs '&' is also interesting regarding URLs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...