IOS 数据转换

  • NSDictionary to NSData
     1   NSMutableDictionary *mDic = [[NSMutableDictionary alloc] initWithCapacity:1];
     2   [mDic setValue:@"test1" forKey:@"test1"];
     3   [mDic setValue:@"test2" forKey:@"test2"];
     4   [mDic setValue:@"test3" forKey:@"test3"];
     5   [mDic setValue:@"test4" forKey:@"test4"];
     6   [mDic setValue:@"test5" forKey:@"test5"];
     7   
     8   NSLog(@"mdic = %@", mDic);
     9   
    10   NSMutableData *mData = [[NSMutableData alloc] init];
    11   NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
    12   [archiver encodeObject:mDic forKey:@"mutDic"];
    13   [archiver finishEncoding];
    14   NSLog(@"mdata = %@", mData);

     

  • NSData to NSString
    1   NSMutableData *mData = [[NSMutableData alloc] init];
    2   NSString *mStr = [[NSString alloc] initWithData:mData encoding:NSUTF8StringEncoding];
    3   NSLog(@"mstr = %@", mStr);

     

  • NSString to NSData
    1   NSString *mStr = [[NSString alloc] initWithData:mData encoding:NSUTF8StringEncoding];
    2   NSLog(@"mstr = %@", mStr);
    3   
    4   NSData *data = [mStr dataUsingEncoding:NSUTF8StringEncoding];

     

  • NSData to NSDictionary
posted @ 2016-03-08 16:31  naray  阅读(254)  评论(0编辑  收藏  举报