Reputation: 16292
I have a few macro defined as follows:
#define text @"play"
#define text_s @"stop"
#define text_f @"forward"
#define text_r @"rewind"
#define text_m @"mute"
I have an NSArray
filled with strings such as @""
, @"_s"
, @"_f"
, @"_r"
, @"_m"
.
So, during runtime I would like to concatenate one of the strings in this array with the macro name, for example, @"text"
.
Other words, I would like to fetch the corresponding macro based on a condition by concatenating it (i.e. "text") with either @""
, @"_s"
, @"_f"
, @"_r"
, @"_m"
and get the macro correspondingly.
So far I have tried to use:
label.text = [NSString stringWithFormat:@"%@%@", text, [myArray objectAtIndex:<INDEX>]];
But this will only concat the string (for example if it is @"stop") with the macro text and it will become "playstop".
What I would like to archive is to be able to concat the string (i.e. the prefix) and form a macro name and use it correspondingly.
For example: if @"_s"
is used, I would get the macro text_s
, if @"_f"
is used, then the macro text_f
will be used.
How could I archive this? How could I concat a macro name with a string (i.e. @"_s"
) during runtime?
Upvotes: 0
Views: 689
Reputation: 6211
You could create a macro that takes in a variable and assigns your string based on the input. (this could be a global function as well, but since you insist on macros here is an option:
#define getText(a) \
[a isEqualToString:@""] ? @"play" : \
[a isEqualToString:@"_s"] ? @"stop" : \
[a isEqualToString:@"_f"] ? @"forward" : \
[a isEqualToString:@"_r"] ? @"rewind" : \
[a isEqualToString:@"_m"] ? @"mute" : \
@""
You would call this with:
NSString *string = getText(@"_s");
//string is now @"stop"
It will be a little slow, but it will get the job done. This will be slow because if you want the macro for mute it has to check the string you send against 4 other strings first. Calling this a thousand times per second will be slow, but if you only need it once a minute then it won't matter at all. Again, there are better options for this type of behavior, but if you need this solution it works.
It needs to be said that this macro cannot be used at compile time, only at run time. So if you need something that will work in .h files this will not.
A better solution would be to add these strings to your array instead of adding @"_m", @"_r"
etc. When you create your array just fill it with the final data instead of an interim data set that never gets changed. I don't know enough about your application and it's needs to give you the best solution for your needs. You just need to find what works and is safe.
Update based on your information:
The best case would be to rewrite all the macros into more useful information, but since this is for school and performance isn't of highest priority...
#define text @"play"
#define text_s @"stop"
#define text_f @"forward"
#define text_r @"rewind"
#define text_m @"mute"
#define getText(a) \
[a isEqualToString:@""] ? text : \
[a isEqualToString:@"_s"] ? text_s : \
[a isEqualToString:@"_f"] ? text_f : \
[a isEqualToString:@"_r"] ? text_r : \
[a isEqualToString:@"_m"] ? text_m : \
@""
or:
#define text @"play"
#define text_s @"stop"
#define text_f @"forward"
#define text_r @"rewind"
#define text_m @"mute"
yourClassDictionary = @{ @"" : text,
@"_s": text_s,
@"_f": text_f,
@"_r": text_r,
@"_m": text_m };
The dictionary would be the fastest. Either will work for your situation.
Upvotes: 3
Reputation: 2130
If you don't actually have to use macros, I would do something like this...
NSDictionary *text = @{ @"" : @"play",
@"_s": @"stop",
@"_f": @"forward",
@"_r": @"rewind",
@"_m": @"mute" };
and then get the values like this:
text[suffix];
where suffix is one of @""
, @"_s"
, etc.
Upvotes: 1