Swift编程规范之 Naming

  • 1 Swift中不需要再使用Objective-C那样的前缀,譬如使用 GuybrushThreepwood 而不是LIGuybrushThreepwood。

  • 2 对于类型名即struct, enum, class, typedef, associatedtype等等使用 PascalCase 。

    -3 对于函数名、方法名、变量名、常量、参数名等使用camelCase。

  • 4 在使用首字母缩写的时候尽可能地全部大写,并且注意保证全部代码中的统一。不过如果缩写被用于命名的起始,那么就全部小写。

// "HTML" is at the start of a variable name, so we use lowercase "html"
let htmlBodyContent: String = "<p>Hello, World!</p>"
// Prefer using ID to Id
let profileID: Int = 1
// Prefer URLFinder to UrlFinder
class URLFinder {
    /* ... */
}
  • 5 对于静态常量使用 k 前缀 + PascalCase。
class MyClassName {
    // use `k` prefix for constant primitives
    static let kSomeConstantHeight: CGFloat = 80.0
    // use `k` prefix for non-primitives as well
    static let kDeleteButtonColor = UIColor.redColor()
    // don't use `k` prefix for singletons
    static let sharedInstance = MyClassName()
    /* ... */
}
  • 6 对于泛型或者关联类型,使用PascalCase描述泛型,如果泛型名与其他重复,那么可以添加一个Type后缀名到泛型名上。
class SomeClass<t> { /* ... */ }
class SomeClass<model> { /* ... */ }
protocol Modelable {
    associatedtype Model
}
protocol Sequence {
    associatedtype IteratorType: Iterator
}</model></t>
  • 7 命名必须要是不模糊的并且方便表述的
// PREFERRED
class RoundAnimatingButton: UIButton { /* ... */ }
// NOT PREFERRED
class CustomButton: UIButton { /* ... */ }
  • 8 不要使用缩写,可以选择较为简短的单词。
// PREFERRED
class RoundAnimatingButton: UIButton {
    let animationDuration: NSTimeInterval
    func startAnimating() {
        let firstSubview = subviews.first
    }
}
// NOT PREFERRED
class RoundAnimating: UIButton {
    let aniDur: NSTimeInterval
    func srtAnmating() {
        let v = subviews.first
    }
}
  • 9 对于不是很明显的类型需要将类型信息包含在属性名中。
// PREFERRED
class ConnectionTableViewCell: UITableViewCell {
    let personImageView: UIImageView
    let animationDuration: NSTimeInterval
    // it is ok not to include string in the ivar name here because it's obvious
    // that it's a string from the property name
    let firstName: String
    // though not preferred, it is OK to use `Controller` instead of `ViewController`
    let popupController: UIViewController
    let popupViewController: UIViewController
    // when working with a subclass of `UIViewController` such as a table view
    // controller, collection view controller, split view controller, etc.,
    // fully indicate the type in the name.
    let popupTableViewController: UITableViewController
    // when working with outlets, make sure to specify the outlet type in the
    // variable name.
    @IBOutlet weak var submitButton: UIButton!
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var nameLabel: UILabel!
}
// NOT PREFERRED
class ConnectionTableViewCell: UITableViewCell {
    // this isn't a `UIImage`, so shouldn't be called image
    // use personImageView instead
    let personImage: UIImageView
    // this isn't a `String`, so it should be `textLabel`
    let text: UILabel
    // `animation` is not clearly a time interval
    // use `animationDuration` or `animationTimeInterval` instead
    let animation: NSTimeInterval
    // this is not obviously a `String`
    // use `transitionText` or `transitionString` instead
    let transition: String
    // this is a view controller - not a view
    let popupView: UIViewController
    // as mentioned previously, we don't want to use abbreviations, so don't use
    // `VC` instead of `ViewController`
    let popupVC: UIViewController
    // even though this is still technically a `UIViewController`, this variable
    // should indicate that we are working with a *Table* View Controller
    let popupViewController: UITableViewController
    // for the sake of consistency, we should put the type name at the end of the
    // variable name and not at the start
    @IBOutlet weak var btnSubmit: UIButton!
    @IBOutlet weak var buttonSubmit: UIButton!
    // we should always have a type in the variable name when dealing with outlets
    // for example, here, we should have `firstNameLabel` instead
    @IBOutlet weak var firstName: UILabel!
}
  • 10 在编写函数参数的时候,要保证每个参数都易于理解其功能。

  • 11 根据 Apple’s API Design Guidelines, 对于protocol,如果其描述的是正在做的事情,譬如Collection,那么应该命名为名词。而如果是用于描述某种能力,譬如Equatable, ProgressReporting,那么应该添加 able, ible, 或者 ing 这样的后缀。如果你的协议并不符合上述两种情形,那么应该直接添加一个Protocol后缀,譬如:

// here, the name is a noun that describes what the protocol does
protocol TableViewSectionProvider {
    func rowHeight(atRow row: Int) -> CGFloat
    var numberOfRows: Int { get }
    /* ... */
}
// here, the protocol is a capability, and we name it appropriately
protocol Loggable {
    func logCurrentState()
    /* ... */
}
// suppose we have an `InputTextView` class, but we also want a protocol
// to generalize some of the functionality - it might be appropriate to
// use the `Protocol` suffix here
protocol InputTextViewProtocol {
    func sendTrackingEvent()
    func inputText() -> String
    /* ... */
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值