someFunctionWithABunchOfArguments(
someStringArgument: "hello I am a string",
someArrayArgument: [
"dadada daaaa daaaa dadada daaaa daaaa dadada daaaa daaaa",
"string one is crazy - what is it thinking?"
],
someDictionaryArgument: [
"dictionary key 1": "some value 1, but also some more text here",
"dictionary key 2": "some value 2"
],
someClosure: { parameter1 in
print(parameter1)
})
11. 空数组和空字典
对于空数组和空字典,在声明时需要指定类型
// 指定类型 ✅推荐方式
var names: [String] = []
var lookup: [String: Int] = [:]
// 类型推导 ❌不推荐
var names = [String]()
var lookup = [String: Int]()
12. 使用变量拆分过多的条件判断
当if语句的条件过多时,使用变量拆分条件
例如 使用如下代码👇
let firstCondition = x == firstReallyReallyLongPredicateFunction()
let secondCondition = y == secondReallyReallyLongPredicateFunction()
let thirdCondition = z == thirdReallyReallyLongPredicateFunction()
if firstCondition && secondCondition && thirdCondition {
// do something
}
替换下面的代码👇
if x == firstReallyReallyLongPredicateFunction()
&& y == secondReallyReallyLongPredicateFunction()
&& z == thirdReallyReallyLongPredicateFunction() {
// do something
}
13. 控制流语句避免使用括号
在Swift标准中,不需要在控制流语句中使用括号
// ✅推荐
if x == y {
/* ... */
}
// ❌不推荐
if (x == y) {
/* ... */
}