- NSString 转 NSData:
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
- NSData 转 NSString:
NSString *originString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSArray 转 NSData
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
- NSData 转 NSArray
NSArray *originArray =[NSKeyedUnarchiver unarchiveObjectWithData:data];
- NSArray 转 NSString
NSString *string = [array componentsJoinedByString:@"#"];
- NSString 转 NSArray
NSArray *originArray = [string componentsSeparatedByString:@"#"];
1.NSString 与 NSData 的互相转换
- NSString 转 NSData:
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *string = @"This is a string!";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"The string is: %@",string);
NSLog(@"The data is: %@",data);
输出结果:
The string is: This is a string!
The data is: <54686973 20697320 61207374 72696e67 21>
- NSData 转 NSString:
NSString *originString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *string = @"This is a string!";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *originString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"The string is: %@",string);
NSLog(@"The data is: %@",data);
NSLog(@"The origin string is: %@",originString);
输出结果:
The string is: This is a string!
The data is: <54686973 20697320 61207374 72696e67 21>
The origin string is: This is a string!
2.NSArray 与 NSData 的互相转换
- NSArray 转 NSData:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSArray *array = @[@"testA", @"testB", @"testC"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSLog(@"The array is: %@",array);
NSLog(@"The data is: %@",data);
输出结果:
The array is: (
testA,
testB,
testC
)
The data is: <62706c69 73743030 d4010203 04050619 1a582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f701200 0186a0a6 07081011 12135524 6e756c6c d2090a0b 0f5a4e53 2e6f626a 65637473 5624636c 617373a3 0c0d0e80 02800380 04800555 74657374 41557465 73744255 74657374 43d21415 16175a24 636c6173 736e616d 65582463 6c617373 6573574e 53417272 6179a216 18584e53 4f626a65 63745f10 0f4e534b 65796564 41726368 69766572 d11b1c54 726f6f74 80010811 1a232d32 373e4449 545b5f61 6365676d 73797e89 929a9da6 b8bbc000 00000000 00010100 00000000 00001d00 00000000 00000000 00000000 0000c2>
- NSData 转 NSArray:
NSArray *originArray =[NSKeyedUnarchiver unarchiveObjectWithData:data];
NSArray *array = @[@"testA", @"testB", @"testC"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSArray *originArray =[NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"The array is: %@",array);
NSLog(@"The data is: %@",data);
NSLog(@"The origin array is: %@",originArray);
输出结果:
The array is: (
testA,
testB,
testC
)
The data is: <62706c69 73743030 d4010203 04050619 1a582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f701200 0186a0a6 07081011 12135524 6e756c6c d2090a0b 0f5a4e53 2e6f626a 65637473 5624636c 617373a3 0c0d0e80 02800380 04800555 74657374 41557465 73744255 74657374 43d21415 16175a24 636c6173 736e616d 65582463 6c617373 6573574e 53417272 6179a216 18584e53 4f626a65 63745f10 0f4e534b 65796564 41726368 69766572 d11b1c54 726f6f74 80010811 1a232d32 373e4449 545b5f61 6365676d 73797e89 929a9da6 b8bbc000 00000000 00010100 00000000 00001d00 00000000 00000000 00000000 0000c2>
The origin array is: (
testA,
testB,
testC
)
3.NSArray 与 NSString 的互相转换
- NSArray 转 NSString:
NSString *string = [array componentsJoinedByString:@"#"];
NSArray *array = @[@"testA", @"testB", @"testC"];
NSString *string = [array componentsJoinedByString:@"#"];
NSLog(@"The array is: %@",array);
NSLog(@"The string is: %@",string);
输出结果:
The array is: (
testA,
testB,
testC
)
The string is: testA#testB#testC
- NSString 转 NSArray:
NSArray *originArray = [string componentsSeparatedByString:@"#"];
NSArray *array = @[@"testA", @"testB", @"testC"];
NSString *string = [array componentsJoinedByString:@"#"];
NSArray *originArray = [string componentsSeparatedByString:@"#"];
NSLog(@"The array is: %@",array);
NSLog(@"The string is: %@",string);
NSLog(@"The origin array is: %@",originArray);
输出结果:
The array is: (
testA,
testB,
testC
)
The string is: testA#testB#testC
The origin array is: (
testA,
testB,
testC
)