Swift编程规范之 Naming
// "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 {
/* ... */
}
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()
/* ... */
}
class SomeClass<t> { /* ... */ }
class SomeClass<model> { /* ... */ }
protocol Modelable {
associatedtype Model
}
protocol Sequence {
associatedtype IteratorType: Iterator
}</model></t>
// PREFERRED
class RoundAnimatingButton: UIButton { /* ... */ }
// NOT PREFERRED
class CustomButton: UIButton { /* ... */ }
// 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
}
}
// 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!
}
// 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
/* ... */
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |