views:

295

answers:

4

Hi Guys,

I don't know what I am missing here. I am trying to concatenate strings using NSString stringWithFormat function. This is what I am doing.

NSString *category = [row objectForKey:@"category"];
NSString *logonUser = [row objectForKey:@"username"];
user.text = [NSString stringWithFormat:@"In %@ by %@", category, logonUser];

The problem here is that it always print only one variable. Say if there is "Sports" in category and "Leo" in logonUser it will print "In Sports" and skip the remaining text. It should print "In Sports by Leo".

Thanks

A: 

Whats the point for the first line in this code? It seems unrelated to the 3rd line?

Are you 100% sure that both category and logonUser are populated in the code? Perhaps put a NSLog statement right after the user.text = line and make sure they have the values you expect because your 3rd line looks fine.

Edit

I would try changing

user.text = [NSString stringWithFormat:@"In %@ by %@", category, logonUser];

to

user.text = [NSString stringWithFormat:@"In %@ by %@", @"category", @"logonUser"];

and see if that outputs In category by logonUser. Because it sure looks correct to me.

jamone
Sorry about the mistake. Category declaration is below. I accidentally copied the wrong line. NSString *category = [row objectForKey:@"category"];Yes, I have tried NSLog and they are populated. Any idea?
Leo
+4  A: 

Is user a UILabel? Make sure that your text isn't wrapping or being clipped. Try making the UILabel bigger.

lucius
A: 

The code looks correct:

By any chance are u getting a carriage return or extra white space in the category variable? In case of a small label, it may not display the full string. Try swapping the two variables in the third line and see what the output is.

I am baffled that even the "by" is missing from the output. I have a feeling that the value of the category variable is masking the text.

Bharat Ahluwalia
A: 

You need to try:

NSlog(@"In %@ by %@", category, logonUser);

To check the problem! Let me know the results on debugger console XD

Pokono
Good suggestion. I tried NSLog and it is being printed in two lines. I think category has carriage return. How can I remove the carriage return and unnecessary spaces around?Thanks
Leo
Sorry for the late reply!You can use this --> - (NSString *)substringWithRange:(NSRange)aRangeAnd for range you can do this --> NSRange *myRange = NSRangeMake(x,y);
Pokono