PDA

View Full Version : extern NSString passed between classes changes value?




stjohnbee
Mar 31, 2009, 09:53 AM
Hi guys,

I have a variable which is declared as...

extern NSString *savedUid;

..in a .h file. Then, in one of my classes the value is set. When I try to access it from another class it does something odd. The value is set to 'keroack' in one class, then in the second class I set the navigation.title to its value and it looks as you would expect. Then, on the very next line I tried to use it to build up another string and got a string of numbers and other characters...


// Works as expected, setting title to 'keroack'
self.navigationItem.title = savedUid;

// Prints a strange string to the console... '\240r\240\214'
printf("%s \n", savedUid);

Also, when I look at the variable in the debugger it has changed from type NSString to type NSCFstring.

I could understand if it was consistent, but it sets the title to the correct string.

Does anyone know what might be causing this strange (ie. not expected by me but probably complete correct) behaviour? And how I can use the value in another string like...

NSString * filter = [[NSString alloc] initWithString:(NSString *)savedUid];


Without getting an error like...

[NSPlaceholderString stringWithFormat:]: unrecognized selector sent to instance 0x512d50


Many thanks
Ade



dejo
Mar 31, 2009, 10:22 AM
I'm pretty sure you can't printf an NSString without doing something to it first. Instead try:
NSLog(@"%@", savedUid);

jnic
Mar 31, 2009, 10:46 AM
Prints a strange string to the console...

Are you retaining it anywhere? If it's printing a "strange string", it's likely that the memory's been overwritten by something else.

admanimal
Mar 31, 2009, 10:59 AM
Dejo is right, printf with the format specifier %s is mean to print a C string (i.e. char *) not an NSString.

ghayenga
Mar 31, 2009, 05:09 PM
Dejo is right, printf with the format specifier %s is mean to print a C string (i.e. char *) not an NSString.

And all NSString are NSCFString internally.

stjohnbee
Apr 2, 2009, 03:34 AM
Might not be ideal, but I used a char * instead and that moved around classes without a problem. Not perfect, but a solution.

dejo
Apr 2, 2009, 09:46 AM
Might not be ideal, but I used a char * instead and that moved around classes without a problem. Not perfect, but a solution.
Because when you used an NSString it was changing the value whenever "moved around classes"? (Not exactly sure what that means even).