iOS 字符编码

iOS 9之后苹果推荐使用编码方法

[str  stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]]
  • 其中对NSCharacter​Set类就很值得研究
  • 此处用到URLPathAllowedCharacterSet并不在 NSCharacterSet.h类文件中

//    URLFragmentAllowedCharacterSet    "#%<>^`\[]{|}
//    URLPasswordAllowedCharacterSet    "#%<>^`\[]{|}/:?@
//    URLPathAllowedCharacterSet        "#%<>^`\[]{|};?
//    URLQueryAllowedCharacterSet       "#%<>^`\[]{|}
//    URLUserAllowedCharacterSet        "#%<>^`\[]/:?@
//    URLHostAllowedCharacterSet        "#%<>^`\{|}/?@
  • 对于中文、中文标点符号以及空格都是要编码的
  • 每个属性后面对应的特殊符号也都是要编码的
  • emoji表情也是都要编码的
  • 对于字母、数字英文标点符号+-/* 都直接显示不编码
  • 其中对应后面没有提到的特殊字符大都不编码
NSString *string = @"$&1111😆aaaaa#";
NSString *unicode = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
屏幕快照 2019-03-29 16.25.53.png

打印结果:$&1111%F0%9F%98%86aaaaa%23
由此可以看出表情😆 和 # 是进行编码 的其他字符是不进行编码。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。