encodeURIComponent equivalent object-c
encodeURIComponent equivalent object-c
我在谷歌上搜索这个时遇到了麻烦。
Objective-C是否有编码URI组件的等效方法?
http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp
这似乎是一个非常常见的用例,但我在 Objective-C 中找不到任何关于它的文档。
谢谢。
-[NSString stringByAddingPercentEscapesUsingEncoding:]
是最简单的方法。
当然,你可以选择这种方法,希望这会有所帮助。
//encode URL string
+(NSString *)URLEncodedString:(NSString *)str
{
NSString *encodedString = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)str,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
return encodedString;
}
}
//decode URL string
+(NSString *)URLDecodedString:(NSString *)str
{
NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
return decodedString;
}
现在stringByAddingPercentEscapesUsingEncoding:
已被弃用。这是相当于encodeURIComponent
的iOS 7+:
[str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]
使用 CFURLCreateStringByAddingPercentEscapes
或 stringByAddingPercentEscapesUsingEncoding
并不理想,因为这些是已弃用的方法。
戴夫的答案是正确的,但不够明确。
我们想调用stringByAddingPercentEncodingWithAllowedCharacters
但我们需要选择一个字符集。不管我们的直觉怎么说,NSCharacterSet.URLQueryAllowedCharacterSet
实际上不是一个好的选择。它引用 URL 的整个查询部分,而不仅仅是查询参数的值,因此它也允许使用 ?
或 &
等字符,这些字符在这里无效。您想要的是根据文档手动构建允许的字符集并使用它。允许encodeURIComponent()
字符为:
A–Z a–z 0–9 - _ . ! ~ * ' ( )
这为我们提供了以下解决方案:
// Note: Using NSCharacterSet.URLQueryAllowedCharacterSet is inappropriate for this purpose.
// It contains the allowed chars for the whole Query part of the URL (including '?' or '&'),
// not just for the values of query parameters (where chars are more restricted).
+ (NSString *)encodeURIComponent:(NSString *)uriComponent {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
NSString *allowedChars = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"-_.!~*'()";
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:allowedChars];
return [uriComponent stringByAddingPercentEncodingWithAllowedCharacters:charSet];
}
+ (NSString *)decodeURIComponent:(NSString *)uriComponent {
return [uriComponent stringByRemovingPercentEncoding];
}
相关文章:
- 函数未在Object.keys或Object.getOwnPropertyNames下列出,但可以调用
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- 有没有一种方法可以列出Ember.Object的所有绑定
- 如何取消object.prototypes javascript的一个函数
- 为什么是文档.旧版应用程序中的DOM-object.properties为null
- 将*.js文件的内容放入Object中
- MongoDB + SQL Select equivalent
- Object.prototype using 'this'
- 使用Object.create()的角度服务继承
- 如何使用object.assign()从其他对象引用基本对象属性
- 循环的数组推入在Object容器中具有不同的值
- reducers在redux中得到Function not Object,what'it’他错了
- JSON.stringify和Object.keys在同一个对象上产生不同的结果
- 在使用object.create创建的对象中使用super
- Join架构验证:Join.object定义数组中的有效键
- jquery打印[object XMLDocument]而不是文件内容
- Jframe equivalent for javascript
- encodeURIComponent equivalent object-c
- Dojo Equivalent for Object.keys
- Python equivalent of JavaScript's `Object.defineProperty