\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{{[}deleted{]}} \pdfinfo{ /Title (swift-4-syntax-beginner.pdf) /Creator (Cheatography) /Author ({[}deleted{]}) /Subject (Swift 4 Syntax (beginner) 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}{7C6F64} \definecolor{LightBackground}{HTML}{F6F6F5} \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 Syntax (beginner) Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{{[}deleted{]}} via \textcolor{DarkBackground}{\uline{cheatography.com/66851/cs/16721/}}} \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}{[}deleted{]} \\ \uline{cheatography.com/deleted-66851} \\ \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 19th August, 2018.\\ 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Control Flow}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{If / else if / else}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{if var1 == var2 \{ \newline print("Ok") \newline \} else if var1 \textless{} var2 \{ \newline print("Not ok") \newline \} else \{ \newline print("Maybe ok") \newline \}} \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}{Switch}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{switch}} (var1, var2) \{ \newline {\bf{case}} (3, 8): \newline {\emph{print}}("Sure") \newline {\bf{case}} (1...15, 20..\textless{}25): \newline {\emph{print}}("Ranges") \newline {\bf{case}} (let localVar1, let localVar2) where localVar1 + localVar2 \textgreater{} 30: \newline {\emph{print}}(localVar1, localVar2) \newline {\bf{default}}: \newline {\emph{print}}("Nothing") \newline \}} \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}{while / repeat while}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var digit = 5 \newline \newline while digit \textgreater{} 0 \{ \newline digit -= 1 \newline print(digit) \newline \} \newline \newline repeat \{ \newline digit -= 1 \newline print(digit) \newline \} while digit \textgreater{}= 0} \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}{for in}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{for item in {[}"item1", "item2"{]} \{ \newline print(item) \newline \}} \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}{Guard}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{guard var1 \textgreater{}= 3 else \{ \newline print("Lower than 2") \newline continue \newline \}} \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}{Optionals}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var optional1: String? \newline var optional2: Int? = 10} \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}{Classes}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Basic class}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Adventurer \{ \newline var name: String \newline var hp: Int \newline let maxHealth: Int = 100 \newline \newline static var credo = "Defend the helpless" \newline \newline var specialMove: String? \newline \newline var toFullHp: Int \{ \newline return maxHealth - hp \newline \} \newline \newline var health: Int \{ \newline get \{ \newline return hp \newline \} \newline set \{ \newline hp = newValue \newline \} \newline \} \newline \newline init(name: String, hp: Int) \{ \newline self.name = name \newline self.hp = hp \newline \} \newline \newline convenience init(name: String) \{ \newline self.init(name: name, hp: 100) \newline \} \newline \}} \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}{Subclass}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Adventurer \{ \newline var name: String \newline static var credo = "Defend the helpless" \newline \newline init(name: String) \{ \newline self.name = name \newline \} \newline \newline func attack(damage: Int) \{ \newline print("Attacking for \textbackslash{}(damage) damage.") \newline \} \newline \newline static func printCredo() \{ \newline print(credo) \newline \} \newline \} \newline \newline class Ranger: Adventurer \{ \newline \newline var classAdvantage: String \newline \newline init(name: String, advantage: String) \{ \newline self.classAdvantage = advantage \newline super.init(name: name) \newline \} \newline \newline override func attack(damage: Int) \{ \newline print("Ranger attack for \textbackslash{}(damage)") \newline \} \newline \} \newline \newline var adventurer = Adventurer(name: "Harrison") \newline var ranger = Ranger(name: "Steven", advantage: "Stealth")} \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}{Collections}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Array}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var Array: Array\textless{}String\textgreater{} = {[}{]} \newline var Array2 = Array\textless{}String\textgreater{}() \newline var Array3 = {[}Double{]}() \newline var Array4: {[}Int{]} = {[}{]}} \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}{Dictionary}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var dict1: Dictionary\textless{}Int, Int\textgreater{} = {[}:{]} \newline var dict2 = Dictionary\textless{}Int, String\textgreater{}() \newline var dict3 = {[}String : String{]}() \newline var dict4: {[}String : String{]} = {[}:{]}} \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}{Tuple}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var tuple = (string: "str", int: 1, isBool: true) \newline \newline var tuple2: (string: String, int: Int, isBool: Bool) \newline tuple2 = ("str", 1, true) \newline \newline var tuple3: (String, Int, Bool) = ("str", 1, true)} \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}{Set}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var set = Set\textless{}Int\textgreater{}() \newline var set2: Set\textless{}Int\textgreater{} = {[}{]}} \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}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Basic function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{func myFunction(value: String) -\textgreater{} Bool \{ \newline print("Basic function") \newline return true \newline \}} \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}{Complex function}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{func myFunction(value1: String = "Value1", value2: Int = 2) -\textgreater{} (return1: String, return2: Int) \{ \newline print("Complex function") \newline return ("string", 3) \newline \}} \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}{Functions as parameter}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{func superFunc(value: Int) -\textgreater{} Int \{ \newline return value * 4 \newline \} \newline \newline func myFunc(value: Int, anotherFunc: (Int) -\textgreater{} Int) \{ \newline let var1 = anotherFunc(value) \newline print(var1) \newline \} \newline \newline myFunc(2, superFunc)} \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}{Closures}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Empty Closure}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var closureDeclaration: () -\textgreater{} () = \{\}} \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}{Basic shorthand}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var closure = \{ (parameterString: String) -\textgreater{} Void in \newline print(parameterString) \newline \}} \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}{Closure shorthand}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var closure: (String) -\textgreater{} String = \{ value in \newline return "\textbackslash{}(value)" \newline \}} \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}{Closures as function parameters}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{func myFunc(closure: ({[}String{]}) -\textgreater{} Void) \{ \newline let array = {[}"value1", "value2", "value3", "value4"{]} \newline closure(array) \newline \} \newline \newline myFunc \{ (values) in \newline for value in values \{ \newline print("\textbackslash{}(value)") \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}