一. Base64 编码
Base64 是许多 web 协议的标准,在日常开发中很多地方会需要利用 Base64 进行编解码的操作;
在相互转换操作的时候,可以借用 NSData 来执行,例如接口中提供的如下方法:
/* Create an NSData from a Base-64 encoded NSString using the given options. By default, returns nil when the input is not recognized as valid Base-64.
*/
- (nullable instancetype)initWithBase64EncodedString:(NSString *)base64String options:(NSDataBase64DecodingOptions)options API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
/* Create a Base-64 encoded NSString from the receiver's contents using the given options.
*/
- (NSString *)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
其中,第一个方法用来编码时操作,官方大致意思为将一个字符串(NSString)类型的参数通过编码后转换为 NSData 类型,若所输入的数据未能被识别为有效 Base-64 时,则返回 nil.
第二个方法是将 NSData 类型数据通过 Base-64 的方式解码为字符串类型参数.
具体使用方式 code 如下:
1.Data 转 String
NSData *data = [[NSData alloc] initWithBase64EncodedString:@"Encoded String"
options:0];//编码
NSString *decodeStr = [data base64EncodedStringWithOptions:0];//解码
NSLog(@"data --- %@/n decodeStr --- %@", data, decodeStr);
2.字符串与 Data 间相互转换
NSString *encodeStr = @"Encode String Test";
NSData *encodeData = [[encodeStr dataUsingEncoding:NSUTF8StringEncoding] base64EncodedDataWithOptions:0];// 编码
NSData *decodeData = [[NSData alloc] initWithBase64EncodedData:encodeData
options:0];// 解码
NSString *decodeStr = [[NSString alloc] initWithData:decodeData
encoding:NSUTF8StringEncoding];// Data 转 String
NSLog(@"编码:\n%@解码:\n%@Data to change String:\n%@", encodeData, decodeData, decodeStr);
二.百分号编码
百分号编码对 web 协议也很重要,尤其是对 URL 的处理上;
其中官方 API 提供的接口方法如下:
// Returns a new string made from the receiver by replacing all percent encoded sequences with the matching UTF-8 characters.
@property (nullable, readonly, copy) NSString *stringByRemovingPercentEncoding API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
// 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.
- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
其中,第一项为将输入的字符串内容通过百分号编码序列后生成一个编码后的新字符串;
第二项与第一项类似,同样是将输入的字符串内容通过百分号编码序列的方式编码后生成一个新的字符串,但该方法允许开发者控制需要百分号编码的字符,目的是成一个 URL 字符串编码组件或子组件,而不是整个 URL 字符串.在7位 ASCII 范围外的任何字符被忽略.
具体使用方式 code 如下:
NSString *testStr = @"Percent Test";
NSString *encodStr = [testStr stringByRemovingPercentEncoding];// 编码
NSString *encodCharacters = [testStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];// 编码并控制所需百分号编码的字符
NSLog(@"\nPercentEncoding:\n%@\nPercentEncodingAndCharacters:\n%@", encodStr, encodCharacters);
打印结果如下: