项目作者: vineetchoudhary

项目描述 :
iOS Coding Style Guide
高级语言:
项目地址: git://github.com/vineetchoudhary/iOS-Coding-Style-Guide.git
创建时间: 2017-05-24T10:27:31Z
项目社区:https://github.com/vineetchoudhary/iOS-Coding-Style-Guide

开源协议:

下载


Introduction

This style guide outlines the common coding conventions of the iOS Developers.

Table of Contents

Spacing

  • Indent must use 4 spaces. Be sure to set this preference in Xcode (defaults is 4 spaces).
  • 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

    1. if (user.isHappy) {
    2. // Do something
    3. }
    4. else {
    5. // Do something else
    6. }

    Swift

    1. if user.isHappy == true {
    2. // Do something
    3. }
    4. else {
    5. // Do something else
    6. }
  • 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

    1. #import <AwesomeFramework/AwesomeFramework.h>
    2. #import <AnotherFramework/AnotherFramework.h>
    3. #import "SomeDependency.h"
    4. #import "SomeOtherDependency.h"
    5. @interface MyClass

    Swift

    1. import AwesomeFramework
    2. import AnotherFramework
    3. class MyViewController: UIViewController {
    4. // class stuff here
    5. }
  • In Objective-C, use one empty line between class extension and implementation in .m file.

    1. @interface MyClass()
    2. // Properties - empty line above and below
    3. @end
    4. @implementation MyClass
    5. // Body - empty line above and below
    6. @end
  • When using pragma marks leave 1 newline before and after.

    For example: Objective-c

    1. - (void)awakeFromNib {
    2. [super awakeFromNib];
    3. // something
    4. }
    5. #pragma mark - Config Cell
    6. - (void)configCell {
    7. // something
    8. }

    Swift

    1. override func awakeFromNib() {
    2. super.awakeFromNib()
    3. // somthing
    4. }
    5. // MARK: - Config Cell
    6. func configCell() {
    7. //something
    8. }
  • Prefer using auto-synthesis. But if necessary, @synthesize and @dynamic must each be declared on new lines in the implementation.
  • There should be exactly one blank line between methods to aid in visual clarity and organization.
  • Whitespace within methods should separate functionality, but often there should probably be new methods.
  • When doing math use a single space between operators. Unless that operator is unary in which case don’t use a space.
    For example:
    1. index = index + 1;
    2. index++;
    3. index += 1;
    4. 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

    1. // blocks are easily readable
    2. [UIView animateWithDuration:1.0 animations:^{
    3. // something
    4. } completion:^(BOOL finished) {
    5. // something
    6. }];

    swift

    1. UIView.animate(withDuration: 1.0, animations: {
    2. // something
    3. }, completion: { finished in
    4. // somthing
    5. })
  1. **Bad:**
  2. **objective-c**
  3. ```obj-c
  4. // colon-aligning makes the block indentation wacky and hard to read
  5. [UIView animateWithDuration:1.0
  6. animations:^{
  7. // something
  8. }
  9. completion:^(BOOL finished) {
  10. // something
  11. }];
  12. ```
  13. **swift**
  14. ```swift
  15. UIView.animate(withDuration: 1.0,
  16. animations: {
  17. // something
  18. }, completion: { finished in
  19. // somthing
  20. })
  21. ```

Comments

  • When they are needed, comments should be used to explain why a particular piece of code does something. Any comments that are used must be kept up-to-date or deleted.
  • Avoid block comments inline with code, as the code should be as self-documenting as possible. Exception: This does not apply to those comments used to generate documentation.

Developing….

References