// defines three underscores as nothing
This is something I learned on my first programming job (eons ago…) and found useful but underused.
Useful for what? To solve the persistent tension between the robustness of single-exit point and the complexity of using extra state variables and state checking code.
Let me show you what I mean.
Don’t you just hate when a small change in the innards of your view controller forces you to change its header file just to conform to a delegate protocol? For example, adding emailing functionality requires you to implement the MFMailComposeViewControllerDelegate protocol and @import <MessageUI/MessageUI.h>. Talk about breaking encapsulation…
Thankfully, you can do that in your .m implementation file instead. (Even though Apple sample code doesn’t.) All you need to do is use the empty category:
@interface MyViewController ()
<MFMailComposeViewControllerDelegate> // privately conform to protocol
@property (nonatomic, strong) UIView *somePrivateSubview;
// synthesize and methods implementations
The empty category MyViewController () allows you to define private ivars, properties, methods, and even protocols – all in the privacy of your .m file, transparent to your clients.
To localize apps (which means: to make them work in other languages) you need to avoid using plain @”strings” and instead use the one of the tedious functions NSLocalizedString, NSLocalizedStringFromTable, or NSLocalizedStringFromTableInBundle, that take two to four arguments. Horror.
I mean, any sane programmer will get a headache from replacing a simple @"Great!" with this:
NSLocalizedStringFromTable(@"Great!", @"Messages", nil)
So I took a leaf from WordPress, and shortened the calls:
#define __(str) NSLocalizedStringFromTable(str, @"Messages", nil)
#define __d(str, description) NSLocalizedStringFromTable(str, @"Messages", description)
Now localization is easy and fun: I simply type
__(@"Great!") and the localization code is taken care of by the C pre-processor. Mission accomplished.