NSString stringWithFormat

62,836

Solution 1

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

Solution 2

You need to try:

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

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

Solution 3

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.

Share:
62,836
Leo
Author by

Leo

Updated on August 05, 2022

Comments

  • Leo
    Leo almost 2 years

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

  • Leo
    Leo about 14 years
    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
    Leo about 14 years
    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
  • Ivan Carosati
    Ivan Carosati about 14 years
    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);
  • winklerrr
    winklerrr over 8 years
    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...