iOS Coding Style Guide
This style guide outlines the common coding conventions of the iOS Developers.
Method braces and other braces (if/else/switch/while etc.) must open on the same line as the statement. Braces must close on a new line.
For example: Objective-c
if (user.isHappy) {
// Do something
}
else {
// Do something else
}
Swift
if user.isHappy == true {
// Do something
}
else {
// Do something else
}
Separate imports from the rest of your file by 1 space. Optionally group imports if there are many (but try to have less dependencies). Include frameworks first.
For example: Objective-c
#import <AwesomeFramework/AwesomeFramework.h>
#import <AnotherFramework/AnotherFramework.h>
#import "SomeDependency.h"
#import "SomeOtherDependency.h"
@interface MyClass
Swift
import AwesomeFramework
import AnotherFramework
class MyViewController: UIViewController {
// class stuff here
}
In Objective-C, use one empty line between class extension and implementation in .m file.
@interface MyClass()
// Properties - empty line above and below
@end
@implementation MyClass
// Body - empty line above and below
@end
When using pragma marks leave 1 newline before and after.
For example: Objective-c
- (void)awakeFromNib {
[super awakeFromNib];
// something
}
#pragma mark - Config Cell
- (void)configCell {
// something
}
Swift
override func awakeFromNib() {
super.awakeFromNib()
// somthing
}
// MARK: - Config Cell
func configCell() {
//something
}
@synthesize
and @dynamic
must each be declared on new lines in the implementation.
index = index + 1;
index++;
index += 1;
index--;
Colon-aligning method invocation should often be avoided. There are cases where a method signature may have >= 3 colons and colon-aligning makes the code more readable. Please do NOT however colon align methods containing blocks because Xcode’s indenting makes it illegible.
Good:
objective-c
// blocks are easily readable
[UIView animateWithDuration:1.0 animations:^{
// something
} completion:^(BOOL finished) {
// something
}];
swift
UIView.animate(withDuration: 1.0, animations: {
// something
}, completion: { finished in
// somthing
})
**Bad:**
**objective-c**
```obj-c
// colon-aligning makes the block indentation wacky and hard to read
[UIView animateWithDuration:1.0
animations:^{
// something
}
completion:^(BOOL finished) {
// something
}];
```
**swift**
```swift
UIView.animate(withDuration: 1.0,
animations: {
// something
}, completion: { finished in
// somthing
})
```
Developing….