ohh*_*hho 8 regex objective-c nsurl nsstring ios
提取和删除方案名称的正确方法://是NSURL什么?
例如:
note://Hello -> @"Hello"
calc://3+4/5 -> @"3+4/5"
Run Code Online (Sandbox Code Playgroud)
所以
NSString *scheme = @"note://";
NSString *path = @"Hello";
Run Code Online (Sandbox Code Playgroud)
供以后使用:
[[NSNotificationCenter defaultCenter] postNotificationName:scheme object:path];
Run Code Online (Sandbox Code Playgroud)
Wri*_*sCS 15
你可以这样看(大多数未经测试的代码,但你明白了):
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSLog(@"url: %@", url);
NSLog(@"scheme: %@", [url scheme]);
NSLog(@"query: %@", [url query]);
NSLog(@"host: %@", [url host]);
NSLog(@"path: %@", [url path]);
NSDictionary * dict = [self parseQueryString:[url query]];
NSLog(@"query dict: %@", dict);
}
Run Code Online (Sandbox Code Playgroud)
所以你可以这样做:
NSString * strNoURLScheme =
[strMyURLWithScheme stringByReplacingOccurrencesOfString:[url scheme] withString:@""];
NSLog(@"URL without scheme: %@", strNoURLScheme);
Run Code Online (Sandbox Code Playgroud)
parseQueryString
- (NSDictionary *)parseQueryString:(NSString *)query
{
NSMutableDictionary *dict = [[[NSMutableDictionary alloc] initWithCapacity:6] autorelease];
NSArray *pairs = [query componentsSeparatedByString:@"&"];
for (NSString *pair in pairs) {
NSArray *elements = [pair componentsSeparatedByString:@"="];
NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[dict setObject:val forKey:key];
}
return dict;
}
Run Code Online (Sandbox Code Playgroud)
我的倾向是滚动你自己的字符串搜索:
NSRange dividerRange = [urlString rangeOfString:@"://"];
NSUInteger divide = NSMaxRange(dividerRange);
NSString *scheme = [urlString substringToIndex:divide];
NSString *path = [urlString substringFromIndex:divide];
Run Code Online (Sandbox Code Playgroud)
这就是你所要求的非常直接的方式,将URL分成一半.要获得更高级的处理,您必须提供更多详细信息.
只需使用 NSURL 的 API 即可:resourceSpecifier:)
并从字符串开头删除两个斜杠:
NSURL *url = [NSURL URLWithString:@"https://some.url.com/path];
url.resourceSpecifier将导致://some.url.com/path
| 归档时间: |
|
| 查看次数: |
11931 次 |
| 最近记录: |