Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stjohnbee

macrumors newbie
Original poster
Mar 25, 2009
7
0
Hi guys,

I have a variable which is declared as...

Code:
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...

Code:
// 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...

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


Without getting an error like...

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


Many thanks
Ade
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
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

macrumors 68040
Apr 22, 2005
3,531
2
Dejo is right, printf with the format specifier %s is mean to print a C string (i.e. char *) not an NSString.
 

stjohnbee

macrumors newbie
Original poster
Mar 25, 2009
7
0
Might not be ideal, but I used a char * instead and that moved around classes without a problem. Not perfect, but a solution.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.