将string转换成UTF8在进行请求
在請求服務器時,如果參數中帶有中文字符。就會報參數格式錯誤,需要將其轉換成UTF8
?
@interface NSString (NSURLUtilities)
?
/* Adds all percent escapes necessary to convert the receiver into a legal URL string.? Uses the given encoding to determine the correct percent escapes (returning nil if the given encoding cannot encode a particular character).? See CFURLCreateStringByAddingPercentEscapes in CFURL.h for more complex transformations
?*/
- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc;
?
/* Replaces all percent escapes with the matching characters as determined by the given encoding.? Returns nil if the transformation is not possible (i.e. the percent escapes give a byte sequence not legal in the given encoding).? See CFURLCreateStringByReplacingPercentEscapes in CFURL.h for more complex transformations
?*/
- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)enc;
?
@end
代碼如下:
? ? ? ? ? ? ? ??@"keyword": [searchSoft.text?stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
轉載于:https://www.cnblogs.com/superchao8/p/3326721.html
總結
以上是生活随笔為你收集整理的将string转换成UTF8在进行请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [原]unity3d ios平台内存优化
- 下一篇: 浙大pat1009题解