This document defines Swift and Objective-C code style rules for this project.
The swift code style rules are defined by this set of swiftlint rules
Variable names should use capitalization on all word expect the first, and never '_' ie
Right:
BOOL isFirstTimeReading;
int age;
NSArray *myArray;
Wrong:
BOOL is_first_time_reading;
int Age;
int isFirstTimeReading_ever;
Objective-C Instance variables must be prefixed with _
Right:
@interface Object {
BOOL _isFirstTimeReading;
int _age;
}
Wrong:
@interface Object {
BOOL isFirstTimeReading;
int age;
}
Pointer * must be preceded with a space and with no space after ie
Right:
void *pointer;
Wrong:
void * pointer;
void* pointer;
void*pointer;
If, switch, and other keyword that are not function but takes parameter should have a space before ()
Right:
if (a) {
...
}
function();
Wrong:
if( a )
if ( a )
if(a)
function ();
Brackets usage
Right:
if (a) {
...
}
while (a) {
...
}
void function()
{
...
}
- (void)functionWithParameter:(BOOL)parameter
{
...
}
Wrong:
- (void)function {
...
}
if (a)
{
...
}
Prefer early return ie:
Prefer:
if (!a)
return;
Over:
if (a) {
...
...
}
Objective C code - Don't call multiply the same method.
Right:
NSWindow *window = [self window];
NSRect frame = [window frame];
frame.origin.x = 0;
[window setFrame:frame display:NO];
Wrong:
NSRect frame = [[self window] frame];
frame.origin.x = 0;
[[self window] setFrame:frame display:NO];
objectAtIndex is gone - keep it like this
Right:
NSArray *array;
…filled with lots of stuff
id object = array[index];
Wrong:
NSArray *array;
…filled with lots of stuff
id object = [array objectAtIndex:index];
NSArray literals improve readability - use them
Right:
NSArray *array = @[obj1, obj2, obj3];
Wrong:
NSArray *array = [NSArray arrayWithObjects: obj1, obj2, obj3, nil];