美文网首页
iOS (NSData、NSString、NSDictionar

iOS (NSData、NSString、NSDictionar

作者: 木马不在转 | 来源:发表于2017-07-10 14:48 被阅读193次
    1.NSData转换为NSString、NSDictionary和NSArray
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];   
    
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  
    NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];  
    
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];  
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];   
    
    2.NSString、NSDictionary和NSArray转换为NSData
    NSString *string = @"I am a String";    
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];  
    
    NSDictionary *dic = @{@"name": @"Jerry", @"age": @"24"};  
    NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];  
    
    NSArray *array = [[NSArray alloc]initWithObjects:@"0",@"1",nil];  
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];  
    

    相关文章

      网友评论

          本文标题:iOS (NSData、NSString、NSDictionar

          本文链接:https://www.haomeiwen.com/subject/oroyhxtx.html