作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个 NSURL
URL 将包含一些转义字符(日语)
NSString* currentlocationbarString = @"mbos.help.jp/search?q=専門&pg=1"
NSString *escapedString = [currentlocationbarString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:escapedString];
//url is mbos.help.jp%2Fsearch%3Fq=%E5%B0%82%E9%96%80&pg=1
当我创建 NSURLComponents
并尝试获取查询项时,它给了我 nil。
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:YES];
NSArray *queryItems = urlComponents.queryItems;
//这里是queryItems的问题
如果有人有获取查询项目的解决方案,请提供帮助。提前致谢
最佳答案
问题不在于 Unicode 字符,每当您添加编码时,请使用适合我的情况的正确字符集,我使用以下 setURLHostAllowedCharacterSet
这意味着您的 NSURLComponents
仅提供您的编码主机,要获取正确的queryItems
,请像这样使用URLQueryAllowedCharacterSet
。
NSString* currentlocationbarString = @"mbos.help.jp/search?q=専門&pg=1"
NSString *escapedString = [currentlocationbarString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:escapedString];
现在你可以获取queryItems了。
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:YES];
NSArray *queryItems = urlComponents.queryItems;
关于ios - NSURLComponents 中的 URL 查询项为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59737465/
我是一名优秀的程序员,十分优秀!