Khant Thu Linn Khant Thu Linn - 1 year ago 65
iOS Question

iOS define String dynamically

For localization, I can define %@ in localized string and it will be populated dynamically.

But can I do same thing for this one? If I can't, is there any alternative way? I don't want to write function and call from there.

#define MESSAGE @"Event - %@ is now saved in your calender."

Answer Source

Macros are not dynamic. That means, that the compiler will write @"Event - %@ is now saved in your calender." everywhere you use MESSAGE.

But you can replace the %@ placeholder on runtime using stringWithFormat::

#define MESSAGE @"Event - %@ is now saved in your calender."
NSString *string = [NSString stringWithFormat:MESSAGE, @"EventName"];

string will now be: Event - EventName is now saved in your calender.

More information about Macros and the Preprocessor can be found in the documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download