My code convention for Objective-C inspired by Sources
Use #pragma mark -
to categorize methods in functional groupings.
#pragma mark - Lifecycle
- (instancetype)init {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)didReceiveMemoryWarning {}
#pragma mark - Custom Accessors
- (void)setCustomProperty:(id)value {}
- (id)customProperty {}
#pragma mark - Public
- (void)publicMethod {}
#pragma mark - Private
- (void)privateMethod {}
#pragma mark - Protocol conformance
#pragma mark - UITextFieldDelegate
#pragma mark - UITableViewDataSource
#pragma mark - UITableViewDelegate
#pragma mark - IBActions
- (IBAction)submitData:(id)sender {}
if
/else
/switch
/while
etc.) always open on the same line as the statement but close on a new line.For example:
if(user.isHappy) {
// Do something
}
else {
// Do something else
}
@synthesize
and @dynamic
should each be declared on new lines in the implementation.For example:
if (!error) {
return success;
}
Not:
if (!error)
return success;
or:
if (!error) return success;
The Ternary operator, ? , should only be used when it increases clarity or code neatness, only with a single condition.
For example:
result = a > b ? x : y;
Not:
result = a > b ? x = c > d ? c : d : y;
In method signatures, there should be a space after the scope (-/+ symbol). There should be a space between the method segments.
For example:
- (void)setExampleText:(NSString *)text image:(UIImage *)image;
The beginning bracket should be in the same line of the method signature
For example:
- (void)setExampleText:(NSString *)text image:(UIImage *)image {
// Code...
}
TO-DO
TO-DO
TO-DO
TO-DO
Singleton objects should use a thread-safe pattern for creating their shared instance.
+ (instancetype)sharedInstance
{
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
This guide has been built taking inspiration from the following sources: