[译]raywenderlich.com官方的swift风格指南(二)
2016-03-13 10:55
483 查看
函数声明
让函数声明短到能在一行内显示,包括左花括号:func reticulateSplines(spline: [Double]) ->Bool { // reticulate code goes here }
对于一些比较长的函数声明,可以在适当的地方换行,同时要在子行加上额外的缩进
func reticulateSplines(spline: [Double], adjustmentFactor: Double, translateConstant: Int, comment: String) -> Bool{ // reticulate code goes here }
闭包表达式
只有当参数列表后面仅有一个单独的闭包表达式参数时,使用后关闭的语法。闭包的参数要给描述性的命名
推荐
UIView.animateWithDuration(1.0) { self.myView.alpha = 0 } UIView.animateWithDuration(1.0, animations: { self.myView.alpha = 0 }, completion: { finished in self.myView.removeFromSuperview() } )
不推荐
UIView.animateWithDuration(1.0, animations: { self.myView.alpha = 0 }) UIView.animateWithDuration(1.0, animations: { self.myView.alpha = 0 }) { f in self.myView.removeFromSuperview() }
对于内容很清晰的单个闭包表达式,使用隐式的返回
attendeeList.sort { a, b in a > b }
类型
推荐使用Swift的原生类型。Swift提供了OC的桥接,所以如果需要你仍然可以使用原生的全部方法推荐
let width =120.0 // Double let widthString =(width as NSNumber).stringValue // String
不推荐
let width: NSNumber = 120.0 // NSNumber let widthString: NSString =width.stringValue // NSString
在SpriteKit的代码中,为了使代码更简洁,避免太多转换,使用CGFloat
常量
常量被关键字let定义,变量被关键字var定义,所以如果变量的值不改变推荐使用let而不是varTip:一个好的技巧就是先用let定义所有变量,只有当编译器报错了才改成var
可选
当变量或函数返回类型的值可为nil时,用?号声明其为可选类型只有那些你知道会在使用前已初始化的实例变量,比如子视图会在viewDidLoad中建立中(涉及到的变量),你才能使用!号声明的拆包类型
当访问一个可选值时,使用可选链,如果值是仅被访问一次或者链中有许多个可选类型。
self.textContainer?.textLabel?.setNeedsDisplay()
使用可选(optional binding)绑定,会让一次拆包和执行多个操作更方便
if lettextContainer = self.textContainer { // do many things with textContainer }
当命名可选变量或属性时,避免将他们命名为optionalString 或maybeView,因为他们在声明是就已经是声明为可选的了。
对于可选绑定来说,跟随原值命名而不是使用一些像unwrappedView或actualLabel
推荐
var subview: UIView? var volume: Double? // later on... if let subview =subview, volume = volume { // do something with unwrapped subview and volume }
不推荐
var optionalSubview: UIView? var volume: Double? if letunwrappedSubview = optionalSubview { if letrealVolume = volume { // do something with unwrappedSubview and realVolume } }
结构初始化
使用原生的Swift结构初始化而不是传统的CGGeometry构造函数推荐
let message ="Click the button" letcurrentBounds = computeViewBounds() var names =[String]() let maximumWidth: CGFloat = 106.5
不推荐
let message: String = "Click the button" let currentBounds: CGRect = computeViewBounds() varnames: [String] =[]
注意:遵守这份指南意味着选择描述性命名比以往任何情况都重要。
语法糖
推荐更便捷的类型声明而不是写通用语法推荐
var deviceModels: [String] var employees: [Int: String] varfaxNumber: Int?
不推荐
var deviceModels: Array<String> var employees: Dictionary<Int, String> varfaxNumber: Optional<Int>
控制流
推荐for-in而不是for-condition-increment类型推荐
for _ in0..<3 { println("Hello three times") } for (index, person) in attendeeList.enumerate() { println("\(person) is at position #\(index)") }
不推荐
for vari = 0; i <3; i++ { println("Hello three times") } for vari = 0; i <attendeeList.count; i++{ let person =attendeeList[i] println("\(person) is at position #\(i)") }
分号
Swift不需要在你代码中的每个语句后加分号,只有当你需要在一行中写多个语句时才用到。不要在一个单行上写多个语句。
只有在for-condition-increment结构的情况下需要用多个分号。然而有可选的for-in结构可以使用。
推荐
let swift ="not a scripting language"
不推荐
let swift ="not a scripting language";
注意:Swift和JavaScript非常不同,(JavaScript)省略分号通常是不安全的
语言
使用英式英语来拼写苹果的api推荐
let color ="red"
不推荐
let colour ="red"
相关文章推荐
- Apple Swift学习教程
- 介绍 Fedora 上的 Swift
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法
- Lua函数用法研究