开发者

Obj-C combining strings

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


Use the following:

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

Explanations

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


I believe what you're looking for is:

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

Enjoy!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜