You most probably was working on an open source project, and must have noticed projects are usually organized and consistent 🧐, this is due to following a code style, and you must have noticed that some projects are messy, ugly and inconsistent 🤮.
There are different Swift Code Styles, one by Apple, Google, Ray Wenderlich, Linkedin and AirBNB, and others, preferring a code style over another is subjective, I recommend inspecting different styles and adopt one according to the project needs.
Developers spend time reading code, more than writing new code 😌, this alone justifies having a consistent clean coding style, this is an investment you do for yourself as a developer, and for your colleagues 😇, regular indentation, proper spelling, .. etc does not make your task take longer, in the contrary, it saves you big load of time in the future, and improves readability and maintainability.
There is an official API Design guidelines, it covers naming conventions, but is not comprehensive enough.
Another guideline can be found here, by Nikita Lazarev-Zubov, it looks like she has preservations on some guidelines too, I highly recommend going through these guidelines and inspecting yourself.
Swift Style Guides (a brief look)
Swift Reserved Keywords, with brief explanations.5 (2)
A reserved word is a word that cannot be used as an identifier, this is a syntactic definition, as mentioned before in Swift Lexical Structure, all lists below are written alphabetically.
- Keywords used in declarations:
Reserved Word | Details |
---|---|
associatedtype | Associated types are a powerful way of making protocols generic, it gives a placeholder name to a type that’s used as part of the protocol. |
class | One of swift’s general purpose, flexible constructs also see struct |
deinit | A method that gets automatically called when an object is freed up from memory by ARC. |
enum | Enums let’s you define a custom kind of value in Swift, with predefined possible values. |
extension | Extensions add new functionality to an existing class, structure, enumeration, or protocol type. |
fileprivate | One of swift’s access modifiers. |
func | Used for creating functions. |
import | A declaration used for importing modules and submodules |
init | as per swift documentation, “Initialization is the process of preparing an instance of a class, structure, or enumeration for use.” |
inout | inout allows parameters to be changed outside of the function scope. |
internal | One of swift’s access modifiers. |
let | A keyword used for declaring a constant |
open | One of swift’s access modifiers. |
operator | A special symbol/ phrase that you use to check, change, or combine values. |
private | One of swift’s access modifiers. |
precedencegroup | defining precedence groups to use for our custom operators. |
protocol | as per swift’s official documentation “A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.” |
public | One of swift’s access modifiers. |
rethrows | rethrows keyword is used with function that accepts a throwing function as a parameter. |
static | defines a static scope. |
struct | complex value-types. |
subscript | Subscripts allow you to write shortcuts to elements from collections, Sequences in classes, structures and enumerations. |
typealias | Defines an alias for an existing type. |
var | A keyword used for declaring a variable. |
- Keywords used in statements:
Reserved Word | Details |
---|---|
break | One of the control transfer statements, ends execution of a loop, an if statement, or a switch statement. |
case | Used for pattern testing. |
catch | Used for handling any potential errors caused by a function that throws. |
continue | One of the control transfer statements, ends execution of the current iteration of a loop statement but does not stop execution of the loop statement. |
default | Used for default cases in switch. |
defer | Used for making a block to execute just before a function exits. |
do | Used for creating do blocks, for example (do-while, do-catch) |
else | Used for executing a block when a condition is not satisfied. |
fallthrough | One of the control transfer statements |
for | Used for iterating over a sequence. |
guard | Used in control transfer statement and optional unwrapping. |
if | Used for condition evaluation. |
in | In is a keyword defined in the Swift closure syntax as a separator between the function type and the function body in a closure, and used in checking if an object is in a sequence. |
repeat | A control flow statement, similar to while loop |
return | One of the control transfer statements |
throw | Used for throwing an error in a function that throws. |
switch | A switch statement considers a value and compares it against several possible matching patterns. |
where | Used to filter out values, in statements like switch, for, protocol extension, first, contains, initializers. |
while | A control flow statement, it performs a set of statements until a condition becomes false |
- Keywords used in expressions and types:
Reserved Word | Details |
---|---|
Any | Any can represent an instance of any type at all, including function types. |
as | Used for type casting. |
catch | Used in error handling, when an error is thrown, it’s matched against the catch clauses. |
false | A literal used to express booleans. |
is | Used to check whether an object is of a certain class type |
nil | A valueless state that could be assigned to optionals. |
rethrows | allows forwarding a thrown error by a given function parameter |
self | “self” refers to the current object within a class or struct. |
Self | Refers to a type – usually the current type in the current context. |
super | super is used to call up to your superclass. |
throw | Used for throwing an error in a function that throws. |
throws | To mark a function throwing. |
true | A literal used to express booleans. |
try | The try keyword is used to indicate that a method can throw an error. To catch and handle an error, the throwing method call needs to be wrapped in a do-catch statement. |
- Keywords used in patterns:
Reserved Word |
---|
_ |
. |
- Keywords that begin with a number sign (
#
), literals expressions:
Reserved Word | Details |
---|---|
#available | Used to determine the availability of APIs at runtime |
#colorLiteral | Used to make the XCode IDE to display a color swatch 🟥 |
#column | column number of the line where it is being run. |
#dsohandle | – |
#elseif | Literal conditional statement |
#else | Literal else statement. |
#endif | Literal marker for closing an a literal if statement |
#error | Creates a red compiler error & prevents code from compiling |
#fileID | Generates concise file string in all language modes. |
#fileLiteral | Used to make the XCode IDE link to a local file. |
#filePath | Outputs the file path of in which code is being run. |
#file | Outputs the name of the file in which code is being run. |
#function | Outputs the name of the function where code belongs. |
#if | Literal if statement. |
#imageLiteral | Used to make the XCode IDE to display an image. |
#keyPath | – |
#line | line number where it is being run. |
#selector | – |
#sourceLocation | – |
#warning | will cause Xcode to display a warning with the given message. |
- Keywords reserved in particular contexts:
Reserved Word | Details |
---|---|
associativity | defines how operators of the same precedence are grouped together. |
convenience | Convenience modifier placed before the init keyword. |
didSet | A property observer |
dynamic | A declaration modifier used to make use of Objective-C’s dynamism. |
final | One of Swift’s access modifiers. |
get | Used when getting a computed property. |
indirect | Used for recursive Enums |
infix | Used when creating custom operators. |
lazy | Used for just-in-time calculation. |
left | Used to specify the associativity of a custom operator |
mutating | Functions marked as mutating can change any property within its enclosing value |
none | Used to specify the associativity of a custom operator |
nonmutating | – |
optional | – |
override | Used for overriding child classes. |
postfix | Used in creating custom functions, it’s mathematical notation in which operators follow operands. |
precedence | Operator precedence is a set of rules that determine which operator is executed before another. |
prefix | Used in creating custom functions, it’s mathematical notation in which operators follow operands. |
Protocol | A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. |
required | Required keyword means that inheriting classes must provide an implementation of the method. |
right | Used to specify the associativity of a custom operator |
set | Used when getting a computed property. |
some | denotes an opaque type. |
Type | A metatype type refers to the type of any type, including class types, structure types, enumeration types, and protocol types. |
unowned | A reference type, used for memory management. |
weak | A reference type, used for memory management. |
willSet | A property observer |
- Outside the context in which they appear in the grammar, they can be used as identifiers.
The following tokens are reserved as punctuation and can’t be used as custom operators:
( , ) | { , } | [ , ] |
---|---|---|
. | , | : |
; | = | @ |
# | & (as a prefix operator) | -> |
` | ? | ! (as a postfix operator) |
Swift Lexical Structure5 (2)
Swift lexical structure, consists of valid tokens (lowest-level building blocks) that form the structure of any swift program, these tokens describe the rest of whole swift language…
A token consists of an identifier, keyword, punctuation, literal, or operator.
1) Identifiers:
An example of an identifier is a variable name, for example here “pet” is an identifier.let pet = "Happy Dinosaur 🦖";
Identifiers support unicode characters, you can name you variable in you native language, and as in other programming languages, you cannot use keywords as identifiers, this is still possible if you surrounding a keyword with back-ticks,
var `var` = "var"
examples of unicode identifiers are
var _latitude = 32.0;
;
var アップル = "apple"
2) Keywords:
The list of basic keywords in swift are listed below, see (Swift Reserved Keywords) for comprehensive list and details.
class | deinit | enum |
extension | func | import |
init | let | protocol |
static | struct | subscript |
typealias | var | break |
continue | default | do |
else | fallthrough | if |
in | for | return |
switch | where | while |
as | dynamicType | is |
new | super | self |
Self | Type | __COLUMN__ |
__FILE__ | __FUNCTION__ | __LINE__ |
associativity | didSet | get |
infix | inout | left |
mutating | none | nonmutating |
override | precedence | prefix |
right | set | unowned |
unowned(safe) | unowned(unsafe) | weak |
willSet |
3) Literals:
literals fall into 3 categories, integer, floating point, and string literals
Integer Literalsvar a = 10
//Binary
var b = 00010100b
//Hexadecimal
var c = 14x
//Octal
var d = 24o
leading zeros will be ignored by the compiler, and the use of underscores is possible to increase readability.var a = 100_000_000
Floating Point Literals//Simple floating point number
var a = 10.7
//Exponent floating point number
var b = 10.6e2var c = 10.1e-2
//Exponent floating point number
//Hexa decimal exponent
var d = 0xAp2
//Hexa decimal exponent
var d = 0xAp-2
String Literals
String literals are characters are enclosed within double quotes. Strings can contain escape sequences to represent characters like qoutes. Example for string literal is shown below.
var a = “test”
var a = “Hello\nWorld”
\0 Null Character
\ Backslash
\t Horizontal Tab
\n New line
\r Carriage Return
\” Double Quote
\’ Single Quote
4) Operators:
There are different operators supported in swift which includes
+ : Addition
– : Subtraction
* : Multiplication
/ : Division
% : Remainder
^ : Exponent
& : Bitwise And
&& : Logical And
| : Bitwise Or
|| : Logical Or
++ : Increment Operator
– : Minus
~ : Bitwise Not
< : Less Than
> : Greater Than
… etc.
Keep in mind, as in Swift’s official documentation, this is a list of reserved punctuation and can’t be used as custom operators:
“(
, )
, {
, }
, [
, ]
, .
, ,
, :
, ;
, =
, @
, #
, &
(as a prefix operator), ->
, `
, ?
, and !
(as a postfix operator)”
Swift Whitespace:
White spaces are used to separate tokens and to distinguish prefixes, otherwise it’s normally omitted by the compiler.
Swift Comments:
these are statements that are ignored by the compiler, and meant for documentation purposes of our code, they could be either one-line or multi-line.
// This is a single line comment
/* Multi line (block) comment - can have
more than one line! */