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

1
  • Where's the declaration for category? Make sure it's a NSString* and not something else. Commented Apr 29, 2010 at 19:40

4 Answers 4

15

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

Sign up to request clarification or add additional context in comments.

Comments

6

You need to try:

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

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

2 Comments

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
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);
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.

1 Comment

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

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.