NSString 转 NSData
- NSString -> NSData
NSString *string;
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
- NSData -> NSString
NSData *data;
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString 转 NSURL
- NSString -> NSURL
NSString *string;
NSURL *url = [NSURL URLWithString:string];
- NSURL -> NSString
NSURL *url;
NSString *string = [url absoluteString];
NSString 转 NSNumber
- NSString -> NSNumber
NSString *string;
NSNumber *num = @([string integerValue]);
- NSNumber -> NSString
NSNumber *num;
NSString *string = [num stringValue];
NSString 转 Char *
- NSString -> Char *
NSString *string;
const char *c = [string cStringUsingEncoding:NSUTF8StringEncoding];
- Char * -> NSString
const char *c;
NSString *string = [NSString stringWithUTF8String:c];
// or
NSString *string = [NSString stringWithCString:c encoding:NSUTF8StringEncoding];
NSString 转 NSInterger
- NSString -> NSInterger
NSString *string;
NSInteger i = [string integerValue];
- NSInterger -> NSString
NSInteger i;
NSString *string = [@(i) stringValue];
// or
NSString *string = [NSString stringWithFormat:@"%ld", i];
NSData 转 UIImage
- NSData -> UIImage
UIImage *image;
NSData *data = UIImagePNGRepresentation(image);
// or
NSData *data = UIImageJPEGRepresentation(image, 1);
- UIImage -> NSData
NSData *data;
UIImage *image = [[UIImage alloc] initWithData:data];
NSData 转 NSDictionary
- NSData -> NSDictionary
NSData *data;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
- NSDictionary -> NSData
NSDictionary *dict;
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
NSData 转 NSArray
- NSData -> NSArray
NSData *data;
NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
- NSArray -> NSData
NSArray *arr;
NSData *data = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:nil];
暂时想到的就这些,之后会继续补充~