{"id":"l-sw-1-1","courseId":"swift-essentials","lang":"en","kind":"intro","summary":"Declare constants and variables, read the types Swift infers for you, and pick the right number type for money in Pocket Budget.","html":"<p>Every year I review code from new iOS developers, and the most common crash reports trace back to the same few habits: values that change when nobody expected them to, numbers that quietly lose precision, and assumptions the compiler could have caught. Swift is designed to catch exactly those mistakes, but only if you work with the type system instead of around it.</p>\n<p>Over this course you build <strong>Pocket Budget</strong>, a small iPhone app that tracks what you spend. It starts here, with a handful of values in an Xcode playground, and ends as a SwiftUI app on TestFlight. To follow along, open Xcode, choose File › New › Playground, pick the Blank template, and type the examples. A playground runs your code as you type and shows each result in the sidebar.</p>\n<h2 id=\"sec-1\" tabindex=\"-1\">let and var</h2>\n<p>Swift has two ways to name a value. <code dir=\"ltr\">let</code> declares a constant, which is assigned once. <code dir=\"ltr\">var</code> declares a variable, which can change.</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"swift\" data-index=\"0\"><div class=\"code-head\"><span class=\"code-title\">PocketBudget.playground</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> monthlyLimit </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 1500</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">var</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> spentSoFar </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">spentSoFar </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> spentSoFar </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">+</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 42</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">spentSoFar </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">+=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 18</span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">          // shorthand for the line above</span></span>\n<span class=\"line\"><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// monthlyLimit = 2000    // error: cannot assign to value: 'monthlyLimit' is a 'let' constant</span></span></code></pre></div>\n<p>Use <code dir=\"ltr\">let</code> by default. When a value is a constant, you never have to wonder who changed it. If you later need to change it, the compiler tells you, and Xcode offers a one-click fix to turn <code dir=\"ltr\">let</code> into <code dir=\"ltr\">var</code>. Going the other way, Xcode warns you when a <code dir=\"ltr\">var</code> is never mutated. Treat that warning as a request to tidy up.</p>\n<h2 id=\"sec-2\" tabindex=\"-1\">Type inference</h2>\n<p>You didn’t write a single type above, yet every value has one. Swift <strong>infers</strong> the type from the first value you assign:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"swift\" data-index=\"1\"><div class=\"code-head\"><span class=\"code-title\">Swift</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> category </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> \"Coffee\"</span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">     // String</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> cups </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 3</span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">                // Int</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> pricePerCup </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 4.5</span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">       // Double</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> isRecurring </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> false</span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">     // Bool</span></span></code></pre></div>\n<p>Option-click any name in Xcode to see its inferred type. You can also write the type yourself with an annotation, which is useful when the literal alone would pick the wrong one:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"swift\" data-index=\"2\"><div class=\"code-head\"><span class=\"code-title\">Swift</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> fee: </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">Double</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 3</span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">         // without the annotation, 3 would be an Int</span></span></code></pre></div>\n<p>Once a value has a type, it keeps it. You can’t put a <code dir=\"ltr\">String</code> into <code dir=\"ltr\">cups</code> later. This is what makes Swift code readable months later: a name means one kind of thing, forever.</p>\n<h2 id=\"sec-3\" tabindex=\"-1\">No silent conversions</h2>\n<p>Here is the line that surprises people coming from JavaScript or Python:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"swift\" data-index=\"3\"><div class=\"code-head\"><span class=\"code-title\">Swift</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> cups </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 3</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> pricePerCup </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 4.5</span></span>\n<span class=\"line\"><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// let total = cups * pricePerCup   // error: binary operator '*' cannot be applied to operands of type 'Int' and 'Double'</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> total </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> Double</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(cups) </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">*</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> pricePerCup   </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// 13.5</span></span></code></pre></div>\n<p>Swift never converts an <code dir=\"ltr\">Int</code> to a <code dir=\"ltr\">Double</code> (or back) behind your back. You write the conversion, so the reader can see it. Converting a <code dir=\"ltr\">Double</code> to an <code dir=\"ltr\">Int</code> with <code dir=\"ltr\">Int(13.5)</code> drops the fraction and gives 13, which is exactly the kind of decision you want to be visible in a code review.</p>\n<aside class=\"callout callout-note\" role=\"note\"><p class=\"callout-title\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><circle cx=\"12\" cy=\"12\" r=\"10\"/><path d=\"M12 16v-4\"/><path d=\"M12 8h.01\"/></svg><span><span class=\"callout-kind\">Note</span><span class=\"callout-sep\" aria-hidden=\"true\"> · </span>Literals are flexible, values are not</span></p>\n<p><code dir=\"ltr\">let total = 1200 + 4.5</code> compiles, because two bare literals have no type yet and Swift picks <code dir=\"ltr\">Double</code> for both. The error only appears once a value already has a fixed type.</p>\n</aside>\n<h2 id=\"sec-4\" tabindex=\"-1\">Money is not a Double</h2>\n<p><code dir=\"ltr\">Double</code> is binary floating point. It’s fast and perfect for distances or animation timing, but it can’t store most decimal fractions exactly. Add ten cents ten times and look at the result:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"swift\" data-index=\"4\"><div class=\"code-head\"><span class=\"code-title\">Swift</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">import</span><span style=\"color:#953800;--shiki-dark:#FFA657\"> Foundation</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">var</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> withDouble </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0.0</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">var</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> withDecimal: Decimal </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">for</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> _</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> in</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 1</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">...</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">10</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">    withDouble </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">+=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0.1</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">    withDecimal </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">+=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0.1</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(withDouble)    </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// 0.9999999999999999</span></span>\n<span class=\"line\"><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">print</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(withDecimal)   </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// 1</span></span></code></pre></div>\n<p>A budget app that says you spent $0.9999999999999999 loses the user’s trust immediately. <code dir=\"ltr\">Decimal</code>, from Foundation, stores base-10 digits, so cents stay exact. Pocket Budget uses <code dir=\"ltr\">Decimal</code> for every amount, and you will see it in almost every lesson from here on.</p>\n<p>One catch: a fractional literal such as <code dir=\"ltr\">let tip: Decimal = 0.07</code> is read as a <code dir=\"ltr\">Double</code> first and then converted, so it can arrive as <code dir=\"ltr\">0.07000000000000001024</code>. Short example amounts like <code dir=\"ltr\">4.5</code> or <code dir=\"ltr\">12.99</code> come through clean, but for amounts that matter, build the <code dir=\"ltr\">Decimal</code> from text (<code dir=\"ltr\">Decimal(string: &quot;0.07&quot;)</code>) or from whole cents (<code dir=\"ltr\">Decimal(7) / 100</code>).</p>\n<aside class=\"callout callout-mistake\" role=\"note\"><p class=\"callout-title\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3\"/><path d=\"M12 9v4\"/><path d=\"M12 17h.01\"/></svg><span><span class=\"callout-kind\">Common mistake</span><span class=\"callout-sep\" aria-hidden=\"true\"> · </span>Formatting money by hand</span></p>\n<p><code dir=\"ltr\">&quot;$&quot; + String(describing: amount)</code> breaks the first time a user in Germany or Japan opens your app. Use <code dir=\"ltr\">amount.formatted(.currency(code: &quot;USD&quot;))</code>, which applies the right symbol, separators and decimal places for the user’s locale.</p>\n</aside>\n<p>You now have constants, variables and the types behind them. In the next lesson you meet the type that makes Swift famous for safety: the optional, Swift’s way of saying “there might not be a value here”.</p>\n","toc":[{"id":"sec-1","text":"let and var","level":2},{"id":"sec-2","text":"Type inference","level":2},{"id":"sec-3","text":"No silent conversions","level":2},{"id":"sec-4","text":"Money is not a Double","level":2}],"takeaways":["Use <code dir=\"ltr\">let</code> by default and switch to <code dir=\"ltr\">var</code> only when the compiler tells you the value must change.","Swift infers a type from the first value you assign, and that type never changes afterwards.","Swift never converts between <code dir=\"ltr\">Int</code> and <code dir=\"ltr\">Double</code> silently; you convert explicitly, so mixed-type maths can’t surprise you.","Store money as <code dir=\"ltr\">Decimal</code>, not <code dir=\"ltr\">Double</code>, because binary floating point can’t represent most cents exactly."],"further":[{"title":"The Basics (The Swift Programming Language)","url":"https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/"},{"title":"Decimal","url":"https://developer.apple.com/documentation/foundation/decimal"}],"quiz":[{"q":"<p>You write <code dir=\"ltr\">let limit = 300</code> and later <code dir=\"ltr\">limit = 350</code>. What happens?</p>","options":[{"text":"It compiles, and <code dir=\"ltr\">limit</code> is now 350.","why":"That would be true for <code dir=\"ltr\">var</code>. A <code dir=\"ltr\">let</code> constant can be assigned exactly once."},{"text":"The compiler rejects the second line: <code dir=\"ltr\">limit</code> is a <code dir=\"ltr\">let</code> constant.","why":"Correct. Swift catches the reassignment at compile time, before the app runs."},{"text":"It compiles, but the app crashes when that line runs.","why":"Swift doesn’t defer this to runtime. Mutability is checked by the compiler."}],"answer":1},{"q":"<p>What type does Swift infer for <code dir=\"ltr\">let coffee = 4.5</code>?</p>","options":[{"text":"<code dir=\"ltr\">Float</code>","why":"Swift picks <code dir=\"ltr\">Double</code> for a decimal literal unless you ask for <code dir=\"ltr\">Float</code> explicitly."},{"text":"<code dir=\"ltr\">Decimal</code>","why":"<code dir=\"ltr\">Decimal</code> is never inferred from a literal; you have to annotate it, as in <code dir=\"ltr\">let coffee: Decimal = 4.5</code>."},{"text":"<code dir=\"ltr\">Double</code>","why":"Correct. A literal with a fractional part defaults to <code dir=\"ltr\">Double</code>."}],"answer":2},{"q":"<p><code dir=\"ltr\">let rent = 1200</code> and <code dir=\"ltr\">let fee = 2.5</code> are both declared. Why does <code dir=\"ltr\">rent + fee</code> fail to compile?</p>","options":[{"text":"<code dir=\"ltr\">rent</code> is an <code dir=\"ltr\">Int</code> and <code dir=\"ltr\">fee</code> is a <code dir=\"ltr\">Double</code>, and Swift never mixes them without an explicit conversion.","why":"Correct. Write <code dir=\"ltr\">Double(rent) + fee</code> and you have said out loud which conversion you want."},{"text":"Constants can’t be used in arithmetic; only variables can.","why":"Constants work in any expression. The problem is the two different types."},{"text":"<code dir=\"ltr\">+</code> only works on strings in Swift.","why":"<code dir=\"ltr\">+</code> works on numbers and strings, but both sides must be the same type."}],"answer":0}],"exercise":{"kind":"choice","multiple":true,"options":["reassign-let","increment-var","literal-sum","mixed-sum","string-to-int","annotated-double"],"correct":["increment-var","literal-sum","annotated-double"],"title":"Which lines compile?","prompt":"<p>Pocket Budget’s first playground has a few lines that the Swift compiler will reject. Each option below is a complete snippet on its own. Select <strong>every</strong> snippet that compiles without errors.</p>\n","hints":["Ask two questions of each snippet: is anything assigned twice with <code dir=\"ltr\">let</code>, and does any operator mix two values whose types are already fixed?","Bare literals like <code dir=\"ltr\">1200</code> and <code dir=\"ltr\">4.5</code> have no type until Swift picks one for them, so they can adapt to each other.","An annotation such as <code dir=\"ltr\">: Double</code> tells Swift how to read the literal that follows it."],"explanation":"<p>Three snippets compile. <code dir=\"ltr\">var spent</code> can change, so <code dir=\"ltr\">+=</code> is fine. <code dir=\"ltr\">1200 + 4.5</code> is two untyped literals, so Swift makes both <code dir=\"ltr\">Double</code>. <code dir=\"ltr\">let fee: Double = 3</code> reads the integer literal as a <code dir=\"ltr\">Double</code> because of the annotation.</p>\n<p>The other three fail for the reasons you will meet every week: a <code dir=\"ltr\">let</code> can’t be reassigned, an <code dir=\"ltr\">Int</code> value and a <code dir=\"ltr\">Double</code> value can’t be added without <code dir=\"ltr\">Double(rent)</code>, and a <code dir=\"ltr\">String</code> variable can never hold an <code dir=\"ltr\">Int</code>.</p>\n","checkLabels":{},"optionText":{"reassign-let":{"text":"<code dir=\"ltr\">let budget = 500; budget = 450</code>","why":"A <code dir=\"ltr\">let</code> constant is assigned exactly once. Use <code dir=\"ltr\">var</code> if the budget really changes."},"increment-var":{"text":"<code dir=\"ltr\">var spent = 0; spent += 12</code>","why":"Compiles. <code dir=\"ltr\">spent</code> is a variable of type <code dir=\"ltr\">Int</code>, and adding an <code dir=\"ltr\">Int</code> to it is allowed."},"literal-sum":{"text":"<code dir=\"ltr\">let total = 1200 + 4.5</code>","why":"Compiles. Both operands are literals, so Swift chooses <code dir=\"ltr\">Double</code> for both and <code dir=\"ltr\">total</code> is <code dir=\"ltr\">1204.5</code>."},"mixed-sum":{"text":"<code dir=\"ltr\">let rent = 1200; let coffee = 4.5; let sum = rent + coffee</code>","why":"Fails. <code dir=\"ltr\">rent</code> is already an <code dir=\"ltr\">Int</code> and <code dir=\"ltr\">coffee</code> a <code dir=\"ltr\">Double</code>; write <code dir=\"ltr\">Double(rent) + coffee</code>."},"string-to-int":{"text":"<code dir=\"ltr\">var label: String = &quot;Coffee&quot;; label = 42</code>","why":"Fails. A variable keeps its type for life; <code dir=\"ltr\">label</code> can only ever hold a <code dir=\"ltr\">String</code>."},"annotated-double":{"text":"<code dir=\"ltr\">let fee: Double = 3</code>","why":"Compiles. The annotation tells Swift to treat the integer literal <code dir=\"ltr\">3</code> as a <code dir=\"ltr\">Double</code>."}}},"words":632,"readMinutes":3}