\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{MerlinElMago (MerlinElMago)} \pdfinfo{ /Title (swift-4.pdf) /Creator (Cheatography) /Author (MerlinElMago (MerlinElMago)) /Subject (Swift 4 Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{ABCDEF} \definecolor{LightBackground}{HTML}{F4F8FD} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Swift 4 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{MerlinElMago (MerlinElMago)} via \textcolor{DarkBackground}{\uline{cheatography.com/32585/cs/10104/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}MerlinElMago (MerlinElMago) \\ \uline{cheatography.com/merlinelmago} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 10th October, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{4.24 cm} x{3.76 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Variable declarations}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}{\bf{let}} variable {[}: {\bf{type}}{]} = {\emph{value}} & Define a constant, providing a type. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\bf{let}} variable {[}: {\bf{type}}{]} = {\emph{value}} & Defina a variable, providing a type. \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Swift inferes types based on the type of contents that is assigned at creation time. The type declaration is optional.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Comments}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`/* This is a {\bf{multiline}} comment in Swift */`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`// This is a {\bf{single line}} comment in Swift`} \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Comments may be either single or multiline and may even be written after the last command on a line.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.76 cm} x{4.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Data types}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Int}} & Default Integer value depending on the running platform \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\bf{Int8}} & 1 Byte = \{\{nl\}\}-127...+127 (signed) \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Int32}} & 4 Byte = \{\{nl\}\}-2 147 483 648...+2 147 483 648 \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} {\bf{Int64}} & 8 Byte = \{\{nl\}\}-9 223 372 036 854 775 808...\{\{nl\}\}+9 223 372 036 854 775 808 \tn % Row Count 12 (+ 4) % Row 4 \SetRowColor{LightBackground} {\bf{Float}} & ( \textasciitilde{}6 digits precision )\{\{nl\}\}1.2e\textasciicircum{}-38\textasciicircum{}...3.4e\textasciicircum{}+38\textasciicircum{} \tn % Row Count 15 (+ 3) % Row 5 \SetRowColor{white} {\bf{Double}} & ( \textasciitilde{}15 digits precision )\{\{nl\}\}2.3e\textasciicircum{}-308\textasciicircum{}...1.7e\textasciicircum{}+308\textasciicircum{} \tn % Row Count 18 (+ 3) % Row 6 \SetRowColor{LightBackground} {\bf{Bool}} & Can be either true or false. \tn % Row Count 20 (+ 2) % Row 7 \SetRowColor{white} \{\{nobreak\}\}{\bf{Character}}\{\{nobreak\}\} & Only one character in length. \tn % Row Count 22 (+ 2) % Row 8 \SetRowColor{LightBackground} {\bf{String}} & Same as character but unlimited in length \tn % Row Count 24 (+ 2) % Row 9 \SetRowColor{white} {\bf{Optional}} & Can be either a value (of any type) or {\emph{no value}}. \tn % Row Count 27 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{All Int values may be preceded by a capital U to indicate an unsigned range, which shifts min values to 0 and max values to their double.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.76 cm} x{4.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Literals}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Integer}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `var myInt = 10` & Normal, decimal notation \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} `var myInt = 0xa` & {\emph{Hexadecimal}} notation of 10 \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} `var myInt = 0b1001` & {\emph{Binary}} notation of number 10 \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} `var myInt = 0o11` & {\emph{Octal}} notation of number 10 \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Floating Point}}} \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} `var myFloat = 12.345` & Normal, decimal number notation. \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} \{\{nobreak\}\}`var myFloat = 1.2345e1` & Exponential notation of the above number. \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} `var myFloat = ` & Hexadecimal double (which I honestly don't use) \tn % Row Count 17 (+ 3) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{String}}} \tn % Row Count 18 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{All characters should be enclosed in double quotes. We may use the following escape sequences to escape special characters.} \tn % Row Count 21 (+ 3) % Row 11 \SetRowColor{white} `\textbackslash{}0` & NULL Character \tn % Row Count 22 (+ 1) % Row 12 \SetRowColor{LightBackground} ` \textbackslash{}\textbackslash{} ` & Backslash character \tn % Row Count 23 (+ 1) % Row 13 \SetRowColor{white} `\textbackslash{}b` & Backspace \tn % Row Count 24 (+ 1) % Row 14 \SetRowColor{LightBackground} `\textbackslash{}f` & Formfeed \tn % Row Count 25 (+ 1) % Row 15 \SetRowColor{white} `\textbackslash{}n` & Newline \tn % Row Count 26 (+ 1) % Row 16 \SetRowColor{LightBackground} `\textbackslash{}r` & Carriage Return \tn % Row Count 27 (+ 1) % Row 17 \SetRowColor{white} `\textbackslash{}t` & Horizontal Tab \tn % Row Count 28 (+ 1) % Row 18 \SetRowColor{LightBackground} `\textbackslash{}v` & Vertical Tab \tn % Row Count 29 (+ 1) % Row 19 \SetRowColor{white} `\textbackslash{}"` & Double Quote \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.76 cm} x{4.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Literals (cont)}} \tn % Row 20 \SetRowColor{LightBackground} `\textbackslash{}'` & Single Quote \tn % Row Count 1 (+ 1) % Row 21 \SetRowColor{white} `\textbackslash{}055` & Character represented by Octal number 55 \tn % Row Count 3 (+ 2) % Row 22 \SetRowColor{LightBackground} `\textbackslash{}x99` & Character represented by Hex-Number 99 \tn % Row Count 5 (+ 2) % Row 23 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Boolean}}} \tn % Row Count 6 (+ 1) % Row 24 \SetRowColor{LightBackground} `true` & Value is true \tn % Row Count 7 (+ 1) % Row 25 \SetRowColor{white} `false` & Value is false \tn % Row Count 8 (+ 1) % Row 26 \SetRowColor{LightBackground} `nil` & There is no value \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Optionals}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Optionals}} is actually a new datatype within Swift. As the use and understanding of optionals are of utmost importance, let's have a look at them in depth.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`var myInt : Int?` means that the variable named {\emph{myInt}} {\bf{could}} contain an integer value but {\bf{does not}} have to.\{\{nl\}\}If it {\bf{does}} contain a value it will be of type {\emph{Some(Type)}}, if it {\bf{doesn't}} it will be {\emph{None}}} \tn % Row Count 9 (+ 5) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`myInt!` in the code, means that the programmer will make sure that myInt contains a value of type {\emph{Some(T)}}. This is called {\bf{Forced Unwrapping}}.} \tn % Row Count 12 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`var myInt : Int!` allows a normal use (i.e. the use without ? nor !) of the variable in our code. This is called {\bf{Automatic Unwrapping}}.} \tn % Row Count 15 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{To check if an optional contains an actual value or not, we use a code block like this one:\{\{nl\}\}`if myInt != nil \{ //code \} else \{ //code \}`} \tn % Row Count 18 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Another technique is called {\bf{Optional Binding}}, and allows to check for a value directly during declaration of the variable:\{\{nl\}\} `if let myInt = myUncertainInt \{ //code \} else \{ //code \}`} \tn % Row Count 22 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.48 cm} x{5.52 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Operators}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Arithmetic}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `a * b` & {\bf{Multiply}} a by b. \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `a / b` & {\bf{Divide}} a by b. \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} `a \% b` & {\bf{Modulus}} ( remainder after / ) \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} `a + b` & {\bf{Add}} a to b. \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} `a - b` & {\bf{Substract}} a from b. \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} `++a` or `a++` & Pre or post increment. \tn % Row Count 9 (+ 2) % Row 7 \SetRowColor{white} `-{}-a` or `a-{}-` & Pre or post decrement. \tn % Row Count 11 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Comparision}}} \tn % Row Count 12 (+ 1) % Row 9 \SetRowColor{white} `a == b` & A is {\bf{equal}} to b. \tn % Row Count 13 (+ 1) % Row 10 \SetRowColor{LightBackground} `a != b` & A is {\bf{not}} equal to b. \tn % Row Count 14 (+ 1) % Row 11 \SetRowColor{white} `a \textgreater{} b` & A is {\bf{greater}} than b. \tn % Row Count 15 (+ 1) % Row 12 \SetRowColor{LightBackground} `a \textless{} b` & A is {\bf{less}} than b. \tn % Row Count 16 (+ 1) % Row 13 \SetRowColor{white} `a \textgreater{}= b` & A is {\bf{greater}} or equal to b. \tn % Row Count 18 (+ 2) % Row 14 \SetRowColor{LightBackground} `a \textless{}= b` & A is {\bf{less}} or equal to b. \tn % Row Count 20 (+ 2) % Row 15 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Logical}}} \tn % Row Count 21 (+ 1) % Row 16 \SetRowColor{LightBackground} ` a \&\& b` & Is true while a {\bf{and}} b are true. \tn % Row Count 23 (+ 2) % Row 17 \SetRowColor{white} `a || b` & Is true while a {\bf{or}} b are true. \tn % Row Count 25 (+ 2) % Row 18 \SetRowColor{LightBackground} `a ! b` & Is true while {\bf{only one}} of a or b is true. \tn % Row Count 27 (+ 2) % Row 19 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Bitwise}}} \tn % Row Count 28 (+ 1) % Row 20 \SetRowColor{LightBackground} `\&` & Bitwise {\bf{and}}. \tn % Row Count 29 (+ 1) % Row 21 \SetRowColor{white} `|` & Bitwise {\bf{or}}. \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.48 cm} x{5.52 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Operators (cont)}} \tn % Row 22 \SetRowColor{LightBackground} `\textasciicircum{}` & Bitwise {\bf{xor}}. \tn % Row Count 1 (+ 1) % Row 23 \SetRowColor{white} `\textasciitilde{}` & Bitwise ones {\bf{complement}}. \tn % Row Count 3 (+ 2) % Row 24 \SetRowColor{LightBackground} `\textless{}\textless{}` & Bitwise {\bf{shl}}. \tn % Row Count 4 (+ 1) % Row 25 \SetRowColor{white} `\textgreater{}\textgreater{}` & Bitwise {\bf{shr}} \tn % Row Count 5 (+ 1) % Row 26 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Assignment}}} \tn % Row Count 6 (+ 1) % Row 27 \SetRowColor{white} `a = b` & Assign b to a. \tn % Row Count 7 (+ 1) % Row 28 \SetRowColor{LightBackground} `a += b` & Assign a+b to a. \tn % Row Count 8 (+ 1) % Row 29 \SetRowColor{white} `a -= b` & Assign a-b to a. \tn % Row Count 9 (+ 1) % Row 30 \SetRowColor{LightBackground} `a *= b` & Assign a*b to a. \tn % Row Count 10 (+ 1) % Row 31 \SetRowColor{white} `a /= b` & Assign a/b to a. \tn % Row Count 11 (+ 1) % Row 32 \SetRowColor{LightBackground} `a \%= b` & Assign a\%b to a. \tn % Row Count 12 (+ 1) % Row 33 \SetRowColor{white} `a \textless{}\textless{}= b` & Assign a shl b to a. \tn % Row Count 13 (+ 1) % Row 34 \SetRowColor{LightBackground} `a \textgreater{}\textgreater{}= b` & Assign a shr b to a. \tn % Row Count 14 (+ 1) % Row 35 \SetRowColor{white} `a \&= b` & Assign a and b to a. \tn % Row Count 15 (+ 1) % Row 36 \SetRowColor{LightBackground} `a \textasciicircum{}= b` & Assign a xor b to a. \tn % Row Count 16 (+ 1) % Row 37 \SetRowColor{white} `a |= b` & Assign a or b to a. \tn % Row Count 17 (+ 1) % Row 38 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Range}}} \tn % Row Count 18 (+ 1) % Row 39 \SetRowColor{white} `( 1..4 )` & Closed range, i.e. 1,2,3,4. \tn % Row Count 19 (+ 1) % Row 40 \SetRowColor{LightBackground} `( 1..\textless{}4 )` & Half open range, i.e. 1,2,3. \tn % Row Count 21 (+ 2) % Row 41 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Miscellaneous}}} \tn % Row Count 22 (+ 1) % Row 42 \SetRowColor{LightBackground} `( a==b ) ? x : y` & Ternary operator.\{\{nl\}\}If a is equal to b then return x, if not return y. \tn % Row Count 25 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Strings}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}`var myString = ""`\{\{nl\}\}`var myString = String()` & Both create an empty string. \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} `var myString = "one"`\{\{nl\}\}`myString += " two"` & Concatenates two strings, and returns "one two". \tn % Row Count 7 (+ 3) % Row 2 \SetRowColor{LightBackground} `var myString = "one " + "two"` & Another wat to concatenate two strings. \tn % Row Count 9 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\{\{nobreak\}\}`var myName = "Tony"`\{\{nl\}\} `var myGreeting = "Hello \textbackslash{}(myName)"`} \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} & This inserts a string into another string. Note that this is not limited to strings, but also works with numbers. \tn % Row Count 17 (+ 6) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\{\{nobreak\}\}`var myAge = 18`\{\{nl\}\} `var myGreeting = "Age: \textbackslash{}(myAge)"`} \tn % Row Count 19 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{String relevant functions and methods}} \tn % Row 0 \SetRowColor{LightBackground} `myString.isEmpty` & Returns true if empty and false if not. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`var myString = "two words"`} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}`myString.localizedCapitalized` & Returns "Two Words" \tn % Row Count 6 (+ 3) % Row 3 \SetRowColor{white} \seqsplit{`myString.localizedLowercase`} & Returns "two words"\{\{nl\}\}(this appears to be no change, but in fact, all characters are converted to lowercase. \tn % Row Count 12 (+ 6) % Row 4 \SetRowColor{LightBackground} \seqsplit{`myString.localizedUppercase`} & Returns "TWO WORDS" \tn % Row Count 14 (+ 2) % Row 5 \SetRowColor{white} `count( myString )` & Returns 9 \tn % Row Count 15 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{fa-info-circle\}\}Consult: \{\{popup="https://developer.apple.com/reference/swift/string"\}\}\{\{fa-apple\}\}Docs\{\{/link\}\} for further information.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Arrays}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}`var myArray:Array\textless{}Type\textgreater{} = Array()` & This creates an empty array. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \{\{nobreak\}\}`var myArray:{[}Type{]} = {[}{]}` & Shorthand method to create an array with values X, Y and Z \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}`var myArray = {[}Type{]}()` & This is the shortest of the shorthands available. \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} \{\{nobreak\}\}`var myArray = Array(repeating: X, count: X)` & This allows to create an array with a default value and a given size. \tn % Row Count 13 (+ 4) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}`myArray{[}0{]}` & Returns the first element of the array. \tn % Row Count 15 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Multi-Dimensional Arrays}}} \tn % Row Count 16 (+ 1) % Row 6 \SetRowColor{LightBackground} \{\{nobreak\}\}`var myArray:Array\textless{}Array\textless{}Type\textgreater{}\textgreater{} = Array(Array())` & Same as above, but \seqsplit{multi-dimensionally.} \tn % Row Count 19 (+ 3) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\{\{nobreak\}\}`var myArray:{[}{[}Type{]}{]} = {[}{[}{]}{]}`} \tn % Row Count 20 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{nobreak\}\}`var myArray = {[}{[}Type{]}{]}()`} \tn % Row Count 21 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Array relevant functions and methods}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}`myArray.count` & Returns the number of items go an array. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \{\{nobreak\}\}`myArray.isEmpty` & Returns true if it is empty and false if not. \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}`myArray.first` & Returns first element of array \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \{\{nobreak\}\}`myArray.last` & Returns last element of array \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \{\{nobreak\}\}`myArray.append({\emph{value}})` & Appends an element to an array (there are variations to this) \tn % Row Count 13 (+ 4) % Row 5 \SetRowColor{white} \{\{nobreak\}\}`myArray.insert({\emph{value}} at:X)` & Inserts {\emph{value}} at position X \tn % Row Count 16 (+ 3) % Row 6 \SetRowColor{LightBackground} \{\{nobreak\}\}`myArray.remove(at: X)` & Removes the element at position X \tn % Row Count 18 (+ 2) % Row 7 \SetRowColor{white} \{\{nobreak\}\}`myArray.index(of: {\emph{value}})` & Returns the index (i.e. position) of {\emph{value}} inside the array. \tn % Row Count 22 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{fa-info-circle\}\}Consult: \{\{popup="https://developer.apple.com/reference/swift/array"\}\}\{\{fa-apple\}\}Docs\{\{/link\}\} for further information.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Dictionaries}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}`var myDict: {[}{\emph{Type}}:{\emph{Type}}{]} = {[}:{]}` & This creates an empty dictionary. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \{\{nobreak\}\}`var myDict = {[}{\emph{key}}:{\emph{value}}{]}` & Creates a dictionary with given {\emph{key}}:{\emph{value}} pairs. \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}`for ({\emph{key}}, {\emph{value}}) in myDict \{ \{\{nl\}\}~//code\{\{nl\}\} \}` & Iterate over a dictionary accessing {\emph{key}}:{\emph{value}} pairs in each iteration. \tn % Row Count 10 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Dictionaries relevant functions and methods}} \tn % Row 0 \SetRowColor{LightBackground} `myDict.count` & Returns the amount of data pairs available. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} `myDict.isEmpty` & Returns true if the dictionary is empty. \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} `myDict.updateValue({\emph{value}}, forKey: {\emph{key}})` & Updates a {\emph{value}} inside the dictionary. \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} \seqsplit{`myDict.index(forKey:} {\emph{key}})` & Returns the index where given {\emph{key}} is located. \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{`myDict.removeValue(forKey:} {\emph{key}})` & Removes a {\emph{key}}:{\emph{value}} pair. \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{fa-info-circle\}\} Consult: \{\{popup="https://developer.apple.com/reference/swift/dictionary"\}\}\{\{fa-apple\}\}Docs\{\{/link\}\} for further information.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Flow control ({\emph{Decision making}})}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}{\bf{if..else}}\{\{nl\}\}`if a==b \{\{\{nl\}\}~//{\emph{code}}\{\{nl\}\}\} else \{\{\{nl\}\}~//{\emph{code}}\{\{nl\}\}\}` & If a is equal to b, the first block of code is executed. If not, then the last block of code is executed. The {\emph{else}} block may be present or not. \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} \{\{nobreak\}\}{\bf{Switch}} \{\{nl\}\}`switch {\emph{variable}} \{ \{\{nl\}\} ~case 1: \{\{nl\}\} ~~//code1 \{\{nl\}\} ~case 2: \{\{nl\}\} ~~//code2 \{\{nl\}\} ~~fallthrough \{\{nl\}\} ~case 3: \{\{nl\}\} ~~//code 3 \{\{nl\}\} ~default: \{\{nl\}\} ~~//code default \{\{nl\}\} \} \{\{nl\}\}` & The switch statement checks a variable for a given value. No break statement is needed. If we want a fallthrough to happen, we need to specify this in the code block. In this example, case 2 always gets executed together with case 3. The default statement catches all non matching cases. \tn % Row Count 23 (+ 15) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{nobreak\}\}{\bf{Ternary oprerator}}\{\{nl\}\}`( condition) ? value1 : value2`} \tn % Row Count 25 (+ 2) % Row 3 \SetRowColor{white} ~ & The ternary operator is described in the {\emph{Miscellaneous}} section of the {\emph{Operators}} block as well. It is basically a very condensed {\emph{if..then}} statement. \tn % Row Count 33 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Flow control ({\emph{Decision making}}) (cont)}} \tn % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{nobreak\}\}{\bf{Nil-Coalescing oprerator}}\{\{nl\}\}`( a ?? b )`} \tn % Row Count 2 (+ 2) % Row 5 \SetRowColor{white} & Unwraps a and returns it, if it is NOT nil. If it is nil, then b is returned. \tn % Row Count 6 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Flow Control ( Looping )}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}{\bf{for..in}}\{\{nl\}\}`for {\emph{value}} in {\emph{array}} \{ \{\{nl\}\} ~//code \{\{nl\}\} \}` & This loop, iterates over each of the elements within an array (or dictionary) and the variable {\emph{value}} takes the value of an element, one at a time. \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{\{\{nobreak\}\}{\bf{for loop}}\{\{nl\}\}`for initCond; chkCond; operator \{ \{\{nl\}\}~//code\{\{nl\}\} \}`} \tn % Row Count 10 (+ 2) % Row 2 \SetRowColor{LightBackground} & The for loop basically a counts from an initial condition ({\emph{iniCond}}) to an end condition ({\emph{!chkCond}}) using an operator to change the value of the counter. \tn % Row Count 18 (+ 8) % Row 3 \SetRowColor{white} {\bf{while loop}}\{\{nl\}\} `while condition \{ \{\{nl\}\}~//code\{\{nl\}\} \}` & This code block is executed whenever the {\emph{condition}} is true. The {\emph{condition}} thus gets evaluated at the beginning. \tn % Row Count 24 (+ 6) % Row 4 \SetRowColor{LightBackground} {\bf{do..while loop}}\{\{nl\}\}`do \{\{\{nl\}\} ~//code\{\{nl\}\} \} while condition` & This is a special case of the while loop, as the evaluation is at the end, instead of the beginning. \{\{fa-exclamation\}\}The code block is executed at least {\emph{once}}. \tn % Row Count 33 (+ 9) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Flow Control ( Statements )}} \tn % Row 0 \SetRowColor{LightBackground} \{\{nobreak\}\}`fallthrough` & As seen before, the fall through statement is used within the {\emph{switch}} block. It allows tho execute the next case of the matching section. \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} `continue` & Continue allows to skip the rest of a iteration and \tn % Row Count 9 (+ 3) % Row 2 \SetRowColor{LightBackground} `break ` & This statement allows to break out of a loop. It skips the rest of a iteration and aborts all subsequent loops. \tn % Row Count 14 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{nobreak\}\}`{\bf{func}} {\emph{fName}}({\emph{parameter}}:{\bf{Type}})-\textgreater{}{\bf{Type}} \{\{\{nl\}\}~//code\{\{nl\}\}~{\bf{return}} {\emph{Value}}\{\{nl\}\}\}`} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{The function has to return a value of the same type that has been declaren in it's header. If we don't declare a return value, none has to be returned. Parameters are optional as well. A function without return value nor parameters, could look like this:} \tn % Row Count 9 (+ 6) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{nobreak\}\}`{\bf{func}} {\emph{fName}}() \{\{\{nl\}\}~//code\{\{nl\}\}\}`} \tn % Row Count 11 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{The function parameters may have a label. This would look like this (only declaration):} \tn % Row Count 13 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{nobreak\}\}`{\bf{func}} {\emph{fName}}({\emph{label}} {\emph{parameter}}:{\bf{Type}})-\textgreater{}{\bf{Type}} \{\{\{nl\}\}`} \tn % Row Count 15 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Per definition, all function parameters are passed as constants, so they are immutable. If we had to make changes to those parameters inside our function, and have this changes reflected outside as well, we'd use in/out parameters.} \tn % Row Count 20 (+ 5) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{nobreak\}\}`{\bf{func}} {\emph{fName}}({\emph{label}} {\emph{parameter}}:{\bf{inout Type}})-\textgreater{}{\bf{Type}} \{\{\{nl\}\}`} \tn % Row Count 22 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Variadic parameters allow us to use an undefined amount of parameters (of the same type) inside our function.} \tn % Row Count 25 (+ 3) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{nobreak\}\}`{\bf{func}} {\emph{fName}}({\emph{parameter}}:{\bf{Type...}})-\textgreater{}{\bf{Type}} \{\{\{nl\}\}`} \tn % Row Count 27 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{And lastly, if we wanted a default value to be assumed for a given parameter, we'd use:} \tn % Row Count 29 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{nobreak\}\}`{\bf{func}} {\emph{fName}}({\emph{label}} {\emph{parameter}}:{\bf{Type}}={\emph{value}})-\textgreater{}{\bf{Type}} \{\{\{nl\}\}`} \tn % Row Count 31 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\{\{fa-info-circle\}\}Consult: \{\{popup="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift\_Programming\_Language/Functions.html"\}\}\{\{fa-apple\}\}Docs\{\{/link\}\} for further information.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} x{7.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Closures}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`\{( {\bf{Parameters}} ) -\textgreater{} {\bf{Type in}} //code \}`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} & Same as a function but without a name. Can be passed around in functions itself or variables. \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{let}} {\emph{sum}}= \{( n1:{\bf{Int}}, n2:{\bf{Int}})-\textgreater{}{\bf{Int in}}\{\{nl\}\}~{\bf{return}} n1+n2\{\{nl\}\}\}`} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} & This would return the sum of two numbers when called like this: \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{let}} myResult = {\emph{sum}}(2,3)`} \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} & If the type of variables can be inferred, this is true for the sort(by:) function, the closure doesn't have to explicitly declare all types of variables. So instead of writing... \tn % Row Count 14 (+ 5) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{let}} myElements = {[}"two","three","four"{]}\{\{nl\}\}{\bf{let}} mySortedElements = \{\{nl\}\}myElements.sorted(by: \{( prm1: {\bf{String}}, prm2: {\bf{String}} )-\textgreater{}{\bf{Bool in return}} prm1.characters.count \textgreater{} prm2.characters.count\})`} \tn % Row Count 19 (+ 5) % Row 7 \SetRowColor{white} & ...we could write (only closure part)... \tn % Row Count 21 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{let}} mySortedElements = \{\{nl\}\}myElements.sorted(by: \{ prm1, prm2 {\bf{in return}} prm1.characters.count \textgreater{} prm2.characters.count\})`} \tn % Row Count 24 (+ 3) % Row 9 \SetRowColor{white} & This would return {\emph{"three","four","two"}}. But it doesn't stop here. To write it even shorter, we can skip the return keyword as well. \tn % Row Count 28 (+ 4) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{let}} mySortedElements = \{\{nl\}\}myElements.sorted(by: \{ prm1, prm2 {\bf{in}} prm1.characters.count \textgreater{} prm2.characters.count\})`} \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{p{0.8 cm} x{7.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Closures (cont)}} \tn % Row 11 \SetRowColor{LightBackground} & Instead of declaring variables, names we can also use shorthands for the parameters. This could look like... \tn % Row Count 3 (+ 3) % Row 12 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`{\bf{var}} {\emph{myTest}}: ({\bf{Int}}, {\bf{Int}})-\textgreater{}{\bf{Int}}\{\{nl\}\}{\emph{myTest}} = \{ \$0 + \$1 \}\{\{nl\}\}{\emph{myTest}}(1,2) //This would return 3`} \tn % Row Count 6 (+ 3) % Row 13 \SetRowColor{LightBackground} & Swift is even able o infer most of the closure and reduce it to the minimum. \tn % Row Count 9 (+ 3) % Row 14 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`{\bf{let}} mySortedElements = \{\{nl\}\}myElements.sorted(by: \textgreater{} )`} \tn % Row Count 11 (+ 2) % Row 15 \SetRowColor{LightBackground} & Occasionaly, if a closure results to be very long, it can be written at the end of a header, after the parameter closing parenthesis. \tn % Row Count 15 (+ 4) % Row 16 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`{\bf{let}} mySortedElements = \{\{nl\}\}myElements.sorted() \{ \{\{nl\}\}~ \$0.characters.count \textless{} \$1.characters.count \{\{nl\}\} \}`} \tn % Row Count 18 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{fa-info-circle\}\} Consult \{\{popup="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift\_Programming\_Language/Closures.html"\}\}\{\{fa-apple\}\}Docs\{\{/link\}\} for further information.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} x{7.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Enums}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{enum}} Name \{\{\{nl\}\}~{\bf{case}} Label1 {[}= Value{]}\{\{nl\}\}~{\bf{case}} Label2 ({\bf{Type}})\{\{nl\}\}\}`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} & Enumeratios are a way of labelling certain values to be used in the code. \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{enum}} Weekdays \{\{\{nl\}\}~case Mon, Tue, Wed, Thu, Fri, Sat, Sun \{\{nl\}\}\}`} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} & The way of referring to it is... \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`var myWeekDay = Weekdays.Mon`} \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{fa-info-circle\}\}Consult \{\{popup="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift\_Programming\_Language/Enumerations.html"\}\}\{\{fa-apple\}\}Docs\{\{/link\}\} for further information.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} x{7.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Structs}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{struct}} PointStruct \{ \{\{nl\}\}~ {\bf{var}} X:{\bf{Int}},\{\{nl\}\}~ {\bf{var}} Y:{\bf{Int}} \{\{nl\}\}\}`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} & Structs may be used to encapsulate data. They can even have a {\emph{initializer}} to provide the struct with data. \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{struct}} PointStruct \{ \{\{nl\}\}~ {\bf{var}} X:{\bf{Int}},\{\{nl\}\}~ {\bf{var}} Y:{\bf{Int}}\{\{nl\}\}~ {\bf{init}}( x: {\bf{Int}}, y: {\bf{Int}}) \{\{\{nl\}\}~ ~ {\bf{self}}.X = x\{\{nl\}\}~ ~ {\bf{self}}.Y = y \{\{nl\}\} ~\} \{\{nl\}\} \}`} \tn % Row Count 10 (+ 5) % Row 3 \SetRowColor{white} & To declare a variable with this {\emph{struct}} we would write... \tn % Row Count 12 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{let}} myPoint = PointStruct( x: 1, y: 2 )`} \tn % Row Count 13 (+ 1) % Row 5 \SetRowColor{white} & Structures are similar to classes with a few {\emph{important}} differences, being the most important one that their instances are always {\bf{passed by value}}. \tn % Row Count 18 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{fa-info-circle\}\}Consult \{\{popup="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift\_Programming\_Language/ClassesAndStructures.html"\}\}\{\{fa-apple\}\}Docs\{\{/link\}\} for further information.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Classes}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{class}} MyClass : MyProtocol \{ \{\{nl\}\}~ {\bf{var}} X:{\bf{Int}},\{\{nl\}\}~ {\bf{init}}() \{\{\{nl\}\}~ //code\{\{nl\}\}~ \}\{\{nl\}\}~ {\bf{func}} myMethod() \{\{\{nl\}\}~ //code\{\{nl\}\}~ \}\{\{nl\}\}~ {\bf{deinit}}() \{\{\{nl\}\}~ //code\{\{nl\}\}~ \}\{\{nl\}\}\}`} \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \{\{nobreak\}\}{\bf{Objects}} & Can be enumerations, variables or constants.\{\{nl\}\}Declaration can be preceded by: {\emph{lazy}}, {\emph{static}}, {\emph{private}} or {\emph{public}}. \tn % Row Count 12 (+ 6) % Row 2 \SetRowColor{LightBackground} \{\{nobreak\}\}{\bf{Methods}} & The methods {\emph{init()}} and {\emph{deinit()}} are special methods which are called upon instantiation and destruction of the class.\{\{nl\}\}The method declaration can be preceded by one of the options: {\emph{private}}, {\emph{public}}, {\emph{mutating}}, {\emph{class}} or {\emph{static}} \tn % Row Count 23 (+ 11) % Row 3 \SetRowColor{white} \{\{nobreak\}\}{\bf{Protocols}} & {\emph{Protocols}} do specify a certain blueprint a class conforms to. See below \{\{fa-arrow-down\}\} \tn % Row Count 27 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Protocols}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`{\bf{protocol}} MyProtocol \{\{\{nl\}\}~ {\bf{var}} myVariable : {\bf{Type}} \{ get set \} \{\{nl\}\}~ {\bf{func}} myMethod ( parameter: {\bf{Type}})-\textgreater{}{\bf{Type}}\{\{nl\}\}\}`} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{A protocol defines a blueprint of {\emph{methods}}, {\emph{properties}} and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a {\emph{class}}, {\emph{struct}}, or {\emph{enum}} to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.} \tn % Row Count 12 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{\{\{fa-info-circle\}\}Consult: \{\{popup="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift\_Programming\_Language/Protocols.html"\}\}\{\{fa-apple\}\}Docs\{\{/link\}\} for further information.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}