标签:
//1. NSMutableArray和NSArray互转
// NSArray转为NSMutableArray
NSMutableArray *arrM = [arr mutableCopy]; //方法1
NSMutableArray *arrM = [NSMutableArray arrayWithArray:arr]; //方法2
// NSMutableArray转为NSArray
NSArray *arr = [arrM copy]; //方法1
NSArray *arr = [NSArray arrayWithArray: arrM]; //方法2
//2. NSData和NSString互转
// NSData转为NSString
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSString转为NSData对象
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//3. NSString和NSURL互转
// NSString转为NSURL
NSString *str = @"http://www.baidu.com/天气/20160315"; //含有中文字符的str需要先编码,才能转为url
NSString *urlStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
// NSURL转为NSString
NSString *urlStr = [url absoluteString];
NSString *str = [urlStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //将编码的字转换成汉字
//4. 字典和模型互转
// KVC将字典转为模型
[model setValuesForKeysWithDictionary:dict];
// KVC将模型转为数组
NSDictionary *dict = [model dictionaryWithValuesForKeys:@[@"name",@"age",@"dog"] ];
//5. NSArray和NSData互转
// NSArray转为NSData
NSData *data = [NSKeyedArchiver archivedDataWithRootObject: array];
// NSData转为NSArray
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData: data];
标签:
原文地址:http://www.cnblogs.com/stevenwuzheng/p/5466404.html