This works fine, we all know that:
NSString *textoutput = @"Hello";
outLabel.text = textoutput;
However, what if you want to include a variable inside that NSString statement like the following:
NSString *textoutput =@"Hello" Variable;
In C++ I know when I cout something and I wanted to include a variable all I did was soemthing like this:
cout << "Hello" << variableName << endl;
So I’m trying to accomplish that with Objective-C but I don’t see how.
You can do some fancy formatting using the following function:
Note that
%@assumes thatvariableis an Objective-C object. If it’s a C string, use%s, and if it’s any other C type, check out theprintfreference.Alternatively, you can create a new string by appending a string to an existing string:
Note that
NSStringis immutable — once you assign a value, you can’t change it, only derive new objects. If you are going to be appending a lot to a string, you should probably useNSMutableStringinstead.