zoukankan      html  css  js  c++  java
  • 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];

  • 相关阅读:
    uva 1374 快速幂计算
    uva 1343 非原创
    uva 11212
    uva 10603
    路径寻找问题……!
    bzoj 1008: [HNOI2008]越狱
    bzoj 1010: [HNOI2008]玩具装箱toy
    dp斜率优化小计
    bzoj 1002[FJOI2007]轮状病毒
    hihocoder #1114
  • 原文地址:https://www.cnblogs.com/AlvinCrash/p/5379446.html
Copyright © 2011-2022 走看看