iOS开发 Objective-CiOS开发 Swift

swift编码规范

2018-11-29  本文已影响0人  wsj_2012

Swift 编码规范

<a name="basic-rules"></a>

基本原则

本章所含条款是代码规范的基本思想,适用于所有的编程语言。

1. 清晰

2. 简洁

3. 本地化

<a name="reference-links"></a>

参考资料

<a name="general-rules"></a>

通用规则

Preferred:

public mutating func remove(at position: Index) -> Element

Not Preferred:

public mutating func remove(position: Index) -> Element

employees.remove(x) // 不明确: 我们是在删除 x 吗?

Preferred:

public mutating func remove(member: Element) -> Element?

Not Preferred:

public mutating func removeElement(member: Element) -> Element?

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 numberOfSectionsInTableView(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
}

x.insert(y, at: z) // “x, insert y at z”
x.subViews(havingColor: y) // “x's subviews having color y”
x.capitalizingNouns() // “x, capitalizing nouns”
min(x, y, z)
print(x)
sin(x)

Preferred:

let swift = "not a scripting language"

Not Preferred:

let swift = "not a scripting language";

Preferred

resource.request().onComplete { [weak self] response in
guard let strongSelf = self else {
return 
}
let model = strongSelf.updateModel(response)
strongSelf.updateUI(model)
}

Not Preferred

// might crash if self is released before response returns
resource.request().onComplete { [unowned self] response in
let model = self.updateModel(response)
self.updateUI(model)
}

Not Preferred

// deallocate could happen between updating the model and updating UI
resource.request().onComplete { [weak self] response in
let model = self?.updateModel(response)
self?.updateUI(model)
}

Preferred:

let message = "Click the button"
let currentBounds = computeViewBounds()
var names = ["Mic", "Sam", "Christine"]
let maximumWidth: CGFloat = 106.5

Not Preferred:

let message: String = "Click the button"
let currentBounds: CGRect = computeViewBounds()
let names = [String]()
func pirateName() -> (firstName: String, lastName: String) {
return ("Guybrush", "Threepwood")
}

let name = pirateName()
let firstName = name.firstName
let lastName = name.lastName

Preferred:

var deviceModels: [String]
var employees: [Int: String]
var faxNumber: Int?

Not Preferred:

var deviceModels: Array<String>
var employees: Dictionary<Int, String>
var faxNumber: Optional<Int>

<a name="formatting"></a>

格式

class SomeClass {
func someMethod() {
if x == y {
/* ... */
} else if x == z {
/* ... */
} else {
/* ... */
}
}
}
// specifying type
let pirateViewController: PirateViewController

// dictionary syntax (note that we left-align as opposed to aligning colons)
let ninjaDictionary: [String: AnyObject] = [
"fightLikeDairyFarmer": false,
"disgusting": true
]

// declaring a function
func myFunction<T, U: SomeProtocol>(firstArgument: U, secondArgument: T) where T.RelatedType == U {
}

// calling a function
someFunction(someArgument: "Kitten")

// superclasses
class PirateViewController: UIViewController {
}
let myArray = [1, 2, 3, 4, 5]
let myValue = 20 + (30 / 2) * 3
if 1 + 1 == 3 {
fatalError("The universe is broken.")
}
func pancake(with syrup: Syrup) -> Pancake {
/* ... */
}
someFunctionWithManyArguments(firstArgument: "Hello, I am a string", secondArgument: resultFromSomeFunction(), thirdArgument: someOtherLocalProperty)

Preferred

if x == y {
}

Not Preferred

if (x == y) {
}

<a name="naming"></a>

命名

let htmlBodyContent: String = "<p>Hello, World!</p>"
let profileId: Int = 1
class URLFinder {
}

Preferred:

var greeting = "Hello"
protocol ViewController {
associatedtype ContentView: View
}
class ProductionLine {
func restock(from supplier: WidgetFactory)
}    

Not Preferred:

var string = "Hello"
protocol ViewController {
associatedtype ViewType: View
}
class ProductionLine {
func restock(from widgetFactory: WidgetFactory)
}
protocol Sequence {
associatedtype IteratorType : Iterator
}
public enum UIBackgroundRefreshStatus: Int {
case restricted
case denied 
case available
}
class NSNotification {

public struct Name {
init(_ rawValue: String)

static let UIApplicationDidBecomeActive: NSNotification.Name
static let UIApplicationDidEnterBackground: NSNotification.Name
}
}
class SomeClass<T> { /* ... */ }
class SomeClass<Model> { /* ... */ }
protocol Modelable {
associatedtype Model
}
protocol Sequence {
associatedtype IteratorType: Iterator
}
class ConnectionTableViewCell: UITableViewCell {
let personImageView: UIImageView
let popupViewController: UIViewController
}

<a name="coding-style"></a>

编码风格

<a name="access-modifiers"></a>

访问修饰符

private static let myPrivateNumber: Int

<a name="structs"></a>

Struct

Preferred:

let bounds = CGRect(x: 40, y: 20, width: 120, height: 80)
let centerPoint = CGPoint(x: 96, y: 42)

Not Preferred:

let bounds = CGRectMake(40, 20, 120, 80)
let centerPoint = CGPointMake(96, 42)

<a name="enums"></a>

Enum

enum Shape {
case rectangle
case square
case rightTriangle
case equilateralTriangle
}

Preferred

imageView.setImageWithURL(url, type: .person)

Not Preferred

imageView.setImageWithURL(url, type: AsyncImageView.Type.person)

<a name="optionals"></a>

Optional

Preferred

if someOptional != nil {
}

Not Preferred

if let _ = someOptional {
}

<a name="properties"></a>

属性

var computedProperty: String {
if someBool {
return "I'm a mighty pirate!"
}
return "I'm selling these fine leather jackets."
}
var storedProperty: String = "I'm selling these fine leather jackets." {
willSet {
print("will set to \(newValue)")
}
didSet {
print("did set from \(oldValue) to \(storedProperty)")
}
}

var computedProperty: String  {
get {
if someBool {
return "I'm a mighty pirate!"
}
return storedProperty
}
set {
storedProperty = newValue
}
}
class PirateManager {
static let shared = PirateManager()
}

<a name="closures"></a>

Closure

let value = numbers.map { $0 * 2 }.filter { $0 % 3 == 0 }.indexOf(90)

let value = numbers
.map {$0 * 2}
.filter {$0 > 50}
.map {$0 + 10}
myFunctionWithEscapingClosure() { [weak self] (error) -> Void in
guard let strongSelf = self else {
return
}

strongSelf.doSomething()
}

<a name="collections"></a>

Collection

Preferred:

var names: [String] = []
var lookup: [String: Int] = [:]

Not Preferred:

var names = [String]()
var lookup = [String: Int]()

Preferred

let stringOfInts = [1, 2, 3].flatMap { "\($0)" }

Not Preferred

var stringOfInts: [String] = []
for integer in [1, 2, 3] {
stringOfInts.append(String(integer))
}

<a name="error-handling"></a>

错误处理

<a name="guard"></a>

Guard

Preferred

guard let thingOne = thingOne else {
return
}

Not Preferred

guard let thingOne = thingOne else { return }

<a name="self"></a>

Self

class BoardLocation {
let row: Int, column: Int

init(row: Int, column: Int) {
self.row = row
self.column = column

let closure = {
print(self.row)
}
}
}

<a name="comments"></a>

注释

/**
## Feature Support

This class does some awesome things. It supports:

- Feature 1
- Feature 2
- Feature 3

## Examples

Here is an example use case indented by four spaces because that indicates a
code block:

let myAwesomeThing = MyAwesomeClass()
myAwesomeThing.makeMoney()

## Warnings

There are some things you should be careful of:

1. Thing one
2. Thing two
3. Thing three
*/
class MyAwesomeClass {
/* ... */
}
/**
This does something with a `UIViewController`, perchance.
- warning: Make sure that `someValue` is `true` before running this function.
*/
func myFunction() {
/* ... */
}
class Pirate {

// MARK: - instance properties

private let pirateName: String

// MARK: - initialization

init() {
}

}
上一篇下一篇

猜你喜欢

热点阅读