1、场景描述
-
1.1 HTTPS拼接地址
- 如果地址含中文的话程序会crash,所以需要编码使其不包含中文。
-
1.2 文件地址为乱码
2、编码与解码
NSString *string1 = @"https://www.cloudsafe.com/文件夹";
NSString *string2 = [string1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *string3 = [string2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *string4 = [string2 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *string5 = [string3 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *string6 = [string4 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *string7 = [string5 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
CHLog(@"string1 == %@", string1)
CHLog(@"string2 == %@", string2)
CHLog(@"string3 == %@", string3)
CHLog(@"string4 == %@", string4)
CHLog(@"string5 == %@", string5)
CHLog(@"string6 == %@", string6)
CHLog(@"string7 == %@", string7)
-
2.2 测试结果
- 其中,string1 编码 2 次,成 string3,所以需要解码 2 次才看到中文 string7。
- 解码完成,连续解码对结果不会产生影响。