加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift编程规范之 Code Formatting

发布时间:2020-12-14 06:52:09 所属栏目:百科 来源:网络整理
导读:Code Formatting:代码格式化 1 使用4个空格来代替Tabs 2 避免过长的行,可以在XCode中进行设置单行最大长度:(Xcode-Preferences-Text Editing-Page guide at column: 160 is helpful for this) 3 保证每个文件结尾都存在一个新行 Ensure that there is a ne

Code Formatting:代码格式化

  • 1 使用4个空格来代替Tabs

  • 2 避免过长的行,可以在XCode中进行设置单行最大长度:(Xcode->Preferences->Text Editing->Page guide at column: 160 is helpful for this)

  • 3 保证每个文件结尾都存在一个新行 Ensure that there is a newline at the end of every file.

  • 4 避免无意义的尾随空格: (Xcode->Preferences->Text Editing->Automatically trim trailing whitespace + Including whitespace-only lines).

  • 5 避免将单独的左花括号放置到一行,我们参考了:1TBS style。

class SomeClass {
    func someMethod() {
        if x == y {
            /* ... */
        } else if x == z {
            /* ... */
        } else {
            /* ... */
        }
    }
    /* ... */
}
  • 6 在写变量的类型声明、字典类型的键、函数参数、协议的声明或者父类的时候,不要在冒号前添加空格。
// specifying type
let pirateViewController: PirateViewController
// dictionary syntax (note that we left-align as opposed to aligning colons)
let ninjaDictionary: [String: AnyObject] = [
    "fightLikeDairyFarmer": false,"disgusting": true
]
// declaring a function
func myFunction<t,u: someprotocol where t.relatedtype == u>(firstArgument: U,secondArgument: T) {
    /* ... */
}
// calling a function
someFunction(someArgument: "Kitten")
// superclasses
class PirateViewController: UIViewController {
    /* ... */
}
// protocols
extension PirateViewController: UITableViewDataSource {
    /* ... */
}</t,u: someprotocol where t.relatedtype == u>
  • 7 一般来说,逗号后面都要跟随一个空格。
let myArray = [1,2,3,4,5]
  • 8 在二元操作符譬如+,==,或者 ->的前后需要加上空格,但是对于( 、`)的前后不需要加空格。
let myValue = 20 + (30 / 2) * 3
if 1 + 1 == 3 {
    fatalError("The universe is broken.")
}
func pancake() -> Pancake {
    /* ... */
}
  • 9 我们默认使用Xcode推荐的格式化风格(CTRL-I) ,在声明某个函数的时候会多行排布参数。
ode indentation for a function declaration that spans multiple lines
func myFunctionWithManyParameters(parameterOne: String,parameterTwo: String,parameterThree: String) {
    // Xcode indents to here for this kind of statement
    print("(parameterOne) (parameterTwo) (parameterThree)")
}
// Xcode indentation for a multi-line `if` statement
if myFirstVariable > (mySecondVariable + myThirdVariable)
    && myFourthVariable == .SomeEnumValue {
    // Xcode indents to here for this kind of statement
    print("Hello,World!")
}
  • 10 在调用多参数函数的时候,会把多个参数放置到单独的行中:
someFunctionWithManyArguments( firstArgument: "Hello,I am a string",secondArgument: resultFromSomeFunction() thirdArgument: someOtherLocalVariable)
  • 11 对于大型的数组或者字典类型,应该将其分割到多行内,[ 与 ]类比于花括号进行处理。对于闭包而言也应该同样适合于该规则。
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)
    })
  • 12 尽可能地使用本地变量的方式来避免多行的判断语句。
// PREFERRED
let firstCondition = x == firstReallyReallyLongPredicateFunction()
let secondCondition = y == secondReallyReallyLongPredicateFunction()
let thirdCondition = z == thirdReallyReallyLongPredicateFunction()
if firstCondition && secondCondition && thirdCondition {
    // do something
}
// NOT PREFERRED
if x == firstReallyReallyLongPredicateFunction()
    && y == secondReallyReallyLongPredicateFunction()
    && z == thirdReallyReallyLongPredicateFunction() {
    // do something
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读