代码组织/Code Organization

Use extensions to organize your code into logical blocks of functionality. Each extension should be set off with a // MARK: - comment to keep things well-organized.

用扩展将代码组织成各个不同的功能逻辑块。每个扩展都应该添加 // MARK: - 注释符号,以保证代码的结构清晰。

协议一致性/Protocol Conformance

In particular, when adding protocol conformance to a model, prefer adding a separate extension for the protocol methods. This keeps the related methods grouped together with the protocol and can simplify instructions to add a protocol to a class with its associated methods.

通常来说,当遵循某个协议后,推荐将与该协议相关的方法统一到某个单独的扩展中。这会让与协议相关的方法聚集在一起,也能简化整体的代码结构。

推荐(Preferred):

class MyViewController: UIViewController {
  // class stuff here
}

// MARK: - UITableViewDataSource
extension MyViewController: UITableViewDataSource {
  // table view data source methods
}

// MARK: - UIScrollViewDelegate
extension MyViewController: UIScrollViewDelegate {
  // scroll view delegate methods
}

不推荐(Not Preferred):

class MyViewController: UIViewController, UITableViewDataSource, UIScrollViewDelegate {
  // all methods
}

Since the compiler does not allow you to re-declare protocol conformance in a derived class, it is not always required to replicate the extension groups of the base class. This is especially true if the derived class is a terminal class and a small number of methods are being overridden. When to preserve the extension groups is left to the discretion of the author.

编译器不允许你在派生类中重新声明已经遵守的协议,因此可以在派生类中省略基类的扩展声明。这在派生类是一个终端类,且只有很少的方法需要重写的情况下是合理的。何时保留扩展声明将由开发者自行决定。

For UIKit view controllers, consider grouping lifecycle, custom accessors, and IBAction in separate class extensions.

对于 UIKit 中的视图控制器,可考虑将生命周期、自定义存取器和 IBAction 分组在单独的类扩展中。

无效代码/Unused Code

Unused (dead) code, including Xcode template code and placeholder comments should be removed. An exception is when your tutorial or book instructs the user to use the commented code.

应该移除无用代码,比如 Xcode 模板工程代码和占位注释。但教程或书籍中用于指导用户的注释代码除外。

Aspirational methods not directly associated with the tutorial whose implementation simply calls the superclass should also be removed. This includes any empty/unused UIApplicationDelegate methods.

仅实现简单的调用父类,且与教程无直接关联的方法也应该被移除。包括任何空的或无用的 UIApplicationDelegate 方法。

推荐(Preferred):

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return Database.contacts.count
}

不推荐(Not Preferred):

override func didReceiveMemoryWarning() {
  super.didReceiveMemoryWarning()
  // Dispose of any resources that can be recreated.
}

override func numberOfSections(in tableView: UITableView) -> Int {
  // #warning Incomplete implementation, return the number of sections
  return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  // #warning Incomplete implementation, return the number of rows
  return Database.contacts.count
}

简化引用/Minimal Imports

Import only the modules a source file requires. For example, don't import UIKit when importing Foundation will suffice. Likewise, don't import Foundation if you must import UIKit.

只引用必要的文件。举个例子,引用 Foundation 就足够的情况下不要再引用 UIKit。同样,需要引用 UIKit 的时候,就不要引用 Foundation

推荐(Preferred):

import UIKit
var view: UIView
var deviceModels: [String]

推荐(Preferred):

import Foundation
var deviceModels: [String]

不推荐(Not Preferred):

import UIKit
import Foundation
var view: UIView
var deviceModels: [String]

不推荐(Not Preferred):

import UIKit
var deviceModels: [String]

Made with in Shangrao,China By 老雷

Copyright © devler.cn 1987 - Present

赣ICP备19009883号-1