iOS6、7、8、9新特性总汇和适配说明 (10)

- (id)initWithBase64EncodedString:(NSString *)base64String options:(NSDataBase64DecodingOptions)options;

 

- (NSString *)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options;

 

- (id)initWithBase64EncodedData:(NSData *)base64Data options:(NSDataBase64DecodingOptions)options;

 

- (NSData *)base64EncodedDataWithOptions:(NSDataBase64EncodingOptions)options;

 

[Format Time: 0.0021 seconds]

这些方法可以帮助你轻易的将NSData对象转化为Base64,或者将Base64转化为NSData object。见以下的例子:

Crayon Syntax Highlighter v2.7.1

NSData* sampleData = [@"Some sample data" dataUsingEncoding:NSUTF8StringEncoding];

 

NSString * base64String = [sampleData base64EncodedStringWithOptions:0];

NSLog(@"Base64-encoded string is %@", base64String); // prints "U29tZSBzYW1wbGUgZGF0YQ=="

 

NSData* dataFromString = [[NSData alloc] initWithBase64EncodedString:base64String options:0];

NSLog(@"String is %@",[NSString stringWithUTF8String:[dataFromString bytes]]); // prints "String is Some sample data"

1

2

3

4

5

6

7

 

NSData* sampleData = [@"Some sample data" dataUsingEncoding:NSUTF8StringEncoding];

 

NSString * base64String = [sampleData base64EncodedStringWithOptions:0];

NSLog(@"Base64-encoded string is %@", base64String); // prints "U29tZSBzYW1wbGUgZGF0YQ=="

 

NSData* dataFromString = [[NSData alloc] initWithBase64EncodedString:base64String options:0];

NSLog(@"String is %@",[NSString stringWithUTF8String:[dataFromString bytes]]); // prints "String is Some sample data"

 

[Format Time: 0.0025 seconds]

如果你需要支持iOS6或者更早以前的系统,你可以使用以下两个方法:

Crayon Syntax Highlighter v2.7.1

/* These methods first appeared in NSData.h on OS X 10.9 and iOS 7.0. They are deprecated in the same releases in favor of

 the methods in the <code>NSDataBase64Encoding</code> category. However, these methods have existed for several releases, so

 they may be used for applications targeting releases prior to OS X 10.9 and iOS 7.0.

 */

- (id)initWithBase64Encoding:(NSString *)base64String;

- (NSString *)base64Encoding;

1

2

3

4

5

6

 

/* These methods first appeared in NSData.h on OS X 10.9 and iOS 7.0. They are deprecated in the same releases in favor of

 the methods in the <code>NSDataBase64Encoding</code> category. However, these methods have existed for several releases, so

 they may be used for applications targeting releases prior to OS X 10.9 and iOS 7.0.

 */

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwgywp.html