标签:结构 成员 his line let case tor script eth
翻译自:https://github.com/raywenderlich/swift-style-guide
这个风格指南可能和你从其它地方看到的不同,我们的焦点主要集中在互联网和文章上的可读性。创建这个编程风格指南是为了保持我们的书籍、教程和入门工具包中代码的优雅与一致性------虽然我们有和很多不同的作者合作。
我们的首要目的是简洁、可读性和简单。
你在写Objective-C吗?看看我们的Objective-C风格指南吧。
let MaximumWidgetCount = 100
class WidgetContainer {
var widgetButton: UIButton
let widgetHeightPercentage = 0.85
}let MAX_WIDGET_COUNT = 100
class app_widgetContainer {
var wBut: UIButton
let wHeightPct = 0.85
}func dateFromString(dateString: NSString) -> NSDate
func convertPointAt(column: Int, row: Int) -> CGPoint
func timedAction(delay: NSTimeInterval, perform action: SKAction) -> SKAction!
// would be called like this:
dateFromString("2014-03-14")
convertPointAt(column: 42, row: 13)
timedAction(delay: 1.0, perform: someOtherAction)对于方法而言。依照Apple的习惯,在方法名里引用第一个參数:class Guideline {
func combineWithString(incoming: String, options: Dictionary?) { ... }
func upvoteBy(amount: Int) { ... }
}当我们在文章中须要引用方法的地方,要从调用者的角度包括全部必须的參数名。假设上下文非常清晰。并且准确的方法签名不重要时。你就能仅仅用法名。import MyModule var myClass = MyModule.MyClass()你不应该为Swift类型加上前缀。
@objc (RWTChicken) class Chicken {
...
}请务必在Xcode的偏好设置里设置。
if user.isHappy {
//Do something
} else {
//Do something else
}if user.isHappy
{
//Do something
}
else {
//Do something else
}在方法内用空行分隔功能。但假设分隔成太多段的话,经常意味着你须要把一个方法重构成多个方法。
class Circle: Shape {
var x: Int, y: Int
var radius: Double
var diameter: Double {
get {
return radius * 2
}
set {
radius = newValue / 2
}
}
init(x: Int, y: Int, radius: Double) {
self.x = x
self.y = y
self.radius = radius
}
convenience init(x: Int, y: Int, diameter: Double) {
self.init(x: x, y: y, radius: diameter / 2)
}
func describe() -> String {
return "I am a circle at \(centerString()) with an area of \(computeArea())"
}
override func computeArea() -> Double {
return M_PI * radius * radius
}
private func centerString() -> String {
return "(\(x),\(y))"
}
}上面的这个样例清晰地展示了以下规则:class BoardLocation {
let row: Int, column: Int
init(row: Int,column: Int) {
self.row = row
self.column = column
}
}func reticulateSplines(spline: [Double]) -> Bool {
// reticulate code goes here
}对于拥有长签名的函数,在合适的地方加入一个换行符。并为随后的几行加入一个额外的缩进:func reticulateSplines(spline: [Double], adjustmentFactor: Double,
translateConstant: Int, comment: String) -> Bool {
// reticulate code goes here
}在不论什么情况下,给闭包中的參数具有描写叙述性的名称:
return SKAction.customActionWithDuration(effect.duration) { node, elapsedTime in
// more code goes here
}对上下文清晰的单行表达式闭包,使用隐式的return:attendeeList.sort { a, b in
a > b
}Swift提供了桥接(Bridging)到Objective-C的功能,在须要时你也能使用Objective-C的完整方法:
let width = 120.0 //Double let widthString = (width as NSNumber).stringValue //String
let width: NSNumber = 120.0 //NSNumber let widthString: NSString = width.stringValue //NSString在使用Sprite Kit的代码里,使用CGFloat能使代码更加简洁,同一时候能避免太多的类型转换。
myOptional?假设想更方便的做一次拆包然后运行多个操作。用Optional Binding:.anotherOne?.optionalView?.setNeedsDisplay()
if let view = self.optionalView {
// do many things with view
}let bounds = CGRect(x: 40, y: 20, width: 120, height: 80) var centerPoint = CGPoint(x: 96, y: 42)
let bounds = CGRectMake(40, 20, 120, 80) var centerPoint = CGPointMake(96, 42)
let message = "Click the button" var currentBounds = computeViewBounds()
let message: String = "Click the button" var currentBounds: CGRect = computeViewBounds()备注:依照本原则:取一个具有描写叙述性的名字比什么都要重要。
var deviceModels: [String] var employees: [Int: String] var faxNumber: Int?
var deviceModels: Array<String> var employees: Dictionary<Int, String> var faxNumber: Optional<Int>
for _ in 0..<3 {
println("Hello three times")
}
for person in attendeeList {
// do something
}for var i = 0; i < 3; i++ {
println("Hello three times")
}
for var i = 0; i < attendeeList.count; i++ {
let person = attendeeList[i]
// do something
}var swift = "not a scripting language"
var swift = "not a scripting language";备注:Swift与JavaScript有非常大不同,在JavaScript里省略分号通常被觉得是不安全的。
var color = "red"
var colour = "red"
:]
:)
标签:结构 成员 his line let case tor script eth
原文地址:http://www.cnblogs.com/clnchanpin/p/6889794.html