16

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".

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Leo
  • 1,547
  • 3
  • 24
  • 40

4 Answers4

15

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

lucius
  • 8,665
  • 3
  • 34
  • 41
6

You need to try:

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

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

koen
  • 5,383
  • 7
  • 50
  • 89
Ivan Carosati
  • 355
  • 4
  • 13
  • 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 May 01 '10 at 10:39
  • Sorry for the late reply! You can use this --> - (NSString *)substringWithRange:(NSRange)aRange And for range you can do this --> NSRange *myRange = NSRangeMake(x,y); – Ivan Carosati May 06 '10 at 03:54
1

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.

  • Your answer covers all the problems the thread starter ran into. I don't know why your answer isn't marked as correct answer or at least upvoted... – winklerrr Nov 23 '15 at 06:46
0

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
  • 17,253
  • 17
  • 63
  • 98
  • 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 Apr 29 '10 at 20:04