0

I would like to add a predefined string to the object of an NSMutableArray. I thought you would use %@, but apparently the code below does not execute well. Thank You in advance

arrayData = [[NSMutableArray alloc]
               initWithObjects:@"%@ you look tired." name,
                               @"Why do you smell so bad?",
                               @"I have to go potty!",
                               @"%@ put your pants on!" name,
                               @"Mommy!",
                               @"Daddy!",
                               @"NOOOOOO!",
                               @"When are we going to get there?",
                               @"I HATE YOU!",
                               nil]; 
1
  • Whenever you do a format string, put a comma between it and the variables you are substituting. Above, you should have ... @"you look tired", name ... Commented Nov 26, 2010 at 0:23

1 Answer 1

5

%@ is valid inside a stringWithFormat: call. Your code should look something like this:

arrayData = [[NSMutableArray alloc] initWithObjects:
                  [NSString stringWithFormat:@"%@ you look tired.", name],
                  @"Why do you smell so bad?",
                  @"I have to go potty!",
                  [NSString stringWithFormat:@"%@ put your pants on!", name],
                  @"Mommy!",
                  @"Daddy!",
                  @"NOOOOOO!",
                  @"When are we going to get there?",
                  @"I HATE YOU!",
                  nil]; 
Sign up to request clarification or add additional context in comments.

Comments

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.