\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{nixik09} \pdfinfo{ /Title (swift.pdf) /Creator (Cheatography) /Author (nixik09) /Subject (Swift 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}{DBCCD5} \definecolor{LightBackground}{HTML}{F6F2F4} \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 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{nixik09} via \textcolor{DarkBackground}{\uline{cheatography.com/212204/cs/47199/}}} \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}nixik09 \\ \uline{cheatography.com/nixik09} \\ \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 20th November, 2025.\\ 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.4 cm} x{3.6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Simple Values}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`let` for constants; `var` for variables} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Constants: value doesn't need to be known at compile time, but must assign it a value exactly once} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Rule for types: constant or variable must have the same type as the value you want to assign it.} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Inferring Types: Type can be inferred if value provided when creating constant/var.} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} Specifying types: Write after the variable, separated by colon & `let explicitDouble: Double = 70` \tn % Row Count 10 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Values are never implicitly converted to another type. If conversion required, must be done explicitly} \tn % Row Count 13 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`let lottoString = "Today's winning lotto number is " \{\{nl\}\} let lottoNumber = 94\{\{nl\}\} let lottoSentence = lottoString + String(lottoNumber)`} \tn % Row Count 16 (+ 3) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Backslash string interpolation} \tn % Row Count 17 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`let friendCount = 2\{\{nl\}\}let friendCountString = "I have \textbackslash{}(friendCount) number of \{\{nl\}\} friends."`} \tn % Row Count 19 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Use three double quotation marks for strings that take up multiple lines.} \tn % Row Count 21 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Create arrays and dictionaries using brackets, and access their elements by writing the index or key in brackets.} \tn % Row Count 24 (+ 3) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`var toys = {[}"labubu", "simski", "furby"{]}\{\{nl\}\} toys{[}1{]} = "fuggler" \{\{nl\}\} toys.append("jellycat") \{\{nl\}\} var bestItemOfThing = {[} "summer fruit" : "mango",{]} \{\{nl\}\} bestItemOfThing{[}"summer fruit"{]} = "peach"`} \tn % Row Count 29 (+ 5) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Arrays grow automatically.} \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4.4 cm} x{3.6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Simple Values (cont)}} \tn % Row 13 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{For empty array, write `excellentFruits = {[}{]}`. For an empty dictionary, write `mediocreFruits = {[}:{]}`.} \tn % Row Count 3 (+ 3) % Row 14 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{If assigning empty array or dict to new var, you need to specify type.} \tn % Row Count 5 (+ 2) % Row 15 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`let arrayOfGoodCoffeeCafes: {[}String{]} = {[}{]} \{\{nl\}\} let \seqsplit{dictCafesWithMatchaAndRatings:} {[}String: Float{]} = {[}:{]}`} \tn % Row Count 8 (+ 3) \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}{Control Flow}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Conditionals}}: `if`, `switch`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Loops}}: `for-in`, `while`, `repeat - while `} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Parentheses around condition/loop variable are optional, braces are required} \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`let croissantScores = {[}1.2, 2.3, 2.2, 4{]}\{\{nl\}\} for score in croissantScores \{ \{\{nl\}\} ~ ~ if score \textgreater{} 2.5 \{ \{\{nl\}\} ~ ~ ~ print("Great croissant!!") \{\{nl\}\} ~ ~ \} \{\{nl\}\} \}`} \tn % Row Count 9 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{In `if` statement, conditional must be a boolean expression. `if score \{ ... \}` is an error, not implicit comparison to zero.} \tn % Row Count 12 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{You can assign if conditions to an assignment} \tn % Row Count 13 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{` let scoreDecoration = if teamScore \textgreater{} 10 \{ \{\{nl\}\} ~ "🎂" \{\{nl\}\} \} else \{ \{\{nl\}\} ~ "" \{\{nl\}\} \} \{\{nl\}\} print("Score:", teamScore, scoreDecoration)`} \tn % Row Count 17 (+ 4) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{Optionals}}: either contains a value or contains `nil` to indicate a value is missing. A question mark after type of value marks it as optional.} \tn % Row Count 20 (+ 3) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`var optionalString: String? = "Hello" \{\{nl\}\} print(optionalString == nil) \{\{nl\}\} // prints "false"`} \tn % Row Count 22 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Use `if` and `let` to work with missing values. If optional value is `nil`, conditional is false and code in braces is skipped. Otherwise, optional value is unwrapped and assigned to constant after `let`, which makes the unwrapped value available inside block of code.} \tn % Row Count 28 (+ 6) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`var optionalName: String? = "Big Dog" \{\{nl\}\} var greeting = "Hello!" \{\{nl\}\} if let name = optionalName \{ \{\{nl\}\} ~ greeting = "Hello, \textbackslash{}(name)" \{\{nl\}\} \} else \{ \{\{nl\}\} ~ greeting = "Wow!" \{\{nl\}\} \} `} \tn % Row Count 33 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Control Flow (cont)}} \tn % Row 11 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`??` operator: If optional value missing, default value used instead} \tn % Row Count 2 (+ 2) % Row 12 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{`let holidayInNov: String? = nil \{\{nl\}\} let holidayInDec: String = "Bali" \{\{nl\}\} let welcomeMsg = "Enjoy \textbackslash{}(holidayInNov ?? holidayInDec)" `} \tn % Row Count 5 (+ 3) % Row 13 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Switches}}: support any kind of data and variety of comparisons} \tn % Row Count 7 (+ 2) % Row 14 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{After executing the code inside the switch case that matched, program exits from the switch statement. Execution doesn't continue to the next case, so you don't need to explicitly break out of the switch at the end of each case's code.} \tn % Row Count 12 (+ 5) % Row 15 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{`let perfume = "Eau Rose" \{\{nl\}\} switch perfume \{ \{\{nl\}\} case "Electric Cherry": \{\{nl\}\} ~ print("Jasmine sambac, ambrettolide musk") \{\{nl\}\} case "On a Date", "Born In Roma": \{\{nl\}\} ~ print("These have a black currant note.") \{\{nl\}\} case let x where x.hasSuffix("rose"): \{\{nl\}\} ~ print("Is it a fragrance with \textbackslash{}(x)?") \{\{nl\}\} default: \{\{nl\}\} ~ print("A delicious choice.") \{\{nl\}\} \} \{\{nl\}\} // Prints "Is it a fragrance with rose?" `} \tn % Row Count 21 (+ 9) % Row 16 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{`for-in`}}: Use to iterate over items in a dict by providing pair of names for each key-value pair.} \tn % Row Count 24 (+ 3) % Row 17 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{` let interestingNumbers = {[} \{\{nl\}\} ~ "country codes from my holidays": {[}66, 1, 86, 82, 45{]}, \{\{nl\}\} ~ "cricket players": {[}49, 56, 26, 30{]}, \{\{nl\}\} {]} \{\{nl\}\} \{\{nl\}\} var largest = 0 \{\{nl\}\} for (\_, numbers) in interestingNumbers \{ \{\{nl\}\} ~ for number in numbers \{ \{\{nl\}\} ~ ~ if number \textgreater{} largest \{ \{\{nl\}\} ~ ~ ~ largest = number \{\{nl\}\} ~ ~ \} \{\{nl\}\} ~ \} \{\{nl\}\} \} \{\{nl\}\} `} \tn % Row Count 33 (+ 9) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}