ios NSString URL 解码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15651553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:53:24  来源:igfitidea点击:

NSString URL decode?

iosnsstringurldecodepercent-encoding

提问by Geri Borbás

I have tried a lot of approaches out there, but this tiny little string just cannot be URL decoded.

我已经尝试了很多方法,但是这个很小的字符串不能被 URL 解码。

NSString *decoded;
NSString *encoded = @"fields=ID%2CdeviceToken";
decoded = (__bridge NSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (CFStringRef)encoded, NULL, NSUTF8StringEncoding);
NSLog(@"decodedString %@", decoded);

The code above just logs the same (!) string after replacing percent escapes.

上面的代码只是在替换百分比转义后记录相同的 (!) 字符串。

Is there a reliable solution out there? I think some kind of RegEx solution based on some documentation could work. Any suggestion?

有可靠的解决方案吗?我认为基于某些文档的某种 RegEx 解决方案可以工作。有什么建议吗?

采纳答案by Carl Veazey

Use CFSTR("")instead of NULLfor the second to last argument. From the CFURL reference:

对倒数第二个参数使用CFSTR("")代替NULL。从CFURL 参考

charactersToLeaveEscaped

Characters whose percent escape sequences, such as %20 for a space character, you want to leave intact. Pass NULL to specify that no percent escapes be replaced, or the empty string (CFSTR("")) to specify that all be replaced.

转义字符

要保留其百分比转义序列(例如空格字符的 %20)的字符。传递 NULL 以指定不替换任何百分比转义,或传递空字符串 (CFSTR("")) 以指定全部替换。

    NSString *encoded = @"fields=ID%2CdeviceToken";
    NSString *decoded = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (CFStringRef)encoded, CFSTR(""), kCFStringEncodingUTF8);
    NSLog(@"decodedString %@", decoded);

Prints:

印刷:

2013-03-26 21:48:52.559 URLDecoding[28794:303] decodedString fields=ID?,?deviceToken

2013-03-26 21:48:52.559 URLDecoding[28794:303] decodeString fields=ID?,?deviceToken

回答by rmaddy

Another option would be:

另一种选择是:

NSString *decoded = [encoded stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

回答by Mohammed Afsul

CFURLCreateStringByReplacingPercentEscapesUsingEncodingis deprecated in iOS 9. Use stringByRemovingPercentEncodinginstead.

CFURLCreateStringByReplacingPercentEscapesUsingEncoding在 iOS 9 中已弃用。请改用stringByRemovingPercentEncoding

NSString *decoded = [encoded stringByRemovingPercentEncoding];

回答by hashier

Swift 3

斯威夫特 3

import Foundation

let charSet = CharacterSet.urlPathAllowed.union(.urlQueryAllowed) // just use what you need, either path or query
let enc = "Test Test Test".addingPercentEncoding(withAllowedCharacters: charSet)!
let dec = enc.removingPercentEncoding!

print("Encoded: \(enc)")
print("Decoded: \(dec)")

Output:

输出:

Encoded: Test%20Test%20Test

Decoded: Test Test Test

编码:Test%20Test%20Test

解码:测试测试测试