Skip to content

Instantly share code, notes, and snippets.

@capnslipp
Last active September 17, 2022 15:53
Show Gist options
  • Save capnslipp/acf198c07eeb18b9f3a530d168f1e03b to your computer and use it in GitHub Desktop.
Save capnslipp/acf198c07eeb18b9f3a530d168f1e03b to your computer and use it in GitHub Desktop.
My preferred code style for colons in Swift (following good coding habits learned long ago).
import Foundation
class Example {
static func method(label argName: String) -> Void {
NSLog(argName)
}
}
class SuperClass {}
class AType {
init(_ aString: String) {
}
}
extension String {
func __conversion() -> AType {
return AType(self)
}
}
//: Code With Differing Intents Should Look Different
class SubClass : SuperClass { // A colon used in an inheritence specifier has spaces both before & after it.
var someVar: AType = AType("123") // A colon used as a variable type specifier has a space only after it, not before.
func method1(argName: AType) {} // Likewise, a colon used as an _argument_ variable type specifier also has a space only after it.
func method2(label argName: AType) {} // Same when an argument label is present.
}
Example.method(label: "value") // A colon used as an argument label has a space only after it, not before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment