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:代码格式化
class SomeClass {
func someMethod() {
if x == y {
/* ... */
} else if x == z {
/* ... */
} else {
/* ... */
}
}
/* ... */
}
// 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>
let myArray = [1,2,3,4,5]
let myValue = 20 + (30 / 2) * 3
if 1 + 1 == 3 {
fatalError("The universe is broken.")
}
func pancake() -> Pancake {
/* ... */
}
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!")
}
someFunctionWithManyArguments( firstArgument: "Hello,I am a string",secondArgument: resultFromSomeFunction() thirdArgument: someOtherLocalVariable)
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)
})
// 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
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |