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];
网友评论