I’m thinking of wrapping frequently used Cocoa object selectors with my own code to improve my typing speed. A typical example would be something like the trim white space selector:-
[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
My choices are :-
(1) wrap it up in a NSSTring category like so
- (NSString *)Trim
{
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
(2) define it as a macro instead like so
#define TRIM(X) [X stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
I would prefer option (1) above, but are there any performance hits?
It is extremely unlikely that the category will make any significant, or even noticeable, difference in performance.
The category method requires one additional message dispatch, so yes, it will be slower than the macro. But the ObjC message dispatcher is one of the most optimized bits of code in the whole OS — it is by no means slow.
However, if you use the macro multiple times, your code size will increase by more than it would have with the category, which might have worse side effects. (But this isn’t much code, so it would take a lot of instances to make any real difference.)
So, as usual, it entirely depends on your exact situation — you’d have to measure it and see. I’d be amazed if you could actually measure the difference.