NSData NSDate NSString NSArray NSDictionary 相互转换
// NSData NSDate NSString NSArray NSDictionary json
NSString *string = @"hello word";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
NSString *dateString = @"19891123";
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
@"hello", @"word",
@"chen", @"lu",
@"jin", @"mei",
@"cerastes", @"hehe",nil]; // NSString to NSData
NSData *string2data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",string2data);
// NSData to NSString
NSString *data2String = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"dataString = %@",data2String); // NSArray to NSData NSData *arry2data = [NSKeyedArchiver archivedDataWithRootObject:array];
// NSData to NSArray
NSArray *data2arry = [NSKeyedUnarchiver unarchiveObjectWithData:arry2data];
NSLog(@"%@ /n %@",arry2data,data2arry); // NSString to NSDate
NSDate *string2date = [self convertDateFromString:dateString withFormattre:@"yyyyMMdd"];
NSLog(@"date = %@",string2date); // NSDate to NSString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *date2string = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", date2string); // NSArray to NSString NSString *arry2String = [array componentsJoinedByString:@" "]; NSLog(@"string = %@",arry2String); // NSString to NSArray
NSArray *string2Arry = [arry2String componentsSeparatedByString:@" "];
NSLog(@" %@",string2Arry); // NSDictionary to NSData:
NSMutableData *dic2data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dic2data];
[archiver encodeObject:dic forKey:@"Some Key Value"];
[archiver finishEncoding];
NSLog(@"%@",dic2data); // NSData to NSDictionary
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:dic2data];
NSDictionary *data2dic = [unarchiver decodeObjectForKey:@"Some Key Value"];
[unarchiver finishDecoding];
NSLog(@"%@",data2dic); // NSDictionary to NSString
NSString *dic2string=[dic JSONRepresentation]; //这个方法是SBJson类库里的,需要导入SBJson.h
NSLog(@"%@",dic2string); // NSString to NSDictionary
NSError *error = nil;
NSDictionary *string2dic = [NSJSONSerialization JSONObjectWithData: [dic2string dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: &error];
NSLog(@"%@",string2dic);
// 将数组转为json 序列化 NSArray *body = @[@"1",@"2"]; NSData *data = [NSJSONSerialization dataWithJSONObject:body
options:NSJSONWritingPrettyPrinted error:nil];
NSData NSDate NSString NSArray NSDictionary 相互转换的更多相关文章
- NSData NSDate NSString NSArray NSDictionary 相互转化
// NSData NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; ...
- Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区
Fouandation 中常见的理解错误区 1.NSString //快速创建(实例和类方法) 存放的地址是 常量区 NSString * string1 = [NSString alloc]init ...
- iOS NSDictionary、NSData、JSON等 数据类型相互转换
1.NSDictionary类型转换为NSData类型: NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @&qu ...
- Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage
1,NSData 与 NSString NSData --> NSString NSString *aString = [[NSString alloc] initWithData:adata ...
- (转)Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage
,NSData 与 NSString NSData --> NSString NSString *aString = [[NSString alloc] initWithData:adata e ...
- Read and Write NSArray, NSDictionary and NSSet to a File
查询地址:http://iosdevelopertips.com/data-file-management/read-and-write-nsarray-nsdictionary-and-nsset- ...
- Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法
Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法 从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性. ...
- 解决NSData转NSString返回nil的问题
// 字符串转Data NSString *str =@"jesfds"; NSData *data =[str dataUsingEncoding:NSUTF8StringEnc ...
- iOS解决NSData转NSString后字符为空
iOS中,将NSData转NSString的一般方法为[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];但是当dat ...
随机推荐
- 作业一直"执行"
背景:一个作业有7个步骤,前面的步骤成功/失败都转到下一步,直至最后退出,作业计划是每天早上8点执行.步骤中的语句是例行检查脚本,之前的历史记录都是一分钟内完成.此次重启数据库服务器后,检查发现此作业 ...
- OSG绘制几何图形
在OSGMFC程序基础上修改OSG_MFC类的方法,如下: void cOSG::InitSceneGraph(void) { // Init the main Root Node/Group mRo ...
- hadoop 文件系统API操作
配置参数:-DHADOOP_USER_NAME=hadoop public class HdfsUtils { private static FileSystem fileSystem; @Befor ...
- iOS让数组内对象执行同一方法
// 让数组中的所有对象都执行removeFromSuperview方法 [self.answerView.subviews makeObjectsPerformSelector:@selector( ...
- spring 定时任务标注
使用spring框架,需要定时任务只需要在方法上加@Component 就可以了 package hello; import java.text.SimpleDateFormat; import ja ...
- 线性空间光照(即Gamma)
在渲染管线中正确使用Gamma校正是决定渲染效果的一个非常重要的因素,现在商业引擎包括很多国内引擎都已经是在线性空间计算光照.当然也包括我们之前公司设计的引擎.关于gamma校正的定义可以查看wiki ...
- Java基础之创建窗口——使窗口在屏幕居中(TryWindow2/TryWindow3)
控制台程序. 1.使用ToolKit对象在屏幕的中心显示窗口,将窗口的宽度和高度设置为屏幕的一半: import javax.swing.JFrame; import javax.swing.Swin ...
- pgbouncer+pg(fdw)+pg(datanode)分表方案
pgbouncer+pg(fdw)+pg(datanode)分表方案 (环境RHEL6.5,PG9.4.5,pgbouncer1.5.4,libevent2.0.22) 方案架构图如下: pgboun ...
- .NET: WPF DependencyProperty
DependencyProperty and DependencyObject is the core of WPF data binding. We can use this two class t ...
- C++之路起航——标准模板库(vector)
vector(动态数组或向量):动态分配内存空间的线性储存结构. 需要包括头文件<vector> 定义: vector<数据类型> 变量名: Eg: vector<int ...