{"id":2598,"date":"2026-02-05T07:13:35","date_gmt":"2026-02-05T07:13:35","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2598"},"modified":"2026-02-05T07:13:35","modified_gmt":"2026-02-05T07:13:35","slug":"chapter-9-swift-variables","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-9-swift-variables\/","title":{"rendered":"Chapter 9: Swift Variables"},"content":{"rendered":"<h3 dir=\"auto\">1. What is a variable in Swift? (the big picture)<\/h3>\n<p dir=\"auto\">A variable is a <strong>named storage location<\/strong> in memory that holds a value.<\/p>\n<p dir=\"auto\">In Swift you have <strong>two kinds<\/strong> of these storage locations:<\/p>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"xs\">Keyword<\/th>\n<th data-col-size=\"lg\">Meaning<\/th>\n<th data-col-size=\"xs\">Can you change the value later?<\/th>\n<th data-col-size=\"xl\">Real-life analogy<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"xs\">let<\/td>\n<td data-col-size=\"lg\">constant \u2013 immutable<\/td>\n<td data-col-size=\"xs\"><strong>No<\/strong><\/td>\n<td data-col-size=\"xl\">Your birth date, your Aadhaar number<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">var<\/td>\n<td data-col-size=\"lg\">variable \u2013 mutable<\/td>\n<td data-col-size=\"xs\"><strong>Yes<\/strong><\/td>\n<td data-col-size=\"xl\">Your bank balance, current temperature<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Golden rule that almost every good Swift developer follows (2025 style):<\/strong><\/p>\n<blockquote dir=\"auto\">\n<p dir=\"auto\">Use <strong>let<\/strong> by default Use <strong>var<\/strong> <strong>only<\/strong> when you are <strong>sure<\/strong> the value will actually change<\/p>\n<\/blockquote>\n<p dir=\"auto\">This is probably the <strong>most important habit<\/strong> to develop early.<\/p>\n<h3 dir=\"auto\">2. Basic syntax \u2013 declaring variables<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Constant (preferred in most cases)\r\nlet pi = 3.14159\r\nlet maximumUsers = 1000\r\nlet appName = \"MyTodoApp\"\r\n\r\n\/\/ Variable (only when it will change)\r\nvar score = 0\r\nvar currentTemperature = 28.6\r\nvar username = \"guest123\"<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">After declaration:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ This is allowed\r\nscore = 10\r\nscore += 50\r\ncurrentTemperature = 29.2\r\nusername = \"player_456\"\r\n\r\n\/\/ This will NOT compile \u2013 good!\r\npi = 3.14           \/\/ Error: Cannot assign to value: 'pi' is a 'let' constant<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">3. Type inference \u2013 Swift usually knows the type<\/h3>\n<p dir=\"auto\">Swift is very smart \u2014 you <strong>rarely<\/strong> need to write the type.<\/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 age = 25                    \/\/ Swift knows \u2192 Int\r\nlet height = 1.68               \/\/ \u2192 Double\r\nlet isStudent = true            \/\/ \u2192 Bool\r\nlet name = \"Aarav\"              \/\/ \u2192 String\r\nlet emoji = \"\ud83d\udd25\"                 \/\/ \u2192 String (not Character!)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">But you <strong>can<\/strong> write the type when it makes code clearer:<\/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 maxFileSize: Int64 = 5_000_000_000\r\nlet taxRate: Double = 0.18\r\nlet userID: UUID = UUID()\r\nlet status: HTTPStatus = .ok<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>When should you write the type explicitly?<\/strong><\/p>\n<ul dir=\"auto\">\n<li>Working with very specific number types (Int32, UInt64, Float, etc.)<\/li>\n<li>When the inferred type might be surprising<\/li>\n<li>In function parameters \/ return types (very common)<\/li>\n<li>When reading code is easier with explicit types<\/li>\n<\/ul>\n<h3 dir=\"auto\">4. Side-by-side comparison with other languages<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"sm\">Language<\/th>\n<th data-col-size=\"lg\">Constant style<\/th>\n<th data-col-size=\"lg\">Variable style<\/th>\n<th data-col-size=\"md\">Swift style (modern)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"sm\">C \/ C++<\/td>\n<td data-col-size=\"lg\">const int max = 100;<\/td>\n<td data-col-size=\"lg\">int count = 0;<\/td>\n<td data-col-size=\"md\">let max = 100 \/ var count = 0<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">Java<\/td>\n<td data-col-size=\"lg\">final int MAX = 100;<\/td>\n<td data-col-size=\"lg\">int count = 0;<\/td>\n<td data-col-size=\"md\">let \/ var<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">Python<\/td>\n<td data-col-size=\"lg\">(no real constant)<\/td>\n<td data-col-size=\"lg\">count = 0<\/td>\n<td data-col-size=\"md\">let \/ var<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\">Swift<\/td>\n<td data-col-size=\"lg\">let max = 100<\/td>\n<td data-col-size=\"lg\">var count = 0<\/td>\n<td data-col-size=\"md\"><strong>prefer let<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Swift encourages <strong>immutability<\/strong> much more strongly than most languages.<\/p>\n<h3 dir=\"auto\">5. Very common beginner mistakes (and correct way)<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">Wrong \/ bad habit<\/th>\n<th data-col-size=\"md\">Why it&#8217;s bad<\/th>\n<th data-col-size=\"lg\">Better way<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">var name = &#8220;Rahul&#8221; when name never changes<\/td>\n<td data-col-size=\"md\">Creates false expectation that it will change<\/td>\n<td data-col-size=\"lg\">let name = &#8220;Rahul&#8221;<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">var age: Int = 25 everywhere<\/td>\n<td data-col-size=\"md\">Unnecessary noise<\/td>\n<td data-col-size=\"lg\">let age = 25 (type inference)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">var counter = 0 and then never changing it<\/td>\n<td data-col-size=\"md\">Misleading<\/td>\n<td data-col-size=\"lg\">let initialCount = 0<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Force-unwrapping optionals inside variables<\/td>\n<td data-col-size=\"md\">var name = optionalName! \u2192 crashes easily<\/td>\n<td data-col-size=\"lg\">var name = optionalName ?? &#8220;Unknown&#8221;<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Declaring everything as var &#8220;just in case&#8221;<\/td>\n<td data-col-size=\"md\">Loses safety &amp; clarity<\/td>\n<td data-col-size=\"lg\">Start with let, change to var only if needed<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">6. Practical real-world examples<\/h3>\n<h4 dir=\"auto\">Example 1 \u2013 Game score<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let maxLives = 3                \/\/ never changes\r\nvar currentLives = maxLives     \/\/ changes during game\r\nvar score = 0                   \/\/ changes a lot\r\n\r\n\/\/ later in code...\r\nscore += 100\r\ncurrentLives -= 1\r\n\r\nif currentLives == 0 {\r\n    print(\"Game Over! Final score: \\(score)\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 2 \u2013 User profile<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let userID = UUID()                 \/\/ never changes\r\nlet registeredOn = Date()           \/\/ fixed\r\n\r\nvar displayName = \"guest_123\"\r\nvar profilePictureURL: URL?         \/\/ can be nil or change\r\nvar isPremium = false\r\n\r\n\/\/ later...\r\ndisplayName = \"Aarav_007\"\r\nisPremium = true<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 3 \u2013 Temperature converter (shows let vs var clearly)<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>func convertToCelsius(fahrenheit: Double) -&gt; Double {\r\n    let adjusted = fahrenheit - 32              \/\/ never changes inside function\r\n    let multiplier = 5.0 \/ 9.0                  \/\/ constant\r\n    return adjusted * multiplier\r\n}\r\n\r\nvar currentTempF = 98.6\r\nvar currentTempC = convertToCelsius(fahrenheit: currentTempF)\r\n\r\n\/\/ later...\r\ncurrentTempF = 104.0\r\ncurrentTempC = convertToCelsius(fahrenheit: currentTempF)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">7. Naming conventions \u2013 what real Swift developers do<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"md\">Kind of name<\/th>\n<th data-col-size=\"md\">Convention<\/th>\n<th data-col-size=\"lg\">Example<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\">Constants (let)<\/td>\n<td data-col-size=\"md\">camelCase or UPPER_CASE for global<\/td>\n<td data-col-size=\"lg\">maxRetryCount, API_KEY<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Variables (var)<\/td>\n<td data-col-size=\"md\">camelCase<\/td>\n<td data-col-size=\"lg\">currentUser, totalPrice<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Private properties<\/td>\n<td data-col-size=\"md\">Often private var<\/td>\n<td data-col-size=\"lg\">private var internalCache<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Global constants<\/td>\n<td data-col-size=\"md\">UPPER_CASE_WITH_UNDERSCORES (rare)<\/td>\n<td data-col-size=\"lg\">MAX_UPLOAD_SIZE_BYTES<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Very common pattern in modern SwiftUI \/ UIKit:<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>private let cellReuseIdentifier = \"UserCell\"\r\nprivate var users: [User] = []<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">8. Quick cheat sheet \u2013 when to choose let vs var<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">Situation<\/th>\n<th data-col-size=\"xs\">Use let or var?<\/th>\n<th data-col-size=\"md\">Why?<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">Value will never change<\/td>\n<td data-col-size=\"xs\">let<\/td>\n<td data-col-size=\"md\">Safety + clarity<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Value changes in one small scope<\/td>\n<td data-col-size=\"xs\">var<\/td>\n<td data-col-size=\"md\">Normal<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Loop variable (for-in)<\/td>\n<td data-col-size=\"xs\">let (default)<\/td>\n<td data-col-size=\"md\">You rarely change it<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Property that gets set once (like in init)<\/td>\n<td data-col-size=\"xs\">let<\/td>\n<td data-col-size=\"md\">Expresses intent<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Counter, score, position, status<\/td>\n<td data-col-size=\"xs\">var<\/td>\n<td data-col-size=\"md\">Obviously changes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Model \/ struct property that should not change<\/td>\n<td data-col-size=\"xs\">let<\/td>\n<td data-col-size=\"md\">Makes data safer<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">9. Mini practice \u2013 try to fix these<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Try to improve these declarations\r\n\r\nvar firstName = \"Sneha\"             \/\/ never changes\r\nvar age = 24                        \/\/ never changes later\r\nvar totalPrice = 1499.99            \/\/ calculated once\r\nvar counter = 0                     \/\/ used in for loop but never reassigned<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Expected better versions:<\/strong><\/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 firstName = \"Sneha\"\r\nlet age = 24\r\nlet totalPrice = 1499.99\r\n\/\/ counter \u2192 usually let inside for-in<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Would you like to continue with:<\/p>\n<ul dir=\"auto\">\n<li><strong>Optionals<\/strong> (String?, Int?, etc.) \u2013 very important next step<\/li>\n<li><strong>Computed properties<\/strong> (variables that calculate their value)<\/li>\n<li><strong>Variables inside functions vs properties in structs\/classes<\/strong><\/li>\n<li><strong>Type aliases<\/strong> (typealias UserID = Int64)<\/li>\n<li>Or move to another topic (constants in detail, printing variables, etc.)<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me what you want next \u2014 I&#8217;ll continue in the same detailed, teacher-like way \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What is a variable in Swift? (the big picture) A variable is a named storage location in memory that holds a value. In Swift you have two kinds of these storage locations: Keyword&#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-2598","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2598","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=2598"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2598\/revisions"}],"predecessor-version":[{"id":2599,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2598\/revisions\/2599"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}