Why can't this NSString created from an NSData object show that it has content?

Why does the following code create a registry below? Here's the anomaly: my second NSLog should print chrStr, but it doesn't produce anything, empty, which is checked by this debug command:

(gdb) po chrStr

object returns an empty description

However, the third NSString, where I re-convert the NSString back to an NSData object, displays the data, the same value as the first NSLog, as it should be. This indicates that chrStr should have the actual contents. But this does not seem to apply to the NSLOG or the po command. Why?


NSString *login;   
NSString *pass;


// Purpose: NSString *loginString = [NSString stringWithFormat:@"\000%@\000%@", login, pass];    
login = @"Loginname";       // text string1
pass = @"Password";         // text string2

// convert text strings to data objects
NSData *subData1 = [login dataUsingEncoding:NSUTF8StringEncoding];  
NSData *subData2 = [pass dataUsingEncoding:NSUTF8StringEncoding];   

// embed a NULL into new NSData object
NSMutableData *data = [NSMutableData data];
unsigned char zeroByte = 0;
[data appendBytes:&zeroByte length:1];

// append string1, NULL, string2 to data object
[data appendData:subData1];
[data appendBytes:&zeroByte length:1];
[data appendData:subData2];
NSLog(@"1.NSData:  %@", data);                  // print data object

// create a character string from data object
NSString *chrStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"2.NSString:  %@", chrStr);              // print character string

// create data object from string object
NSData *chrData = [chrStr dataUsingEncoding:NSUTF8StringEncoding]; 
NSLog(@"3.NSDATA:  %@", chrData);               // print data object

Gives: [1071: 207] 1.NSData: 004c6f67 696e6e61 6d650050 61737377 6f7264

[1071: 207] 2.NSString:

[1071: 207] 3.NSDATA: 004c6f67 696e6e61 6d650050 61737377 6f7264


. chrStr , 3-NSDATA , !

? , ://:

, uncommented , , , . , . , , , @ "\ 000% @\000% @", , . .

+5
1

C ( objective-c) . , , , . , . , , , , . , .

+3

All Articles