This doesn’t seem to work:
NSString *string = [[NSString alloc] init];
string = @"%@M", anotherstring;
I expect this to make “string” equal to “5M” if “anotherstring” is “5”.
Is this not the right syntax? Now, I could use initWithFormat and it would work, but how can you separate it into two different lines and also work?
There are two mistakes in your code. Firstly,
NSStrings are immutable, and once you allocate and initialize them, they’re set, and there’s no way to change them. For that, you’d have to look intoNSMutableString.Secondly, the syntax of your code makes no sense.
@"%@M", anotherStringis not a valid Objective-C method call. You are indeed looking for-initWithFormat:to perform the operation you want. The code should look like this:Read more about
NSStringand how to use it in the NSString Programming Reference document, and the String Programming Guide to get a sense of how to work with strings in Cocoa and Objective-C.