views:

47

answers:

2

this must be such a simple problem but can someone tell me why this doesnt work:

visibilityString1 = @"the";
visibilityString2 = @"end";

visibilityString = (@"This is %@ %@", visibilityString1, visibilityString2);

Every time I try to combine strings this way, it will only return the second string so what I get is:

end

+5  A: 

I believe what you're looking for is:

visibilityString = [NSString stringWithFormat:@"This is %@ %@", visibilityString1, visibilityString2];

Enjoy!

andyvn22
+2  A: 

Use the following:

visibilityString = [NSString stringWithFormat:@"This is %@ %@", visibilityString1, visibilittyString2];

Explications

In C (and therefore also in ObjC), the syntax (expression, expression, expression) evaluates all expressions and returns the value of the last one. So if you do:

int foo = (bar(), baz(), 4);

bar() and baz() will be called, but foo will be 4. (Don't do this at home. It's not a good practice.)

zneak