• 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);
    


  • 相关阅读:
    【Java技术系列】爱情36技之暗送秋波的技术
    【Java技术系列】爱情36技之追美妹的技术
    设计模式(五)——建造者模式
    设计模式(四)——一文搞清楚三种工厂模式
    设计模式(三)—— 工厂方法模式
    设计模式(二)—— 简单工厂模式
    设计模式实战——开发中常用到的单例模式
    设计模式(一)—— 单例模式
    设计模式(开篇)—— 设计模式概述
    设计模式系列博客传送门
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5251894.html
Copyright © 2020-2023  润新知