iOS 9.0以降NSString encodeメソッドの置き換え
2886 ワード
iOS 9.0以降、以前よく使われていた
'stringByAddingPercentEscapesUsingEncoding:' is deprecated: first deprecated in iOS 9.0 - Use -stringByAddingPercentEncodingWithAllowedCharacters: instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.
強迫症のある優秀なプログラム猿として、我慢できないことを示しています!
文書には次のように書かれています.
よくわかりました.
この方法の公式ドキュメントはこう言います.
//Returns a new string made from the receiver by replacing all characters not in the allowedCharacters set with percent encoded characters. UTF-8 encoding is used to determine the correct percent encoded characters. Entire URL strings cannot be percent-encoded. This method is intended to percent-encode an URL component or subcomponent string, NOT the entire URL string. Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.
最後の
言い換えれば、上記の方法は、urlの7-bit以外のASCII文字を処理時に符号化し、これらの[`#%^{}[]|<>]のように、無視する必要がある場合は
注意:文字セットは最後にスペースです! 文字セットとは、文字列に文字セットが含まれている文字が符号化されないことを意味します.
また、URLでよく使用される
以上、まだ何を待っているのか、グローバル検索に置き換えましょう、消滅⚠️!
私の個人のブログを収集することができることが好きです:RobberJJ
NSString
符号化方法stringByAddingPercentEscapesUsingEncoding:
が廃棄され、プロジェクトには次のようなものが山積みになる可能性があります.⚠️: 'stringByAddingPercentEscapesUsingEncoding:' is deprecated: first deprecated in iOS 9.0 - Use -stringByAddingPercentEncodingWithAllowedCharacters: instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.
強迫症のある優秀なプログラム猿として、我慢できないことを示しています!
文書には次のように書かれています.
- (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc
NS_DEPRECATED(10_0, 10_11, 2_0, 9_0, "Use -stringByAddingPercentEncodingWithAllowedCharacters: instead,
which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.");
よくわかりました.
stringByAddingPercentEncodingWithAllowedCharacters:
の方法で置き換えます.この方法の公式ドキュメントはこう言います.
//Returns a new string made from the receiver by replacing all characters not in the allowedCharacters set with percent encoded characters. UTF-8 encoding is used to determine the correct percent encoded characters. Entire URL strings cannot be percent-encoded. This method is intended to percent-encode an URL component or subcomponent string, NOT the entire URL string. Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.
最後の
Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.
は、7-bit以外のASCII文字をallowedCharacters
に入れても無視されます.つまり、allowedCharacters
の文字と7-bit ASCII文字は符号化されません.言い換えれば、上記の方法は、urlの7-bit以外のASCII文字を処理時に符号化し、これらの[`#%^{}[]|<>]のように、無視する必要がある場合は
(NSCharacterSet *)allowedCharacters
というパラメータで指定する必要がある.[aString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//
[aString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "]];
注意:
また、URLでよく使用される
NSCharacterSet
型は分類NSCharacterSet (NSURLUtilities)
に定義され、文字セットは以下のように含まれる.URLHostAllowedCharacterSet "#%/<>?@\^`{|}
URLFragmentAllowedCharacterSet "#%<>[\]^`{|}
URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet "#%<>[\]^`{|}
URLUserAllowedCharacterSet "#%/:<>?@[\]^`
以上、まだ何を待っているのか、グローバル検索に置き換えましょう、消滅⚠️!
私の個人のブログを収集することができることが好きです:RobberJJ