{"id":2586,"date":"2026-02-02T12:09:18","date_gmt":"2026-02-02T12:09:18","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2586"},"modified":"2026-02-02T12:09:18","modified_gmt":"2026-02-02T12:09:18","slug":"chapter-3-swift-syntax","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-3-swift-syntax\/","title":{"rendered":"Chapter 3: Swift Syntax"},"content":{"rendered":"<p dir=\"auto\"><strong>Swift Syntax<\/strong> \u2014 written as if I\u2019m sitting next to you, explaining slowly, with many small examples, comparisons to other languages you might know (C\/C++\/Python), common traps, and why Swift does things a certain way.<\/p>\n<p dir=\"auto\">We will cover the <strong>most important syntax patterns<\/strong> you will see in almost every Swift file.<\/p>\n<h3 dir=\"auto\">1. Variables &amp; Constants \u2013 The foundation<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Immutable (cannot be changed) \u2013 use this MOST of the time\r\nlet maximumAttempts = 5\r\nlet apiKey = \"sk-abc123xyz\"\r\nlet isProduction = true\r\n\r\n\/\/ Mutable (can be changed)\r\nvar score = 0\r\nvar username = \"guest123\"\r\nvar temperature = 28.7\r\n\r\nscore += 10\r\nusername = \"player_789\"\r\ntemperature -= 2.3<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Important style rule in modern Swift (2024\u20132026):<\/strong><\/p>\n<ul dir=\"auto\">\n<li><strong>Default = let<\/strong><\/li>\n<li>Use var<strong>only<\/strong> when you are sure the value will actually change<\/li>\n<\/ul>\n<p dir=\"auto\">Very common beginner mistake:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var name = \"Rahul\"          \/\/ \u2190 wrong if you never reassign it!\r\nvar email = \"rahul@xyz.com\" \/\/ \u2190 wrong if never changed<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Correct:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let name = \"Rahul\"\r\nlet email = \"rahul@xyz.com\"<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">2. Type annotation vs type inference<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Type inference (preferred in 99% of cases)\r\nlet age = 27                \/\/ Swift knows it's Int\r\nlet height = 1.74           \/\/ Double\r\nlet isActive = true         \/\/ Bool\r\nlet title = \"Swift Syntax\"  \/\/ String\r\n\r\n\/\/ Explicit type annotation (use when it improves clarity)\r\nlet maxFileSize: Int64 = 5_000_000_000\r\nlet pi: Double = 3.141592653589793\r\nlet statusCode: UInt16 = 200\r\nlet userID: UUID = UUID()<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>When to write types explicitly:<\/strong><\/p>\n<ul dir=\"auto\">\n<li>When the inferred type is surprising<\/li>\n<li>Working with very large \/ small numbers (Int64, UInt32, etc.)<\/li>\n<li>Interacting with C \/ Objective-C APIs<\/li>\n<li>Making function signatures clearer<\/li>\n<\/ul>\n<h3 dir=\"auto\">3. String syntax \u2013 very flexible<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Normal string\r\nlet name = \"Ananya\"\r\n\r\n\/\/ String interpolation (very common)\r\nlet greeting = \"Hello, \\(name)!\"\r\n\r\n\/\/ Multi-line string (very clean)\r\nlet license = \"\"\"\r\nMIT License\r\n\r\nCopyright (c) 2025 Your Name\r\n\r\nPermission is hereby granted, free of charge...\r\n\"\"\"\r\n\r\n\/\/ Raw string (ignores escapes \u2013 useful for regex, paths, JSON)\r\nlet regexPattern = #\"\\d{3}-\\d{3}-\\d{4}\"#\r\nlet windowsPath = #\"C:\\Users\\Public\\Documents\"#<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Common operations:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let text = \"Swift is expressive\"\r\n\r\ntext.count                   \/\/ Int\r\ntext.isEmpty                 \/\/ Bool\r\ntext.uppercased()\r\ntext.lowercased()\r\ntext.hasPrefix(\"Swift\")\r\ntext.hasSuffix(\"ive\")\r\ntext.contains(\"press\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. Numbers \u2013 literals &amp; separators<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let smallInt   = 42\r\nlet bigInt     = 1_000_000_000      \/\/ underscore = readable separator\r\nlet veryBig    = 1_234_567_890_123\r\n\r\nlet hex        = 0xFF               \/\/ 255\r\nlet binary     = 0b101010           \/\/ 42\r\nlet octal      = 0o777              \/\/ 511\r\n\r\nlet scientific = 1.23e-4            \/\/ 0.000123\r\nlet hexFloat   = 0x1.8p3            \/\/ 12.0 (1.5 \u00d7 2\u00b3)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">5. Tuples \u2013 lightweight mini-structures<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let coordinates = (3, 4)\r\nlet person = (\"Amit\", 28, true)\r\n\r\nlet (x, y) = coordinates\r\nprint(x, y)                     \/\/ 3 4\r\n\r\nlet (name, age, isStudent) = person\r\n\r\n\/\/ Named tuples (very common in function returns)\r\nlet result = (status: 200, message: \"OK\")\r\nprint(result.status)            \/\/ 200\r\nprint(result.message)           \/\/ \"OK\"<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">6. Optionals \u2013 the syntax that defines Swift<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Declaration\r\nvar name: String? = \"Priya\"\r\nvar age: Int? = nil\r\n\r\n\/\/ Most common safe unwrapping patterns\r\n\r\n\/\/ Pattern 1 \u2013 if let (classic &amp; very common)\r\nif let n = name {\r\n    print(\"Name: \\(n)\")\r\n}\r\n\r\n\/\/ Pattern 2 \u2013 modern short syntax (very popular 2023+)\r\nif let name {\r\n    print(\"Hello \\(name)\")\r\n}\r\n\r\n\/\/ Pattern 3 \u2013 nil coalescing\r\nlet displayName = name ?? \"Guest\"\r\n\r\n\/\/ Pattern 4 \u2013 guard (early exit \u2013 very clean)\r\nguard let age else {\r\n    print(\"Age is required\")\r\n    return\r\n}\r\nprint(\"You are \\(age) years old\")\r\n\r\n\/\/ Pattern 5 \u2013 optional chaining\r\nlet length = user?.profile?.bio?.count<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Never do this<\/strong> (very dangerous):<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let forced = name!          \/\/ crashes if nil \u2013 avoid unless 100% sure<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">7. Arrays \u2013 syntax variations<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Creation\r\nvar colors = [\"red\", \"green\", \"blue\"]\r\nlet numbers: [Int] = [1, 2, 3, 4]\r\n\r\n\/\/ Empty\r\nvar names: [String] = []\r\nvar scores = [Double]()\r\n\r\n\/\/ Append \/ insert \/ remove\r\ncolors.append(\"yellow\")\r\ncolors += [\"purple\", \"orange\"]\r\ncolors.insert(\"black\", at: 0)\r\ncolors.remove(at: 2)\r\ncolors.removeLast()\r\n\r\n\/\/ Access\r\ncolors[0]           \/\/ first\r\ncolors.last         \/\/ optional last element\r\ncolors.isEmpty\r\ncolors.count<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">8. Dictionaries \u2013 key-value syntax<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var scores = [\r\n    \"Ananya\": 92,\r\n    \"Rahul\": 85,\r\n    \"Priya\": 98\r\n]\r\n\r\n\/\/ Add \/ update\r\nscores[\"Vikram\"] = 76\r\nscores[\"Rahul\"] = 88\r\n\r\n\/\/ Remove\r\nscores[\"Vikram\"] = nil\r\n\r\n\/\/ Safe access\r\nif let s = scores[\"Ananya\"] {\r\n    print(\"Score: \\(s)\")\r\n}\r\n\r\n\/\/ Default value\r\nlet points = scores[\"Unknown\"] ?? 0\r\n\r\n\/\/ Loop\r\nfor (name, score) in scores {\r\n    print(\"\\(name): \\(score)\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">9. Control flow \u2013 if, guard, switch<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ if \u2013 else if \u2013 else\r\nlet temp = 33\r\n\r\nif temp &gt;= 35 {\r\n    print(\"Extreme heat\")\r\n} else if temp &gt;= 28 {\r\n    print(\"Hot\")\r\n} else {\r\n    print(\"Comfortable\")\r\n}\r\n\r\n\/\/ guard \u2013 early exit (very clean)\r\nfunc processUser(age: Int?) {\r\n    guard let age else {\r\n        print(\"Age missing\")\r\n        return\r\n    }\r\n    \r\n    guard age &gt;= 18 else {\r\n        print(\"Too young\")\r\n        return\r\n    }\r\n    \r\n    print(\"Welcome!\")\r\n}\r\n\r\n\/\/ switch \u2013 very powerful\r\nlet direction = \"N\"\r\n\r\nswitch direction {\r\ncase \"N\", \"North\":\r\n    print(\"\u2191\")\r\ncase \"S\", \"South\":\r\n    print(\"\u2193\")\r\ncase \"E\", \"East\":\r\n    print(\"\u2192\")\r\ncase \"W\", \"West\":\r\n    print(\"\u2190\")\r\ndefault:\r\n    print(\"Unknown\")\r\n}\r\n\r\n\/\/ Range matching (very common)\r\nlet code = 404\r\n\r\nswitch code {\r\ncase 200..&lt;300: print(\"Success\")\r\ncase 400..&lt;500: print(\"Client error\")\r\ncase 500...:    print(\"Server error\")\r\ndefault:        print(\"Weird\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">10. Functions \u2013 basic syntax patterns<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Simple\r\nfunc sayHello() {\r\n    print(\"Namaste!\")\r\n}\r\n\r\n\/\/ With parameters (external + internal names)\r\nfunc greet(person name: String, age years: Int) {\r\n    print(\"Hello \\(name), you are \\(years) years old.\")\r\n}\r\n\r\ngreet(person: \"Meera\", age: 24)\r\n\r\n\/\/ Default values\r\nfunc connect(to host: String = \"api.example.com\", port: Int = 443) {\r\n    print(\"Connecting to \\(host):\\(port)\")\r\n}\r\n\r\n\/\/ Returning value\r\nfunc square(_ number: Int) -&gt; Int {\r\n    return number * number\r\n}\r\n\r\n\/\/ Multiple return values (tuple)\r\nfunc minMax(numbers: [Int]) -&gt; (min: Int, max: Int)? {\r\n    guard !numbers.isEmpty else { return nil }\r\n    return (numbers.min()!, numbers.max()!)\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Quick Summary Table \u2013 Most Important Syntax Patterns<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"sm\">What<\/th>\n<th data-col-size=\"md\">Syntax Example<\/th>\n<th data-col-size=\"lg\">Most common style in 2025\u20132026<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"sm\">Constant<\/td>\n<td data-col-size=\"md\">let count = 10<\/td>\n<td data-col-size=\"lg\">Always prefer let<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">Optional<\/td>\n<td data-col-size=\"md\">var email: String?<\/td>\n<td data-col-size=\"lg\">if let, guard let, ??<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">String interpolation<\/td>\n<td data-col-size=\"md\">&#8220;Hello \\(name)&#8221;<\/td>\n<td data-col-size=\"lg\">Almost always used<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">Array<\/td>\n<td data-col-size=\"md\">var items: [String] = []<\/td>\n<td data-col-size=\"lg\">append, for-in<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">Dictionary<\/td>\n<td data-col-size=\"md\">[String: Int]<\/td>\n<td data-col-size=\"lg\">subscript + ??<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">Switch with ranges<\/td>\n<td data-col-size=\"md\">case 200..&lt;300:<\/td>\n<td data-col-size=\"lg\">Very common for status codes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">Guard<\/td>\n<td data-col-size=\"md\">guard let x else { return }<\/td>\n<td data-col-size=\"lg\">Preferred for early exits<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">Function labels<\/td>\n<td data-col-size=\"md\">greet(person name: String)<\/td>\n<td data-col-size=\"lg\">Clear external names<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Would you like to go deeper into any of these areas next?<\/p>\n<ul dir=\"auto\">\n<li><strong>Enums<\/strong> (very powerful in Swift)<\/li>\n<li><strong>Struct vs Class<\/strong> (when to use which)<\/li>\n<li><strong>Closures<\/strong> (very important syntax)<\/li>\n<li><strong>Async\/await<\/strong> syntax patterns<\/li>\n<li><strong>Property wrappers<\/strong> (@State, @Published, @ObservedObject\u2026)<\/li>\n<li>Or start building a <strong>small complete example program<\/strong><\/li>\n<\/ul>\n<p dir=\"auto\">Tell me what feels most useful or interesting to you right now \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swift Syntax \u2014 written as if I\u2019m sitting next to you, explaining slowly, with many small examples, comparisons to other languages you might know (C\/C++\/Python), common traps, and why Swift does things a certain&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2586","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/comments?post=2586"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2586\/revisions"}],"predecessor-version":[{"id":2587,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2586\/revisions\/2587"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}