I have this method which basically adds the letters to the contents in an array (demonstration)
- (NSMutableArray *) addTheLetterZ:(NSArray *)array {
NSMutableArray *addedLetterArray = [[NSMutableArray alloc] init];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[addedLetterArray addObject:[NSString stringWithFormat:@"%@ Y", obj]];
}];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[addedLetterArray addObject:[NSString stringWithFormat:@"%@ Z", obj]];
}];
return addedLetterArray;
}
I have 2 doubts:
-
Can any method be converted to a macro?
-
Is there a general rule of thumb to follow when converting a function to a macro?
I have looked at other questions but even in their solutions i get an error when i try the do-loop solution to convert this method to a macro.
The link you posted is the correct way to write a multi-line macro. To answer your questions:
If you are having problems the best approach is to post your attempt at “macro-fication”.
However: There is almost no case that you should use a macro to replace a function or method call. Most of the time macros are used they are used for the wrong reasons. For example if you are repeating a bunch of code that is only a few lines long and you think it will be more efficient to replace these lines with a macro instead of a function call you will generally be wrong. Modern compilers are very efficient at determining when functions should be inlined and effectively giving you the same advantages of a macro without the disadvantages. Macros have no compiler support for things like type checking, are error prone, and are harder to write.
I’m not sure what you had in mind, but here is an example from your own code on how you could use functions rather than macros: