NSData NSDate NSString NSArray NSDictionary 相互转化

    1. //    NSData  NSDate NSString NSArray NSDictionary json
    2. NSString *string = @"hello word";
    3. NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    4. NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
    5. NSString *dateString = @"19891123";
    6. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
    7. @"hello", @"word",
    8. @"chen", @"lu",
    9. @"jin", @"mei",
    10. @"cerastes", @"hehe",nil];
    11. //    NSString to NSData
    12. NSData *string2data = [string dataUsingEncoding:NSUTF8StringEncoding];
    13. NSLog(@"%@",string2data);
    14. //    NSData to NSString
    15. NSString *data2String = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    16. NSLog(@"dataString = %@",data2String);
    17. //    NSArray to NSData
    18. NSData *arry2data = [NSKeyedArchiver archivedDataWithRootObject:array];
    19. //    NSData to NSArray
    20. NSArray *data2arry = [NSKeyedUnarchiver unarchiveObjectWithData:arry2data];
    21. NSLog(@"%@  /n %@",arry2data,data2arry);
    22. //    NSString to NSDate
    23. NSDate *string2date = [self convertDateFromString:dateString withFormattre:@"yyyyMMdd"];
    24. NSLog(@"date = %@",string2date);
    25. //    NSDate to NSString
    26. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    27. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    28. NSString *date2string = [dateFormatter stringFromDate:[NSDate date]];
    29. NSLog(@"%@", date2string);
    30. //   NSArray to NSString
    31. NSString *arry2String = [array componentsJoinedByString:@" "];
    32. NSLog(@"string = %@",arry2String);
    33. //    NSString to NSArray
    34. NSArray *string2Arry = [arry2String componentsSeparatedByString:@" "];
    35. NSLog(@" %@",string2Arry);
    36. //    NSDictionary to NSData:
    37. NSMutableData *dic2data = [[NSMutableData alloc] init];
    38. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dic2data];
    39. [archiver encodeObject:dic forKey:@"Some Key Value"];
    40. [archiver finishEncoding];
    41. NSLog(@"%@",dic2data);
    42. //    NSData to NSDictionary
    43. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:dic2data];
    44. NSDictionary *data2dic = [unarchiver decodeObjectForKey:@"Some Key Value"];
    45. [unarchiver finishDecoding];
    46. NSLog(@"%@",data2dic);
    47. //    NSDictionary to NSString
    48. NSString *dic2string=[dic JSONRepresentation]; //这个方法是SBJson类库里的,需要导入SBJson.h
    49. NSLog(@"%@",dic2string);
    50. //    NSString to NSDictionary
    51. NSError *error = nil;
    52. NSDictionary *string2dic = [NSJSONSerialization JSONObjectWithData: [dic2string dataUsingEncoding:NSUTF8StringEncoding]
    53. options: NSJSONReadingMutableContainers
    54. error: &error];
    55. NSLog(@"%@",string2dic);
时间: 2024-10-15 01:35:53

NSData NSDate NSString NSArray NSDictionary 相互转化的相关文章

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]; N

Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区

Fouandation 中常见的理解错误区 1.NSString //快速创建(实例和类方法) 存放的地址是 常量区 NSString * string1 = [NSString alloc]initWithString:@“123”]; NSString * string3 = [NSString stringWithString:@“123”]; //格式化方法创建   存放地址是堆区 NSString * string2 = [NSString alloc]initWithFormat:@

子做嘴柞兹谞wisvfsNSDictionary,NSArray转成NSData(JSON)NSString类型

ufifgbNSDictionary,NSArray转成NSData(JSON)NSString类型NSDictionary,NSArray转成NSData(JSON)NSString类型纂诅租灼柞谞 v1795qp.cn.ec51.com/ 592r727.cn.ec51.com/ f31b6ca.cn.ec51.com/ s48ctk5.cn.ec51.com/ s9d9u37.cn.ec51.com/ r8jo5n7.cn.ec51.com/ p813g69.cn.ec51.com/ 4a

NSString,NSArray和NSDictionary to JSON标准格式字符串

#import <Foundation/Foundation.h> @interface NSString (HXAddtions) +(NSString *) jsonStringWithDictionary:(NSDictionary *)dictionary; +(NSString *) jsonStringWithArray:(NSArray *)array; +(NSString *) jsonStringWithString:(NSString *) string; +(NSStr

oc/object-c/ios哪种遍历NSArray/NSDictionary方式快?测试报告

做app的时候,总免不了要多次遍历数组或者字典.究竟哪种遍历方式比较快呢?我做了如下测试:首先定义测试用宏: ? 1 2 3 4 5 6 7 8 9 #define MULogTimeintervalBegin(INFO) NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];\ NSTimeInterval duration = 0;\ NSLog(@"MULogTimeintervalBegin:%@", IN

Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage

1,NSData 与 NSString NSData --> NSString NSString *aString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding]; NSString --> NSData NSString *aString = @"1234"; NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncod

NSArray / NSDictionary 转 Json

在iOS开发中,网络数据转换是必不可少的,我们时常会用到NSArray / NSDictionary转化成Json字符串. 网上看到很多都是借助于第三方去转化,就个人而言,我认为三方的东西一方面增加了冗余度,另一方面时常更新,比较头疼. 仔细看看了苹果自带的json序列化解析器,苹果提供了字典和数组转化Json字符串的方法. NSDictionary * dict = [[NSDictionary alloc]initWithObjectsAndKeys:@"张三",@"na

Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法

Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法 从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性.创建数组NSArray,字典NSDictionary, 数值对象NSNumber时,可以像NSString的初始化一样简单方便.妈妈再也不担心程序写得手发酸了. A.   NSArray 首先是非常常用的NSArray,NSMutableArray.NSArray是一个初始化后就固定的静态数组.如果

Read and Write NSArray, NSDictionary and NSSet to a File

查询地址:http://iosdevelopertips.com/data-file-management/read-and-write-nsarray-nsdictionary-and-nsset-to-a-file.html With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading both