Khant Thu Linn
Khant Thu Linn

Reputation: 6133

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

Upvotes: 2

Views: 185

Answers (1)

FelixSFD
FelixSFD

Reputation: 6092

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.

Upvotes: 3

Related Questions