在Python中,有一个短语用于描述利用Python的成语(for-in循环与for循环等)编写的代码。我们说这样的代码是 Pythonic 。
Apple的新Swift语言是否有相似的术语? Swiftonic ? Swiftic
我们是否只是坚持使用惯用的快速代码(描述而非术语)?
只是为了让你知道我在这里谈论的是一个例子。两个代码块都在字符串中找到字母的索引:
非swiftic(?)代码[bad] :
// extension method on the String class
func findIndexOf(char : String) -> Int? {
for var i = 0; i < countElements(self); ++i {
var ch = String(Array(self)[i])
if String(ch) == char {
return i
}
}
return nil
}
Swiftic(?)代码[good] :
// extension method on the String class
func findIndexOf(char : String) -> Int? {
for (index, ch) in enumerate(self) {
if String(ch) == char {
return index
}
}
return nil
}