1

I have a String object:

NSString* name = @"John Smith";

and another String object:

NSString* intro =@"Hello this is ";

I want to be able to change the name from the UI make it within the introduction, so I tried:

_introduction = (@"Hello this is, %@", _name);

However, only the name is getting printed when I do:

NSlog(@"%@",_introduction);

I want both sentences to be printed by printing the intro object.

Fady E
  • 346
  • 3
  • 16
  • It's not the same question. I tried the answer included in the other question. However, the answer I was looking for is the one below. – Fady E Mar 26 '19 at 21:12
  • **The** answer? There are many. Look at the second answer, it covers your question. *It's **exactly** the same question* – vadian Mar 26 '19 at 21:19

1 Answers1

1

I'm not sure what (@"Hello this is, %@", _name); does or if that's even correct syntax, but typically to do what you're trying, you'd do something like this:

introduction = [NSString stringWithFormat:@"%@ %@", intro, name];
Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
  • I thought this is how enter an object within a String. Thank you for helping. – Fady E Mar 26 '19 at 19:46
  • It is in correct syntax. The komma operator `,` evaluates both operands (`@"Hello This is, %@"` and `_name`), throws away the result of the first operand and returns the result of the second operand (`_name`). The parenthesis do simply nothing. Since the first operand is side-effect free, it is simply equivalent to `introduction = _name`. – Amin Negm-Awad Mar 26 '19 at 20:27