Bird
Raised Fist0
Swiftprogramming~15 mins

Methods in structs in Swift - Mini Project: Build & Apply

Choose your learning style10 modes available
Methods in structs
📖 Scenario: Imagine you are creating a simple app to keep track of books in a small library. Each book has a title and an author. You want to add a feature that shows a short description of the book using a method inside a struct.
🎯 Goal: You will create a Book struct with properties for the title and author. Then, you will add a method inside the struct that returns a description string combining the title and author. Finally, you will create a book instance and print its description.
📋 What You'll Learn
Create a struct called Book with two String properties: title and author.
Add a method called description() inside the Book struct that returns a String combining the title and author in the format: " by <author>".</div><div class="project-req-item">Create an instance of <code>Book</code> with the title "The Swift Journey" and author "Alex Developer".</div><div class="project-req-item">Print the result of calling the <code>description()</code> method on the book instance.</div></div></div></div><div class="project-faq-item"><div class="project-faq-header"><span class="project-faq-title">💡 Why This Matters</span><svg class="project-faq-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="6 9 12 15 18 9"></polyline></svg></div><div class="project-faq-body"><div class="project-context-grid"><div class="project-context-item"><div class="project-context-label">🌍 Real World</div><div class="project-context-value">Structs with methods are used to model real-world objects and their behaviors in apps, like books in a library app.</div></div><div class="project-context-item job"><div class="project-context-label">💼 Career</div><div class="project-context-value">Understanding how to add methods to structs is important for writing clean, organized Swift code in iOS app development.</div></div></div></div></div></div><div class="project-progress"><div class="project-progress-label"><span>Progress</span><span>0<!-- --> / <!-- -->4<!-- --> steps</span></div><div class="project-progress-bar"><div class="project-progress-fill" style="width:0%"></div></div></div><div class="project-step-nav"><button class="project-step-btn active ">1</button><button class="project-step-btn ">2</button><button class="project-step-btn ">3</button><button class="project-step-btn ">4</button></div></div></article><article class="content-card project-step-container active"><div class="code-articles-card-body "><div class="project-step-header d-flex align-items-center"><div class="project-step-badge ">1</div><div class="project-step-title-wrap "><div class="project-step-title">Create the Book struct with properties</div></div></div><div class="project-instruction">Create a struct called <code>Book</code> with two <code>String</code> properties named <code>title</code> and <code>author</code>.</div><div class="project-code-editor"><div class="code-editor-header"><span class="code-editor-label"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M16 18l6-6-6-6"></path><path d="M8 6l-6 6 6 6"></path></svg>Swift</span><div class="code-editor-actions"><button class="code-reset-btn">↺ Reset</button><button class="code-solution-btn"><span class="new-material-symbols icon-hw-22 me-1"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -960 960 960" fill="#d97706"><path d="M602.83-377.25q50.5-50.57 50.5-122.83 0-72.25-50.58-122.75-50.57-50.5-122.83-50.5-72.25 0-122.75 50.58-50.5 50.57-50.5 122.83 0 72.25 50.58 122.75 50.57 50.5 122.83 50.5 72.25 0 122.75-50.58ZM401.5-421.66q-32.17-32.32-32.17-78.5 0-46.17 32.33-78.34 32.32-32.17 78.5-32.17 46.17 0 78.34 32.33 32.17 32.32 32.17 78.5 0 46.17-32.33 78.34-32.32 32.17-78.5 32.17-46.17 0-78.34-32.33ZM234.83-274.67q-110.5-74.66-176.5-192.66-4.33-7.75-6.5-15.93-2.16-8.18-2.16-16.8 0-8.61 2.16-16.75 2.17-8.14 6.5-15.86 66-118 176.5-192.66Q345.33-800 480-800t245.17 74.67q110.5 74.66 176.5 192.66 4.33 7.75 6.5 15.93 2.16 8.18 2.16 16.8 0 8.61-2.16 16.75-2.17 8.14-6.5 15.86-66 118-176.5 192.66Q614.67-200 480-200t-245.17-74.67Z"></path></svg></span>Show Code</button></div></div><textarea class="project-code-textarea" spellCheck="false"># Create the Book struct with title and author properties # Your code here</textarea></div><button class="run-code-btn project-run-btn"><svg viewBox="0 0 24 24" fill="currentColor"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>Run Code</button><div class="project-hint "><div class="project-hint-title mb-1">Hint</div><div class="hint-content"><p>Use <code>struct Book { }</code> and inside it declare <code>var title: String</code> and <code>var author: String</code>.</p></div></div></div></article><article class="content-card project-step-container "><div class="code-articles-card-body "><div class="project-step-header d-flex align-items-center"><div class="project-step-badge ">2</div><div class="project-step-title-wrap "><div class="project-step-title">Add a description() method to the Book struct</div></div></div><div class="project-instruction">Inside the <code>Book</code> struct, add a method called <code>description()</code> that returns a <code>String</code> combining the <code>title</code> and <code>author</code> in the format: "<title> by <author>".</div><div class="project-code-editor"><div class="code-editor-header"><span class="code-editor-label"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M16 18l6-6-6-6"></path><path d="M8 6l-6 6 6 6"></path></svg>Swift</span><div class="code-editor-actions"><button class="code-reset-btn">↺ Reset</button><button class="code-solution-btn"><span class="new-material-symbols icon-hw-22 me-1"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -960 960 960" fill="#d97706"><path d="M602.83-377.25q50.5-50.57 50.5-122.83 0-72.25-50.58-122.75-50.57-50.5-122.83-50.5-72.25 0-122.75 50.58-50.5 50.57-50.5 122.83 0 72.25 50.58 122.75 50.57 50.5 122.83 50.5 72.25 0 122.75-50.58ZM401.5-421.66q-32.17-32.32-32.17-78.5 0-46.17 32.33-78.34 32.32-32.17 78.5-32.17 46.17 0 78.34 32.33 32.17 32.32 32.17 78.5 0 46.17-32.33 78.34-32.32 32.17-78.5 32.17-46.17 0-78.34-32.33ZM234.83-274.67q-110.5-74.66-176.5-192.66-4.33-7.75-6.5-15.93-2.16-8.18-2.16-16.8 0-8.61 2.16-16.75 2.17-8.14 6.5-15.86 66-118 176.5-192.66Q345.33-800 480-800t245.17 74.67q110.5 74.66 176.5 192.66 4.33 7.75 6.5 15.93 2.16 8.18 2.16 16.8 0 8.61-2.16 16.75-2.17 8.14-6.5 15.86-66 118-176.5 192.66Q614.67-200 480-200t-245.17-74.67Z"></path></svg></span>Show Code</button></div></div><textarea class="project-code-textarea" spellCheck="false">struct Book { var title: String var author: String // Add the description() method below // Your code here }</textarea></div><button class="run-code-btn project-run-btn"><svg viewBox="0 0 24 24" fill="currentColor"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>Run Code</button><div class="project-hint "><div class="project-hint-title mb-1">Hint</div><div class="hint-content"><p>Define <code>func description() -> String</code> inside the struct and return the string using <code>"\(title) by \(author)"</code>.</p></div></div></div></article><article class="content-card project-step-container "><div class="code-articles-card-body "><div class="project-step-header d-flex align-items-center"><div class="project-step-badge ">3</div><div class="project-step-title-wrap "><div class="project-step-title">Create a Book instance</div></div></div><div class="project-instruction">Create a constant called <code>myBook</code> of type <code>Book</code> with the title set to <code>"The Swift Journey"</code> and the author set to <code>"Alex Developer"</code>.</div><div class="project-code-editor"><div class="code-editor-header"><span class="code-editor-label"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M16 18l6-6-6-6"></path><path d="M8 6l-6 6 6 6"></path></svg>Swift</span><div class="code-editor-actions"><button class="code-reset-btn">↺ Reset</button><button class="code-solution-btn"><span class="new-material-symbols icon-hw-22 me-1"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -960 960 960" fill="#d97706"><path d="M602.83-377.25q50.5-50.57 50.5-122.83 0-72.25-50.58-122.75-50.57-50.5-122.83-50.5-72.25 0-122.75 50.58-50.5 50.57-50.5 122.83 0 72.25 50.58 122.75 50.57 50.5 122.83 50.5 72.25 0 122.75-50.58ZM401.5-421.66q-32.17-32.32-32.17-78.5 0-46.17 32.33-78.34 32.32-32.17 78.5-32.17 46.17 0 78.34 32.33 32.17 32.32 32.17 78.5 0 46.17-32.33 78.34-32.32 32.17-78.5 32.17-46.17 0-78.34-32.33ZM234.83-274.67q-110.5-74.66-176.5-192.66-4.33-7.75-6.5-15.93-2.16-8.18-2.16-16.8 0-8.61 2.16-16.75 2.17-8.14 6.5-15.86 66-118 176.5-192.66Q345.33-800 480-800t245.17 74.67q110.5 74.66 176.5 192.66 4.33 7.75 6.5 15.93 2.16 8.18 2.16 16.8 0 8.61-2.16 16.75-2.17 8.14-6.5 15.86-66 118-176.5 192.66Q614.67-200 480-200t-245.17-74.67Z"></path></svg></span>Show Code</button></div></div><textarea class="project-code-textarea" spellCheck="false">struct Book { var title: String var author: String func description() -> String { return "\(title) by \(author)" } } // Create the myBook instance below // Your code here</textarea></div><button class="run-code-btn project-run-btn"><svg viewBox="0 0 24 24" fill="currentColor"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>Run Code</button><div class="project-hint "><div class="project-hint-title mb-1">Hint</div><div class="hint-content"><p>Use <code>let myBook = Book(title: "The Swift Journey", author: "Alex Developer")</code> to create the instance.</p></div></div></div></article><article class="content-card project-step-container "><div class="code-articles-card-body "><div class="project-step-header d-flex align-items-center"><div class="project-step-badge ">4</div><div class="project-step-title-wrap "><div class="project-step-title">Print the book description</div></div></div><div class="project-instruction">Write a <code>print</code> statement that calls the <code>description()</code> method on <code>myBook</code> and prints the result.</div><div class="project-code-editor"><div class="code-editor-header"><span class="code-editor-label"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M16 18l6-6-6-6"></path><path d="M8 6l-6 6 6 6"></path></svg>Swift</span><div class="code-editor-actions"><button class="code-reset-btn">↺ Reset</button><button class="code-solution-btn"><span class="new-material-symbols icon-hw-22 me-1"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -960 960 960" fill="#d97706"><path d="M602.83-377.25q50.5-50.57 50.5-122.83 0-72.25-50.58-122.75-50.57-50.5-122.83-50.5-72.25 0-122.75 50.58-50.5 50.57-50.5 122.83 0 72.25 50.58 122.75 50.57 50.5 122.83 50.5 72.25 0 122.75-50.58ZM401.5-421.66q-32.17-32.32-32.17-78.5 0-46.17 32.33-78.34 32.32-32.17 78.5-32.17 46.17 0 78.34 32.33 32.17 32.32 32.17 78.5 0 46.17-32.33 78.34-32.32 32.17-78.5 32.17-46.17 0-78.34-32.33ZM234.83-274.67q-110.5-74.66-176.5-192.66-4.33-7.75-6.5-15.93-2.16-8.18-2.16-16.8 0-8.61 2.16-16.75 2.17-8.14 6.5-15.86 66-118 176.5-192.66Q345.33-800 480-800t245.17 74.67q110.5 74.66 176.5 192.66 4.33 7.75 6.5 15.93 2.16 8.18 2.16 16.8 0 8.61-2.16 16.75-2.17 8.14-6.5 15.86-66 118-176.5 192.66Q614.67-200 480-200t-245.17-74.67Z"></path></svg></span>Show Code</button></div></div><textarea class="project-code-textarea" spellCheck="false">struct Book { var title: String var author: String func description() -> String { return "\(title) by \(author)" } } let myBook = Book(title: "The Swift Journey", author: "Alex Developer") // Print the description of myBook below # Your code here</textarea></div><button class="run-code-btn project-run-btn"><svg viewBox="0 0 24 24" fill="currentColor"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>Run Code</button><div class="project-hint "><div class="project-hint-title mb-1">Hint</div><div class="hint-content"><p>Use <code>print(myBook.description())</code> to show the description.</p></div></div></div></article></div><div><article class="content-card" id="practice"><div class="code-articles-card-header justify-content-between"><div class="d-flex align-items-center" style="gap:12px"><span class="card-icon nofill intro"><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="blue-icon-color site-fs-20" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M12 12m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0"></path><path d="M12 7a5 5 0 1 0 5 5"></path><path d="M13 3.055a9 9 0 1 0 7.941 7.945"></path><path d="M15 6v3h3l3 -3h-3v-3z"></path><path d="M15 9l-3 3"></path></svg></span><h2 class="codefly-card-title mb-0 pb-0">Practice</h2></div><span class="site-fs-15">(<!-- -->1<!-- -->/<!-- -->5<!-- -->)</span></div><div class="code-articles-card-body" data-theme="light"><div class=""><div class="mb-4"><div class="practice-content"><div class="text-pre-wrap dbd-txt-theme-intense pb-3">1<!-- -->. <p>What is the main purpose of a method inside a Swift struct?</p></div></div><div class="question-pills-container " style="padding-top:unset"><span class="level-badge">easy</span></div></div><div class=""><div class="practice-options"><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>A<!-- -->. <!-- -->To define actions the struct can perform</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>B<!-- -->. <!-- -->To store data values</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>C<!-- -->. <!-- -->To create a new struct type</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>D<!-- -->. <!-- -->To import external libraries</span></div></div><div class="py-3"><button class="site-btn" style="width:200px" disabled="">Verify</button></div></div><div class="py-2 d-none"><div class="practice-helper-btns-wrapper"></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-solution-box"><h4 class="helper-solution-box-title">Solution</h4><div class="practice-helper-body"><ol><li><h4>Step 1: Understand what methods do in structs</h4>Methods are functions inside structs that define behaviors or actions the struct can perform.</li><li><h4>Step 2: Differentiate methods from data storage</h4>Properties store data, while methods perform actions using that data.</li><li><h4>Final Answer:</h4><strong>To define actions the struct can perform</strong> -> Option A</li><li><h4>Quick Check:</h4>Methods = actions in structs [OK]</li></ol></div></div></div></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-hint-box"><div class="practice-helper-body"><strong class="helper-hint-box-title">Hint: </strong>Methods = actions inside structs, properties = data [OK]</div></div></div></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-mistakes-box"><div class="practice-helper-body"><strong class="helper-mistakes-box-title">Common Mistakes: </strong><ul class="ph-mistakes-list"><li class="ph-mistake-item mb-0">Confusing methods with properties</li><li class="ph-mistake-item mb-0">Thinking methods create new types</li><li class="ph-mistake-item mb-0">Mixing methods with import statements</li></ul></div></div></div></div></div></div><div class="d-none"><div class="mb-4"><div class="practice-content"><div class="text-pre-wrap dbd-txt-theme-intense pb-3">2<!-- -->. <p>Which keyword must be used before a method in a Swift struct if it changes the struct's properties?</p></div></div><div class="question-pills-container " style="padding-top:unset"><span class="level-badge">easy</span></div></div><div class=""><div class="practice-options"><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>A<!-- -->. <!-- -->final</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>B<!-- -->. <!-- -->override</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>C<!-- -->. <!-- -->static</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>D<!-- -->. <!-- -->mutating</span></div></div><div class="py-3"><button class="site-btn" style="width:200px" disabled="">Verify</button></div></div><div class="py-2 d-none"><div class="practice-helper-btns-wrapper"></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-solution-box"><h4 class="helper-solution-box-title">Solution</h4><div class="practice-helper-body"><ol><li><h4>Step 1: Recall Swift's rule for modifying struct properties</h4>Struct methods that change properties must be marked with <code>mutating</code> to allow modification.</li><li><h4>Step 2: Identify the correct keyword</h4>Among the options, only <code>mutating</code> enables property changes inside struct methods.</li><li><h4>Final Answer:</h4><strong>mutating</strong> -> Option D</li><li><h4>Quick Check:</h4>Change properties = mutating [OK]</li></ol></div></div></div></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-hint-box"><div class="practice-helper-body"><strong class="helper-hint-box-title">Hint: </strong>Use mutating for methods that change struct properties [OK]</div></div></div></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-mistakes-box"><div class="practice-helper-body"><strong class="helper-mistakes-box-title">Common Mistakes: </strong><ul class="ph-mistakes-list"><li class="ph-mistake-item mb-0">Using override in structs (only for classes)</li><li class="ph-mistake-item mb-0">Confusing static with mutating</li><li class="ph-mistake-item mb-0">Omitting mutating keyword causes errors</li></ul></div></div></div></div></div></div><div class="d-none"><div class="mb-4"><div class="practice-content"><div class="text-pre-wrap dbd-txt-theme-intense pb-3">3<!-- -->. <p>What will be the output of this Swift code?</p><pre>struct Counter { var count = 0 mutating func increment() { count += 1 } } var myCounter = Counter() myCounter.increment() print(myCounter.count)</pre></div></div><div class="question-pills-container " style="padding-top:unset"><span class="level-badge">medium</span></div></div><div class=""><div class="practice-options"><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>A<!-- -->. <!-- -->0</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>B<!-- -->. <!-- -->1</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>C<!-- -->. <!-- -->Error: Cannot modify property</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>D<!-- -->. <!-- -->nil</span></div></div><div class="py-3"><button class="site-btn" style="width:200px" disabled="">Verify</button></div></div><div class="py-2 d-none"><div class="practice-helper-btns-wrapper"></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-solution-box"><h4 class="helper-solution-box-title">Solution</h4><div class="practice-helper-body"><ol><li><h4>Step 1: Understand the mutating method increment()</h4>The method increases count by 1 and is marked mutating, so it can change the property.</li><li><h4>Step 2: Trace the code execution</h4>myCounter starts with count = 0, increment() adds 1, so count becomes 1, then printed.</li><li><h4>Final Answer:</h4><strong>1</strong> -> Option B</li><li><h4>Quick Check:</h4>mutating method changes count to 1 [OK]</li></ol></div></div></div></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-hint-box"><div class="practice-helper-body"><strong class="helper-hint-box-title">Hint: </strong>mutating methods can change properties, so count increments [OK]</div></div></div></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-mistakes-box"><div class="practice-helper-body"><strong class="helper-mistakes-box-title">Common Mistakes: </strong><ul class="ph-mistakes-list"><li class="ph-mistake-item mb-0">Forgetting mutating causes error</li><li class="ph-mistake-item mb-0">Expecting count to stay 0</li><li class="ph-mistake-item mb-0">Thinking print outputs nil</li></ul></div></div></div></div></div></div><div class="d-none"><div class="mb-4"><div class="practice-content"><div class="text-pre-wrap dbd-txt-theme-intense pb-3">4<!-- -->. <p>Identify the error in this Swift struct method and how to fix it:</p><pre>struct Score { var points = 0 func addPoint() { points += 1 } }</pre></div></div><div class="question-pills-container " style="padding-top:unset"><span class="level-badge">medium</span></div></div><div class=""><div class="practice-options"><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>A<!-- -->. <!-- -->Method should return <code>points</code> value</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>B<!-- -->. <!-- -->Property <code>points</code> must be <code>let</code> instead of <code>var</code></span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>C<!-- -->. <!-- -->Method must be marked <code>mutating</code> to change <code>points</code></span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>D<!-- -->. <!-- -->No error, code runs fine</span></div></div><div class="py-3"><button class="site-btn" style="width:200px" disabled="">Verify</button></div></div><div class="py-2 d-none"><div class="practice-helper-btns-wrapper"></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-solution-box"><h4 class="helper-solution-box-title">Solution</h4><div class="practice-helper-body"><ol><li><h4>Step 1: Check if method modifies a property</h4>addPoint() tries to increase points by 1, which modifies the struct's property.</li><li><h4>Step 2: Recall Swift rule for modifying properties in structs</h4>Methods that modify properties must be marked <code>mutating</code>, otherwise compiler error occurs.</li><li><h4>Final Answer:</h4><strong>Method must be marked <code>mutating</code> to change <code>points</code></strong> -> Option C</li><li><h4>Quick Check:</h4>Modify property in struct method = mutating needed [OK]</li></ol></div></div></div></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-hint-box"><div class="practice-helper-body"><strong class="helper-hint-box-title">Hint: </strong>Add mutating keyword to methods that change struct properties [OK]</div></div></div></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-mistakes-box"><div class="practice-helper-body"><strong class="helper-mistakes-box-title">Common Mistakes: </strong><ul class="ph-mistakes-list"><li class="ph-mistake-item mb-0">Omitting mutating keyword</li><li class="ph-mistake-item mb-0">Changing var to let (makes property immutable)</li><li class="ph-mistake-item mb-0">Thinking method must return value</li></ul></div></div></div></div></div></div><div class="d-none"><div class="mb-4"><div class="practice-content"><div class="text-pre-wrap dbd-txt-theme-intense pb-3">5<!-- -->. <p>Given this struct, which method correctly resets the <code>level</code> property to 1?</p><pre>struct Game { var level = 5 // Which method resets level to 1? }</pre></div></div><div class="question-pills-container " style="padding-top:unset"><span class="level-badge">hard</span></div></div><div class=""><div class="practice-options"><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>A<!-- -->. <!-- -->mutating func reset() { level = 1 }</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>B<!-- -->. <!-- -->mutating func reset() -> Int { return 1 }</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>C<!-- -->. <!-- -->static func reset() { level = 1 }</span></div><div class="practice-option-label" style="cursor:pointer"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" class="txt-dark-grey-color site-fs-18 me-2 me-md-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"></path></svg><span>D<!-- -->. <!-- -->func reset() { level = 1 }</span></div></div><div class="py-3"><button class="site-btn" style="width:200px" disabled="">Verify</button></div></div><div class="py-2 d-none"><div class="practice-helper-btns-wrapper"></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-solution-box"><h4 class="helper-solution-box-title">Solution</h4><div class="practice-helper-body"><ol><li><h4>Step 1: Identify method that changes a property</h4>Resetting level means changing the property, so method must be mutating.</li><li><h4>Step 2: Check each option</h4>A lacks mutating, so cannot modify property. B is mutating but only returns 1 without changing level. C is static and cannot access instance property. D is mutating and correctly sets level to 1.</li><li><h4>Final Answer:</h4><strong>mutating func reset() { level = 1 }</strong> -> Option A</li><li><h4>Quick Check:</h4>Change property in struct method = mutating func [OK]</li></ol></div></div></div></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-hint-box"><div class="practice-helper-body"><strong class="helper-hint-box-title">Hint: </strong>Use mutating func to change struct properties inside methods [OK]</div></div></div></div><div class="practice-expanation-div "><div class="practice-expanation-div-body"><div class="practice-helper-content helper-mistakes-box"><div class="practice-helper-body"><strong class="helper-mistakes-box-title">Common Mistakes: </strong><ul class="ph-mistakes-list"><li class="ph-mistake-item mb-0">Forgetting mutating keyword</li><li class="ph-mistake-item mb-0">Using static func to access instance properties</li><li class="ph-mistake-item mb-0">Returning value instead of changing property</li></ul></div></div></div></div></div></div><div class="d-flex align-items-center justify-content-between mt-4"><button class="secondary-site-btn" disabled=""><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" aria-hidden="true" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7"></path></svg> Previous</button><div class="practice-question-toggles-wrapper"><button class="practice-question-toggle-btn active ">1</button><button class="practice-question-toggle-btn ">2</button><button class="practice-question-toggle-btn ">3</button><button class="practice-question-toggle-btn ">4</button><button class="practice-question-toggle-btn ">5</button></div><button class="secondary-site-btn">Next <svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" aria-hidden="true" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" d="M9 5l7 7-7 7"></path></svg></button></div></div></article></div><div id="mock" class="mtc-wrapper"><div class="mtc-card-container"><div class="mtc-card pulse-effect"><span class="mtc-recommended-badge">Recommended</span><div class="mtc-icon-wrap"><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" color="#fff" style="color:#fff" height="24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M12 12m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0"></path><path d="M12 7a5 5 0 1 0 5 5"></path><path d="M13 3.055a9 9 0 1 0 7.941 7.945"></path><path d="M15 6v3h3l3 -3h-3v-3z"></path><path d="M15 9l-3 3"></path></svg></div><h4 class="mtc-heading">Ready to prove it?</h4><p class="mtc-sub">Pass the test and this pattern moves <span class="mtc-state-pill">unknown</span> <svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 512 512" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="48" d="m268 112 144 144-144 144m124-144H100"></path></svg> <span class="mtc-state-pill good">strong</span> <span class="d-inline-block">while earning <span class="weight-800">+1 Grip</span></span> toward your certificate.</p><div class="mtc-pills"><span class="mtc-pill"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="8" y1="6" x2="21" y2="6"></line><line x1="8" y1="12" x2="21" y2="12"></line><line x1="8" y1="18" x2="21" y2="18"></line><line x1="3" y1="6" x2="3.01" y2="6"></line><line x1="3" y1="12" x2="3.01" y2="12"></line><line x1="3" y1="18" x2="3.01" y2="18"></line></svg>10 questions</span><span class="mtc-pill"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 512 512" height="13" width="13" xmlns="http://www.w3.org/2000/svg"><path d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8zm0 448c-110.5 0-200-89.5-200-200S145.5 56 256 56s200 89.5 200 200-89.5 200-200 200zm61.8-104.4l-84.9-61.7c-3.1-2.3-4.9-5.9-4.9-9.7V116c0-6.6 5.4-12 12-12h32c6.6 0 12 5.4 12 12v141.7l66.8 48.6c5.4 3.9 6.5 11.4 2.6 16.8L334.6 349c-3.9 5.3-11.4 6.5-16.8 2.6z"></path></svg>~5 min</span><span class="mtc-pill"><svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="22 12 18 12 15 21 9 3 6 12 2 12"></polyline></svg>easy <svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 512 512" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="48" d="m268 112 144 144-144 144m124-144H100"></path></svg> hard</span></div><div class="mtc-cta-wrap"><button type="button" class="mtc-cta-btn">Take the Test <svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 512 512" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="48" d="m268 112 144 144-144 144m124-144H100"></path></svg></button></div></div></div></div></div></main><div class="rpi-float-wrap"><button type="button" class="rpi-float-btn" aria-label="Report an issue"><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M4 15s1-1 4-1 5 2 8 2 4-1 4-1V3s-1 1-4 1-5-2-8-2-4 1-4 1z"></path><line x1="4" y1="22" x2="4" y2="15"></line></svg><span class="rpi-float-label">Report Issue</span></button></div></div><div style="display:none"><div id="ad-management-config-settings" data-blocklist-all="1"></div></div> <script src="/_next/static/chunks/webpack-28170116499ad51b.js" async=""></script><script>(self.__next_f=self.__next_f||[]).push([0]);self.__next_f.push([2,null])</script><script>self.__next_f.push([1,"1:HL[\"/_next/static/css/d071dca61fbb01bf.css\",\"style\"]\n2:HL[\"/_next/static/css/907864b6faf28c32.css\",\"style\"]\n3:HL[\"/_next/static/css/ada9a246b4e9c7f9.css\",\"style\"]\n4:HL[\"/_next/static/css/725c7861d1898ba8.css\",\"style\"]\n5:HL[\"/_next/static/css/a97525600f39e233.css\",\"style\"]\n6:HL[\"/_next/static/css/989afc350a4b3a28.css\",\"style\"]\n7:HL[\"/_next/static/css/ccfdf35169bd162c.css\",\"style\"]\n8:HL[\"/_next/static/css/417e7aee3e688b57.css\",\"style\"]\n"])</script><script>self.__next_f.push([1,"9:I[95751,[],\"\"]\nc:I[39275,[],\"\"]\n12:I[61343,[],\"\"]\n13:I[84080,[\"8726\",\"static/chunks/8726-fd60949e252ac8d1.js\",\"3185\",\"static/chunks/app/layout-e2ef1005801dc1ed.js\"],\"\"]\n14:I[88726,[\"8726\",\"static/chunks/8726-fd60949e252ac8d1.js\",\"3185\",\"static/chunks/app/layout-e2ef1005801dc1ed.js\"],\"Toaster\"]\n15:I[20154,[\"8422\",\"static/chunks/66ec4792-3d3220126f3e74ee.js\",\"6950\",\"static/chunks/f8025e75-5dd99dd1a689ea97.js\",\"6795\",\"static/chunks/6795-1b544390b9b7cb09.js\",\"5214\",\"static/chunks/5214-cc3a3a8017cc6e4d.js\",\"7078\",\"static/chunks/7078-09556dce22020d83.js\",\"3154\",\"static/chunks/3154-0aa2f15561902c10.js\",\"7562\",\"static/chunks/7562-b71a43604fd32292.js\",\"4481\",\"static/chunks/4481-14671e1e3d1ffff7.js\",\"4662\",\"static/chunks/4662-205abea99f9b3adf.js\",\"9160\",\"static/chunks/app/not-found-b9aad74520590312.js\"],\"default\"]\n16:I[78598,[\"8726\",\"static/chunks/8726-fd60949e252ac8d1.js\",\"3185\",\"static/chunks/app/layout-e2ef1005801dc1ed.js\"],\"default\"]\n17:I[56192,[\"8726\",\"static/chunks/8726-fd60949e252ac8d1.js\",\"3185\",\"static/chunks/app/layout-e2ef1005801dc1ed.js\"],\"default\"]\n19:I[76130,[],\"\"]\nd:[\"lang\",\"en\",\"d\"]\ne:[\"subject\",\"swift\",\"d\"]\nf:[\"part\",\"part-2\",\"d\"]\n10:[\"pattern\",\"swift-methods-in-structs\",\"d\"]\n11:[\"mode\",\"project\",\"oc\"]\n1a:[]\n"])</script><script>self.__next_f.push([1,"0:[[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/d071dca61fbb01bf.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}],[\"$\",\"link\",\"1\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/907864b6faf28c32.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}]],[\"$\",\"$L9\",null,{\"buildId\":\"leyaa-prod-build-id\",\"assetPrefix\":\"\",\"initialCanonicalUrl\":\"/en/codefly/learn/swift/part-2/swift-methods-in-structs/project\",\"initialTree\":[\"\",{\"children\":[[\"lang\",\"en\",\"d\"],{\"children\":[\"codefly\",{\"children\":[\"learn\",{\"children\":[[\"subject\",\"swift\",\"d\"],{\"children\":[[\"part\",\"part-2\",\"d\"],{\"children\":[[\"pattern\",\"swift-methods-in-structs\",\"d\"],{\"children\":[[\"mode\",\"project\",\"oc\"],{\"children\":[\"__PAGE__\",{}]}]}]}]}]}]}]}]},\"$undefined\",\"$undefined\",true],\"initialSeedData\":[\"\",{\"children\":[[\"lang\",\"en\",\"d\"],{\"children\":[\"codefly\",{\"children\":[\"learn\",{\"children\":[[\"subject\",\"swift\",\"d\"],{\"children\":[[\"part\",\"part-2\",\"d\"],{\"children\":[[\"pattern\",\"swift-methods-in-structs\",\"d\"],{\"children\":[[\"mode\",\"project\",\"oc\"],{\"children\":[\"__PAGE__\",{},[[\"$La\",\"$Lb\"],null],null]},[\"$\",\"$Lc\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$d\",\"children\",\"codefly\",\"children\",\"learn\",\"children\",\"$e\",\"children\",\"$f\",\"children\",\"$10\",\"children\",\"$11\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L12\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/ada9a246b4e9c7f9.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}],[\"$\",\"link\",\"1\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/725c7861d1898ba8.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}],[\"$\",\"link\",\"2\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/a97525600f39e233.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}],[\"$\",\"link\",\"3\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/989afc350a4b3a28.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}],[\"$\",\"link\",\"4\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/ccfdf35169bd162c.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}],[\"$\",\"link\",\"5\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/417e7aee3e688b57.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}]]}],null]},[\"$\",\"$Lc\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$d\",\"children\",\"codefly\",\"children\",\"learn\",\"children\",\"$e\",\"children\",\"$f\",\"children\",\"$10\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L12\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":null}],null]},[\"$\",\"$Lc\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$d\",\"children\",\"codefly\",\"children\",\"learn\",\"children\",\"$e\",\"children\",\"$f\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L12\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":null}],null]},[\"$\",\"$Lc\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$d\",\"children\",\"codefly\",\"children\",\"learn\",\"children\",\"$e\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L12\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":null}],null]},[\"$\",\"$Lc\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$d\",\"children\",\"codefly\",\"children\",\"learn\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L12\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":null}],null]},[\"$\",\"$Lc\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$d\",\"children\",\"codefly\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L12\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":null}],null]},[\"$\",\"$Lc\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$d\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L12\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":null}],null]},[[\"$\",\"html\",null,{\"lang\":\"en\",\"children\":[[\"$\",\"head\",null,{\"children\":[[\"$\",\"meta\",null,{\"name\":\"theme-color\",\"content\":\"#5f56fe\"}],[\"$\",\"meta\",null,{\"name\":\"msapplication-TileColor\",\"content\":\"#5f56fe\"}],[\"$\",\"$L13\",null,{\"src\":\"https://www.googletagmanager.com/gtag/js?id=G-N2NY2DMMDW\",\"strategy\":\"afterInteractive\"}],[\"$\",\"$L13\",null,{\"id\":\"google-analytics\",\"strategy\":\"afterInteractive\",\"children\":\"\\n window.dataLayer = window.dataLayer || [];\\n function gtag(){dataLayer.push(arguments);}\\n gtag('js', new Date());\\n gtag('config', 'G-N2NY2DMMDW', {\\n page_path: window.location.pathname,\\n });\\n \"}],[\"$\",\"script\",null,{\"async\":true,\"src\":\"https://www.googletagmanager.com/gtag/js?id=AW-18214378714\"}],[\"$\",\"$L13\",null,{\"children\":\"\\n window.dataLayer = window.dataLayer || [];\\n function gtag() {\\n dataLayer.push(arguments);\\n }\\n gtag('js', new Date());\\n gtag('config', 'AW-18214378714');\\n \"}],[\"$\",\"script\",null,{\"data-grow-initializer\":\"\",\"suppressHydrationWarning\":true,\"dangerouslySetInnerHTML\":{\"__html\":\"!(function(){window.growMe||((window.growMe=function(e){window.growMe._.push(e);}),(window.growMe._=[]));var e=document.createElement(\\\"script\\\");(e.type=\\\"text/javascript\\\"),(e.src=\\\"https://faves.grow.me/main.js\\\"),(e.defer=!0),e.setAttribute(\\\"data-grow-faves-site-id\\\",\\\"U2l0ZTo0MGIxZDBlZC0wNzdlLTQ0NjgtOThmOC1kNDYyZGMwM2IwMWY=\\\");var t=document.getElementsByTagName(\\\"script\\\")[0];t.parentNode.insertBefore(e,t);})();\"}}],[\"$\",\"$L13\",null,{\"src\":\"//scripts.scriptwrapper.com/tags/40b1d0ed-077e-4468-98f8-d462dc03b01f.js\",\"strategy\":\"afterInteractive\",\"data-noptimize\":\"1\",\"data-cfasync\":\"false\"}],[\"$\",\"script\",null,{\"type\":\"application/ld+json\",\"suppressHydrationWarning\":true,\"dangerouslySetInnerHTML\":{\"__html\":\"{\\\"@context\\\":\\\"https://schema.org\\\",\\\"@type\\\":\\\"WebApplication\\\",\\\"name\\\":\\\"Leyaa.ai\\\",\\\"description\\\":\\\"Leyaa.ai builds learning intelligence that understands how you learn - guiding what to study, how to practice, and when to move forward.\\\",\\\"url\\\":\\\"https://leyaa.ai\\\",\\\"applicationCategory\\\":\\\"EducationalApplication\\\",\\\"operatingSystem\\\":\\\"Web\\\",\\\"offers\\\":{\\\"@type\\\":\\\"Offer\\\",\\\"price\\\":\\\"0\\\",\\\"priceCurrency\\\":\\\"USD\\\"},\\\"creator\\\":{\\\"@type\\\":\\\"Organization\\\",\\\"name\\\":\\\"Leyaa.ai\\\"}}\"}}],[\"$\",\"$L13\",null,{\"id\":\"clarity-script\",\"strategy\":\"afterInteractive\",\"dangerouslySetInnerHTML\":{\"__html\":\"\\n (function(c,l,a,r,i,t,y){\\n c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};\\n t=l.createElement(r);t.async=1;t.src=\\\"https://www.clarity.ms/tag/\\\"+i;\\n y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);\\n })(window, document, \\\"clarity\\\", \\\"script\\\", \\\"w4gxh6rdmh\\\");\\n \"}}]]}],[\"$\",\"body\",null,{\"children\":[[\"$\",\"$L14\",null,{\"containerStyle\":{\"top\":70}}],[\"$\",\"$Lc\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L12\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":[\"$\",\"$L15\",null,{}],\"notFoundStyles\":[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/a97525600f39e233.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}]],\"styles\":null}],[\"$\",\"$L16\",null,{}],\" \",[\"$\",\"$L17\",null,{}]]}]]}],null],null],\"couldBeIntercepted\":false,\"initialHead\":[false,\"$L18\"],\"globalErrorComponent\":\"$19\",\"missingSlots\":\"$W1a\"}]]\n"])</script><script>self.__next_f.push([1,"1c:I[43489,[\"8422\",\"static/chunks/66ec4792-3d3220126f3e74ee.js\",\"6950\",\"static/chunks/f8025e75-5dd99dd1a689ea97.js\",\"6051\",\"static/chunks/795d4814-8b40e2eae05948ca.js\",\"5706\",\"static/chunks/9c4e2130-0ef90d15550fcb15.js\",\"522\",\"static/chunks/94730671-fd9628eddbd5107b.js\",\"7699\",\"static/chunks/8e1d74a4-d3c1e3cfb4eeea98.js\",\"7240\",\"static/chunks/53c13509-a3e4ac0640ae03f2.js\",\"9956\",\"static/chunks/ee560e2c-27ba7a66c659fa82.js\",\"2516\",\"static/chunks/f7333993-31ab72eb9b202e3d.js\",\"1779\",\"static/chunks/0e762574-cee3ec2351d97ca0.js\",\"6795\",\"static/chunks/6795-1b544390b9b7cb09.js\",\"5214\",\"static/chunks/5214-cc3a3a8017cc6e4d.js\",\"7078\",\"static/chunks/7078-09556dce22020d83.js\",\"4889\",\"static/chunks/4889-de8d1c4f2ff2f134.js\",\"8726\",\"static/chunks/8726-fd60949e252ac8d1.js\",\"7534\",\"static/chunks/7534-02c906a11e681445.js\",\"4220\",\"static/chunks/4220-e3c1264f6e3453a7.js\",\"7562\",\"static/chunks/7562-b71a43604fd32292.js\",\"4481\",\"static/chunks/4481-14671e1e3d1ffff7.js\",\"6857\",\"static/chunks/6857-f9121be732678b30.js\",\"2202\",\"static/chunks/2202-dad5d33fadc7ea73.js\",\"1786\",\"static/chunks/1786-7253d5c3df8362c5.js\",\"7029\",\"static/chunks/app/%5Blang%5D/codefly/learn/%5Bsubject%5D/%5Bpart%5D/%5Bpattern%5D/%5B%5B...mode%5D%5D/page-b11550cd55444a64.js\"],\"default\"]\n1b:T45a,{\"@context\":\"https://schema.org\",\"@type\":\"LearningResource\",\"name\":\"Methods in structs in Swift - Mini Project: Build \u0026 Apply\",\"description\":\"Apply Methods in structs in Swift in a hands-on mini project. Build something real with step-by-step guidance. Learn by doing, not just reading.\",\"url\":\"https://leyaa.ai/codefly/learn/swift/part-2/swift-methods-in-structs/project\",\"learningResourceType\":\"Practice Exercise\",\"programmingLanguage\":\"Swift\",\"inLanguage\":\"en\",\"isAccessibleForFree\":true,\"teaches\":\"Methods in structs in Swift - Mini Project: Build \u0026 Apply\",\"provider\":{\"@type\":\"Organization\",\"url\":\"https://leyaa.ai\"},\"educationalLevel\":\"Beginner to Advanced\",\"breadcrumb\":{\"@type\":\"BreadcrumbList\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https://"])</script><script>self.__next_f.push([1,"leyaa.ai\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Methods in structs in Swift - Mini Project: Build \u0026 Apply\",\"item\":\"https://leyaa.ai/codefly/learn/swift/part-2/swift-methods-in-structs\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Project\",\"item\":\"https://leyaa.ai/codefly/learn/swift/part-2/swift-methods-in-structs/project\"}]}}"])</script><script>self.__next_f.push([1,"b:[[[\"$\",\"script\",\"0\",{\"type\":\"application/ld+json\",\"dangerouslySetInnerHTML\":{\"__html\":\"$1b\"}}]],[\"$\",\"$L1c\",null,{\"subject\":\"swift\",\"dbSubject\":\"swift\",\"part\":\"part-2\",\"pattern\":\"swift_methods_in_structs\",\"modeSlug\":\"project\",\"lang\":\"en\",\"internalLinks\":[{\"code\":\"LMC\",\"slug\":\"\",\"label\":\"📖 Learn\",\"href\":\"/codefly/learn/swift/part-2/swift-methods-in-structs\",\"active\":false},{\"code\":\"LMCWHY\",\"slug\":\"why\",\"label\":\"💡 Why Learn This\",\"href\":\"/codefly/learn/swift/part-2/swift-methods-in-structs/why\",\"active\":false},{\"code\":\"DLM\",\"slug\":\"deep\",\"label\":\"💡 Deep Learn Mode\",\"href\":\"/codefly/learn/swift/part-2/swift-methods-in-structs/deep\",\"active\":false},{\"code\":\"TMC\",\"slug\":\"try\",\"label\":\"✏️ Try It\",\"href\":\"/codefly/learn/swift/part-2/swift-methods-in-structs/try\",\"active\":false},{\"code\":\"VMC\",\"slug\":\"visualize\",\"label\":\"👁 Visualize\",\"href\":\"/codefly/learn/swift/part-2/swift-methods-in-structs/visualize\",\"active\":false},{\"code\":\"TCM\",\"slug\":\"complexity\",\"label\":\"⏱ Complexity\",\"href\":\"/codefly/learn/swift/part-2/swift-methods-in-structs/complexity\",\"active\":false},{\"code\":\"CMC\",\"slug\":\"challenge\",\"label\":\"🏆 Challenge\",\"href\":\"/codefly/learn/swift/part-2/swift-methods-in-structs/challenge\",\"active\":false},{\"code\":\"PMC\",\"slug\":\"project\",\"label\":\"📁 Project\",\"href\":\"/codefly/learn/swift/part-2/swift-methods-in-structs/project\",\"active\":true},{\"code\":\"RMC\",\"slug\":\"review\",\"label\":\"🧠 Quick Review\",\"href\":\"/codefly/learn/swift/part-2/swift-methods-in-structs/review\",\"active\":false}],\"isLoggedIn\":false,\"seoH1\":\"Methods in structs in Swift - Mini Project: Build \u0026 Apply\",\"contentData\":{\"pattern_id\":\"swift_methods_in_structs\",\"metadata\":{\"slot_map\":{\"LMCWHY\":\"LMCWHY\",\"LMC\":\"LMC\",\"TMC\":\"TMC\",\"CMC\":\"CMC\",\"RMC\":\"RMC\",\"VMC\":\"VMC\",\"TCM\":\"TCM\",\"PMC\":\"PMC\",\"DLM\":\"DLM\"}},\"modes\":{\"PMC\":{\"project_title\":\"Methods in structs\",\"mode\":\"PMC_v4.2\",\"language\":\"swift\",\"content_type\":\"Programming\",\"difficulty\":\"beginner\",\"scenario\":\"\u003cp\u003eImagine you are creating a simple app to keep track of books in a small library. Each book has a title and an author. You want to add a feature that shows a short description of the book using a method inside a struct.\u003c/p\u003e\",\"goal\":\"\u003cp\u003eYou will create a \u003ccode\u003eBook\u003c/code\u003e struct with properties for the title and author. Then, you will add a method inside the struct that returns a description string combining the title and author. Finally, you will create a book instance and print its description.\u003c/p\u003e\",\"requirements\":[\"Create a struct called \u003ccode\u003eBook\u003c/code\u003e with two \u003ccode\u003eString\u003c/code\u003e properties: \u003ccode\u003etitle\u003c/code\u003e and \u003ccode\u003eauthor\u003c/code\u003e.\",\"Add a method called \u003ccode\u003edescription()\u003c/code\u003e inside the \u003ccode\u003eBook\u003c/code\u003e struct that returns a \u003ccode\u003eString\u003c/code\u003e combining the title and author in the format: \\\"\u003ctitle\u003e by \u003cauthor\u003e\\\".\",\"Create an instance of \u003ccode\u003eBook\u003c/code\u003e with the title \\\"The Swift Journey\\\" and author \\\"Alex Developer\\\".\",\"Print the result of calling the \u003ccode\u003edescription()\u003c/code\u003e method on the book instance.\"],\"steps\":[{\"step_number\":1,\"title\":\"Create the Book struct with properties\",\"instruction\":\"Create a struct called \u003ccode\u003eBook\u003c/code\u003e with two \u003ccode\u003eString\u003c/code\u003e properties named \u003ccode\u003etitle\u003c/code\u003e and \u003ccode\u003eauthor\u003c/code\u003e.\",\"starter_code\":\"\",\"placeholder_code\":\"# Create the Book struct with title and author properties\\n# Your code here\",\"solution_code\":\"struct Book {\\n var title: String\\n var author: String\\n}\",\"validation_type\":\"code_contains\",\"validation_data\":[\"struct Book\",\"var title: String\",\"var author: String\"],\"hint\":\"\u003cp\u003eUse \u003ccode\u003estruct Book { }\u003c/code\u003e and inside it declare \u003ccode\u003evar title: String\u003c/code\u003e and \u003ccode\u003evar author: String\u003c/code\u003e.\u003c/p\u003e\",\"timeout_seconds\":5},{\"step_number\":2,\"title\":\"Add a description() method to the Book struct\",\"instruction\":\"Inside the \u003ccode\u003eBook\u003c/code\u003e struct, add a method called \u003ccode\u003edescription()\u003c/code\u003e that returns a \u003ccode\u003eString\u003c/code\u003e combining the \u003ccode\u003etitle\u003c/code\u003e and \u003ccode\u003eauthor\u003c/code\u003e in the format: \\\"\u003ctitle\u003e by \u003cauthor\u003e\\\".\",\"starter_code\":\"struct Book {\\n var title: String\\n var author: String\\n}\",\"placeholder_code\":\"struct Book {\\n var title: String\\n var author: String\\n\\n // Add the description() method below\\n // Your code here\\n}\",\"solution_code\":\"struct Book {\\n var title: String\\n var author: String\\n\\n func description() -\u003e String {\\n return \\\"\\\\(title) by \\\\(author)\\\"\\n }\\n}\",\"validation_type\":\"code_contains\",\"validation_data\":[\"func description() -\u003e String\",\"return \\\"\\\\(title) by \\\\(author)\\\"\"],\"hint\":\"\u003cp\u003eDefine \u003ccode\u003efunc description() -\u003e String\u003c/code\u003e inside the struct and return the string using \u003ccode\u003e\\\"\\\\(title) by \\\\(author)\\\"\u003c/code\u003e.\u003c/p\u003e\",\"timeout_seconds\":5},{\"step_number\":3,\"title\":\"Create a Book instance\",\"instruction\":\"Create a constant called \u003ccode\u003emyBook\u003c/code\u003e of type \u003ccode\u003eBook\u003c/code\u003e with the title set to \u003ccode\u003e\\\"The Swift Journey\\\"\u003c/code\u003e and the author set to \u003ccode\u003e\\\"Alex Developer\\\"\u003c/code\u003e.\",\"starter_code\":\"struct Book {\\n var title: String\\n var author: String\\n\\n func description() -\u003e String {\\n return \\\"\\\\(title) by \\\\(author)\\\"\\n }\\n}\",\"placeholder_code\":\"struct Book {\\n var title: String\\n var author: String\\n\\n func description() -\u003e String {\\n return \\\"\\\\(title) by \\\\(author)\\\"\\n }\\n}\\n\\n// Create the myBook instance below\\n// Your code here\",\"solution_code\":\"struct Book {\\n var title: String\\n var author: String\\n\\n func description() -\u003e String {\\n return \\\"\\\\(title) by \\\\(author)\\\"\\n }\\n}\\n\\nlet myBook = Book(title: \\\"The Swift Journey\\\", author: \\\"Alex Developer\\\")\",\"validation_type\":\"code_contains\",\"validation_data\":[\"let myBook = Book(title: \\\"The Swift Journey\\\", author: \\\"Alex Developer\\\")\"],\"hint\":\"\u003cp\u003eUse \u003ccode\u003elet myBook = Book(title: \\\"The Swift Journey\\\", author: \\\"Alex Developer\\\")\u003c/code\u003e to create the instance.\u003c/p\u003e\",\"timeout_seconds\":5},{\"step_number\":4,\"title\":\"Print the book description\",\"instruction\":\"Write a \u003ccode\u003eprint\u003c/code\u003e statement that calls the \u003ccode\u003edescription()\u003c/code\u003e method on \u003ccode\u003emyBook\u003c/code\u003e and prints the result.\",\"starter_code\":\"struct Book {\\n var title: String\\n var author: String\\n\\n func description() -\u003e String {\\n return \\\"\\\\(title) by \\\\(author)\\\"\\n }\\n}\\n\\nlet myBook = Book(title: \\\"The Swift Journey\\\", author: \\\"Alex Developer\\\")\",\"placeholder_code\":\"struct Book {\\n var title: String\\n var author: String\\n\\n func description() -\u003e String {\\n return \\\"\\\\(title) by \\\\(author)\\\"\\n }\\n}\\n\\nlet myBook = Book(title: \\\"The Swift Journey\\\", author: \\\"Alex Developer\\\")\\n\\n// Print the description of myBook below\\n# Your code here\",\"solution_code\":\"struct Book {\\n var title: String\\n var author: String\\n\\n func description() -\u003e String {\\n return \\\"\\\\(title) by \\\\(author)\\\"\\n }\\n}\\n\\nlet myBook = Book(title: \\\"The Swift Journey\\\", author: \\\"Alex Developer\\\")\\n\\nprint(myBook.description())\",\"validation_type\":\"output_match\",\"validation_data\":[\"The Swift Journey by Alex Developer\"],\"hint\":\"\u003cp\u003eUse \u003ccode\u003eprint(myBook.description())\u003c/code\u003e to show the description.\u003c/p\u003e\",\"timeout_seconds\":5}],\"final_expected_output\":\"The Swift Journey by Alex Developer\",\"summary\":[\"You learned how to create a struct in Swift with properties.\",\"You added a method inside the struct to return a formatted string.\",\"You created an instance of the struct with specific values.\",\"You called the method on the instance and printed the result.\"],\"extensions\":[\"Add another method to the \u003ccode\u003eBook\u003c/code\u003e struct that returns the title in uppercase.\",\"Create an array of \u003ccode\u003eBook\u003c/code\u003e instances and print descriptions for all books using a loop.\"],\"project_context\":{\"real_world_use\":\"Structs with methods are used to model real-world objects and their behaviors in apps, like books in a library app.\",\"job_relevance\":\"Understanding how to add methods to structs is important for writing clean, organized Swift code in iOS app development.\"},\"metadata\":{\"version\":\"4.2\",\"content_type\":\"programming\",\"total_steps\":4,\"estimated_time_minutes\":15}}},\"subject\":\"swift\",\"title\":\"Methods in structs\",\"puzzleDetails\":[{\"_id\":\"69b1784e45baa2b58545c6d1\",\"question_number\":1,\"question\":\"\u003cp\u003eWhat is the main purpose of a method inside a Swift struct?\u003c/p\u003e\",\"options\":{\"A\":\"To define actions the struct can perform\",\"B\":\"To store data values\",\"C\":\"To create a new struct type\",\"D\":\"To import external libraries\"},\"correct_answer\":[\"A\"],\"solution_explanation\":\"\u003cdiv class='solution-box'\u003e\u003cspan class='solution-label'\u003e\u003cstrong\u003eSolution:\u003c/strong\u003e\u003c/span\u003e\u003col\u003e\u003cli\u003e\u003ch4\u003eStep 1: Understand what methods do in structs\u003c/h4\u003eMethods are functions inside structs that define behaviors or actions the struct can perform.\u003c/li\u003e\u003cli\u003e\u003ch4\u003eStep 2: Differentiate methods from data storage\u003c/h4\u003eProperties store data, while methods perform actions using that data.\u003c/li\u003e\u003cli\u003e\u003ch4\u003eFinal Answer:\u003c/h4\u003e\u003cstrong\u003eTo define actions the struct can perform\u003c/strong\u003e -\u003e Option A\u003c/li\u003e\u003cli\u003e\u003ch4\u003eQuick Check:\u003c/h4\u003eMethods = actions in structs [OK]\u003c/li\u003e\u003c/ol\u003e\u003c/div\u003e\",\"quick_trick\":\"Methods = actions inside structs, properties = data [OK]\",\"common_mistakes\":[\"Confusing methods with properties\",\"Thinking methods create new types\",\"Mixing methods with import statements\"],\"subject\":\"swift\",\"pattern\":\"Methods in structs\",\"category\":\"Programming\",\"question_format\":\"MCQ\",\"question_mode\":\"conceptual\",\"difficulty\":\"easy\",\"topic\":\"Structures and Value Semantics\",\"type\":\"Programming\",\"question_type\":\"conceptual\",\"level\":\"easy\",\"test_type\":\"puzzle\",\"pattern_id\":\"swift_methods_in_structs\",\"subjectTitle\":\"Swift\",\"part\":\"part-2\",\"set_no\":1,\"usage_count\":0,\"topic_id\":\"swift_p2_t2\"},{\"_id\":\"69b1784e45baa2b58545c6d2\",\"question_number\":2,\"question\":\"\u003cp\u003eWhich keyword must be used before a method in a Swift struct if it changes the struct's properties?\u003c/p\u003e\",\"options\":{\"A\":\"final\",\"B\":\"override\",\"C\":\"static\",\"D\":\"mutating\"},\"correct_answer\":[\"D\"],\"solution_explanation\":\"\u003cdiv class='solution-box'\u003e\u003cspan class='solution-label'\u003e\u003cstrong\u003eSolution:\u003c/strong\u003e\u003c/span\u003e\u003col\u003e\u003cli\u003e\u003ch4\u003eStep 1: Recall Swift's rule for modifying struct properties\u003c/h4\u003eStruct methods that change properties must be marked with \u003ccode\u003emutating\u003c/code\u003e to allow modification.\u003c/li\u003e\u003cli\u003e\u003ch4\u003eStep 2: Identify the correct keyword\u003c/h4\u003eAmong the options, only \u003ccode\u003emutating\u003c/code\u003e enables property changes inside struct methods.\u003c/li\u003e\u003cli\u003e\u003ch4\u003eFinal Answer:\u003c/h4\u003e\u003cstrong\u003emutating\u003c/strong\u003e -\u003e Option D\u003c/li\u003e\u003cli\u003e\u003ch4\u003eQuick Check:\u003c/h4\u003eChange properties = mutating [OK]\u003c/li\u003e\u003c/ol\u003e\u003c/div\u003e\",\"quick_trick\":\"Use mutating for methods that change struct properties [OK]\",\"common_mistakes\":[\"Using override in structs (only for classes)\",\"Confusing static with mutating\",\"Omitting mutating keyword causes errors\"],\"subject\":\"swift\",\"pattern\":\"Methods in structs\",\"category\":\"Programming\",\"question_format\":\"MCQ\",\"question_mode\":\"syntax\",\"difficulty\":\"easy\",\"topic\":\"Structures and Value Semantics\",\"type\":\"Programming\",\"question_type\":\"syntax\",\"level\":\"easy\",\"test_type\":\"puzzle\",\"pattern_id\":\"swift_methods_in_structs\",\"subjectTitle\":\"Swift\",\"part\":\"part-2\",\"set_no\":1,\"usage_count\":1,\"last_used_at\":\"2026-06-10T08:04:26.738Z\",\"topic_id\":\"swift_p2_t2\"},{\"_id\":\"69b1784e45baa2b58545c6d3\",\"question_number\":3,\"question\":\"\u003cp\u003eWhat will be the output of this Swift code?\u003c/p\u003e\u003cpre\u003estruct Counter {\\n var count = 0\\n mutating func increment() {\\n count += 1\\n }\\n}\\n\\nvar myCounter = Counter()\\nmyCounter.increment()\\nprint(myCounter.count)\u003c/pre\u003e\",\"options\":{\"A\":\"0\",\"B\":\"1\",\"C\":\"Error: Cannot modify property\",\"D\":\"nil\"},\"correct_answer\":[\"B\"],\"solution_explanation\":\"\u003cdiv class='solution-box'\u003e\u003cspan class='solution-label'\u003e\u003cstrong\u003eSolution:\u003c/strong\u003e\u003c/span\u003e\u003col\u003e\u003cli\u003e\u003ch4\u003eStep 1: Understand the mutating method increment()\u003c/h4\u003eThe method increases count by 1 and is marked mutating, so it can change the property.\u003c/li\u003e\u003cli\u003e\u003ch4\u003eStep 2: Trace the code execution\u003c/h4\u003emyCounter starts with count = 0, increment() adds 1, so count becomes 1, then printed.\u003c/li\u003e\u003cli\u003e\u003ch4\u003eFinal Answer:\u003c/h4\u003e\u003cstrong\u003e1\u003c/strong\u003e -\u003e Option B\u003c/li\u003e\u003cli\u003e\u003ch4\u003eQuick Check:\u003c/h4\u003emutating method changes count to 1 [OK]\u003c/li\u003e\u003c/ol\u003e\u003c/div\u003e\",\"quick_trick\":\"mutating methods can change properties, so count increments [OK]\",\"common_mistakes\":[\"Forgetting mutating causes error\",\"Expecting count to stay 0\",\"Thinking print outputs nil\"],\"subject\":\"swift\",\"pattern\":\"Methods in structs\",\"category\":\"Programming\",\"question_format\":\"MCQ\",\"question_mode\":\"code_output\",\"difficulty\":\"medium\",\"topic\":\"Structures and Value Semantics\",\"type\":\"Programming\",\"question_type\":\"code_output\",\"level\":\"medium\",\"test_type\":\"puzzle\",\"pattern_id\":\"swift_methods_in_structs\",\"subjectTitle\":\"Swift\",\"part\":\"part-2\",\"set_no\":1,\"usage_count\":1,\"last_used_at\":\"2026-06-17T15:11:38.690Z\",\"topic_id\":\"swift_p2_t2\"},{\"_id\":\"69b1784e45baa2b58545c6d4\",\"question_number\":4,\"question\":\"\u003cp\u003eIdentify the error in this Swift struct method and how to fix it:\u003c/p\u003e\u003cpre\u003estruct Score {\\n var points = 0\\n func addPoint() {\\n points += 1\\n }\\n}\u003c/pre\u003e\",\"options\":{\"A\":\"Method should return \u003ccode\u003epoints\u003c/code\u003e value\",\"B\":\"Property \u003ccode\u003epoints\u003c/code\u003e must be \u003ccode\u003elet\u003c/code\u003e instead of \u003ccode\u003evar\u003c/code\u003e\",\"C\":\"Method must be marked \u003ccode\u003emutating\u003c/code\u003e to change \u003ccode\u003epoints\u003c/code\u003e\",\"D\":\"No error, code runs fine\"},\"correct_answer\":[\"C\"],\"solution_explanation\":\"\u003cdiv class='solution-box'\u003e\u003cspan class='solution-label'\u003e\u003cstrong\u003eSolution:\u003c/strong\u003e\u003c/span\u003e\u003col\u003e\u003cli\u003e\u003ch4\u003eStep 1: Check if method modifies a property\u003c/h4\u003eaddPoint() tries to increase points by 1, which modifies the struct's property.\u003c/li\u003e\u003cli\u003e\u003ch4\u003eStep 2: Recall Swift rule for modifying properties in structs\u003c/h4\u003eMethods that modify properties must be marked \u003ccode\u003emutating\u003c/code\u003e, otherwise compiler error occurs.\u003c/li\u003e\u003cli\u003e\u003ch4\u003eFinal Answer:\u003c/h4\u003e\u003cstrong\u003eMethod must be marked \u003ccode\u003emutating\u003c/code\u003e to change \u003ccode\u003epoints\u003c/code\u003e\u003c/strong\u003e -\u003e Option C\u003c/li\u003e\u003cli\u003e\u003ch4\u003eQuick Check:\u003c/h4\u003eModify property in struct method = mutating needed [OK]\u003c/li\u003e\u003c/ol\u003e\u003c/div\u003e\",\"quick_trick\":\"Add mutating keyword to methods that change struct properties [OK]\",\"common_mistakes\":[\"Omitting mutating keyword\",\"Changing var to let (makes property immutable)\",\"Thinking method must return value\"],\"subject\":\"swift\",\"pattern\":\"Methods in structs\",\"category\":\"Programming\",\"question_format\":\"MCQ\",\"question_mode\":\"debug\",\"difficulty\":\"medium\",\"topic\":\"Structures and Value Semantics\",\"type\":\"Programming\",\"question_type\":\"debug\",\"level\":\"medium\",\"test_type\":\"puzzle\",\"pattern_id\":\"swift_methods_in_structs\",\"subjectTitle\":\"Swift\",\"part\":\"part-2\",\"set_no\":1,\"usage_count\":0,\"topic_id\":\"swift_p2_t2\"},{\"_id\":\"69b1784e45baa2b58545c6d5\",\"question_number\":5,\"question\":\"\u003cp\u003eGiven this struct, which method correctly resets the \u003ccode\u003elevel\u003c/code\u003e property to 1?\u003c/p\u003e\u003cpre\u003estruct Game {\\n var level = 5\\n\\n // Which method resets level to 1?\\n}\u003c/pre\u003e\",\"options\":{\"A\":\"mutating func reset() { level = 1 }\",\"B\":\"mutating func reset() -\u003e Int { return 1 }\",\"C\":\"static func reset() { level = 1 }\",\"D\":\"func reset() { level = 1 }\"},\"correct_answer\":[\"A\"],\"solution_explanation\":\"\u003cdiv class='solution-box'\u003e\u003cspan class='solution-label'\u003e\u003cstrong\u003eSolution:\u003c/strong\u003e\u003c/span\u003e\u003col\u003e\u003cli\u003e\u003ch4\u003eStep 1: Identify method that changes a property\u003c/h4\u003eResetting level means changing the property, so method must be mutating.\u003c/li\u003e\u003cli\u003e\u003ch4\u003eStep 2: Check each option\u003c/h4\u003eA lacks mutating, so cannot modify property. B is mutating but only returns 1 without changing level. C is static and cannot access instance property. D is mutating and correctly sets level to 1.\u003c/li\u003e\u003cli\u003e\u003ch4\u003eFinal Answer:\u003c/h4\u003e\u003cstrong\u003emutating func reset() { level = 1 }\u003c/strong\u003e -\u003e Option A\u003c/li\u003e\u003cli\u003e\u003ch4\u003eQuick Check:\u003c/h4\u003eChange property in struct method = mutating func [OK]\u003c/li\u003e\u003c/ol\u003e\u003c/div\u003e\",\"quick_trick\":\"Use mutating func to change struct properties inside methods [OK]\",\"common_mistakes\":[\"Forgetting mutating keyword\",\"Using static func to access instance properties\",\"Returning value instead of changing property\"],\"subject\":\"swift\",\"pattern\":\"Methods in structs\",\"category\":\"Programming\",\"question_format\":\"MCQ\",\"question_mode\":\"application\",\"difficulty\":\"hard\",\"topic\":\"Structures and Value Semantics\",\"type\":\"Programming\",\"question_type\":\"application\",\"level\":\"hard\",\"test_type\":\"puzzle\",\"pattern_id\":\"swift_methods_in_structs\",\"subjectTitle\":\"Swift\",\"part\":\"part-2\",\"set_no\":1,\"usage_count\":0,\"topic_id\":\"swift_p2_t2\"}]},\"syllabusData\":{\"subject\":\"swift\",\"part\":\"part-2\",\"difficulty\":\"intermediate\",\"metadata\":{\"total_topics\":9,\"total_patterns\":78,\"patterns_with_content\":78,\"created_at\":\"2026-02-28T20:07:09.058261Z\",\"version\":\"4.2\",\"upload_tool\":\"upload_to_mongo.py v2.0\"},\"part_title\":\"OOP and Protocols\",\"subjectTitle\":\"Swift\",\"topics\":[{\"topic_id\":\"swift_p2_t1\",\"title\":\"Closures\",\"order\":1,\"pattern_count\":10,\"patterns\":[{\"pattern_id\":\"swift_why_closures_are_fundamental_in_swift\",\"title\":\"Why closures are fundamental in Swift\",\"order\":1,\"has_content\":true},{\"pattern_id\":\"swift_closure_expression_syntax\",\"title\":\"Closure expression syntax\",\"order\":2,\"has_content\":true},{\"pattern_id\":\"swift_trailing_closure_syntax\",\"title\":\"Trailing closure syntax\",\"order\":3,\"has_content\":true},{\"pattern_id\":\"swift_shorthand_argument_names_0_1\",\"title\":\"Shorthand argument names ($0, $1)\",\"order\":4,\"has_content\":true},{\"pattern_id\":\"swift_closures_as_function_parameters\",\"title\":\"Closures as function parameters\",\"order\":5,\"has_content\":true},{\"pattern_id\":\"swift_capturing_values_from_context\",\"title\":\"Capturing values from context\",\"order\":6,\"has_content\":true},{\"pattern_id\":\"swift_escaping_closures_escaping\",\"title\":\"Escaping closures (@escaping)\",\"order\":7,\"has_content\":true},{\"pattern_id\":\"swift_autoclosures_autoclosure\",\"title\":\"Autoclosures (@autoclosure)\",\"order\":8,\"has_content\":true},{\"pattern_id\":\"swift_closures_are_reference_types\",\"title\":\"Closures are reference types\",\"order\":9,\"has_content\":true},{\"pattern_id\":\"swift_memory_implications_of_captures\",\"title\":\"Memory implications of captures\",\"order\":10,\"has_content\":true}]},{\"topic_id\":\"swift_p2_t2\",\"title\":\"Structures and Value Semantics\",\"order\":2,\"pattern_count\":9,\"patterns\":[{\"pattern_id\":\"swift_why_structs_are_preferred_in_swift\",\"title\":\"Why structs are preferred in Swift\",\"order\":1,\"has_content\":true},{\"pattern_id\":\"swift_struct_declaration_syntax\",\"title\":\"Struct declaration syntax\",\"order\":2,\"has_content\":true},{\"pattern_id\":\"swift_memberwise_initializer\",\"title\":\"Memberwise initializer\",\"order\":3,\"has_content\":true},{\"pattern_id\":\"swift_methods_in_structs\",\"title\":\"Methods in structs\",\"order\":4,\"has_content\":true},{\"pattern_id\":\"swift_mutating_methods_for_value_types\",\"title\":\"Mutating methods for value types\",\"order\":5,\"has_content\":true},{\"pattern_id\":\"swift_computed_properties\",\"title\":\"Computed properties\",\"order\":6,\"has_content\":true},{\"pattern_id\":\"swift_property_observers_willset_didset\",\"title\":\"Property observers (willSet, didSet)\",\"order\":7,\"has_content\":true},{\"pattern_id\":\"swift_structs_are_value_types_copy_on_assign\",\"title\":\"Structs are value types (copy on assign)\",\"order\":8,\"has_content\":true},{\"pattern_id\":\"swift_comparing_structs_vs_classes_decision\",\"title\":\"Comparing structs vs classes decision\",\"order\":9,\"has_content\":true}]},{\"topic_id\":\"swift_p2_t3\",\"title\":\"Classes and Reference Semantics\",\"order\":3,\"pattern_count\":9,\"patterns\":[{\"pattern_id\":\"swift_why_classes_exist_alongside_structs\",\"title\":\"Why classes exist alongside structs\",\"order\":1,\"has_content\":true},{\"pattern_id\":\"swift_class_declaration_syntax\",\"title\":\"Class declaration syntax\",\"order\":2,\"has_content\":true},{\"pattern_id\":\"swift_classes_are_reference_types\",\"title\":\"Classes are reference types\",\"order\":3,\"has_content\":true},{\"pattern_id\":\"swift_initializers_and_designated_init\",\"title\":\"Initializers and designated init\",\"order\":4,\"has_content\":true},{\"pattern_id\":\"swift_convenience_initializers\",\"title\":\"Convenience initializers\",\"order\":5,\"has_content\":true},{\"pattern_id\":\"swift_deinitializers_for_cleanup\",\"title\":\"Deinitializers for cleanup\",\"order\":6,\"has_content\":true},{\"pattern_id\":\"swift_identity_comparison\",\"title\":\"Identity comparison (===)\",\"order\":7,\"has_content\":true},{\"pattern_id\":\"swift_reference_sharing_and_side_effects\",\"title\":\"Reference sharing and side effects\",\"order\":8,\"has_content\":true},{\"pattern_id\":\"swift_arc_overview_for_memory_management\",\"title\":\"ARC overview for memory management\",\"order\":9,\"has_content\":true}]},{\"topic_id\":\"swift_p2_t4\",\"title\":\"Inheritance\",\"order\":4,\"pattern_count\":8,\"patterns\":[{\"pattern_id\":\"swift_why_inheritance_is_classonly_in_swift\",\"title\":\"Why inheritance is class-only in Swift\",\"order\":1,\"has_content\":true},{\"pattern_id\":\"swift_base_class_and_subclass\",\"title\":\"Base class and subclass\",\"order\":2,\"has_content\":true},{\"pattern_id\":\"swift_overriding_methods_and_properties\",\"title\":\"Overriding methods and properties\",\"order\":3,\"has_content\":true},{\"pattern_id\":\"swift_calling_super_for_parent_behavior\",\"title\":\"Calling super for parent behavior\",\"order\":4,\"has_content\":true},{\"pattern_id\":\"swift_preventing_overrides_with_final\",\"title\":\"Preventing overrides with final\",\"order\":5,\"has_content\":true},{\"pattern_id\":\"swift_type_casting_with_as_as_as\",\"title\":\"Type casting with as, as?, as!\",\"order\":6,\"has_content\":true},{\"pattern_id\":\"swift_is_operator_for_type_checking\",\"title\":\"Is operator for type checking\",\"order\":7,\"has_content\":true},{\"pattern_id\":\"swift_any_and_anyobject_types\",\"title\":\"Any and AnyObject types\",\"order\":8,\"has_content\":true}]},{\"topic_id\":\"swift_p2_t5\",\"title\":\"Protocols\",\"order\":5,\"pattern_count\":10,\"patterns\":[{\"pattern_id\":\"swift_why_protocoloriented_design_matters\",\"title\":\"Why protocol-oriented design matters\",\"order\":1,\"has_content\":true},{\"pattern_id\":\"swift_protocol_declaration_syntax\",\"title\":\"Protocol declaration syntax\",\"order\":2,\"has_content\":true},{\"pattern_id\":\"swift_protocol_requirements_methods_and_properties\",\"title\":\"Protocol requirements (methods and properties)\",\"order\":3,\"has_content\":true},{\"pattern_id\":\"swift_protocol_conformance\",\"title\":\"Protocol conformance\",\"order\":4,\"has_content\":true},{\"pattern_id\":\"swift_protocol_inheritance\",\"title\":\"Protocol inheritance\",\"order\":5,\"has_content\":true},{\"pattern_id\":\"swift_protocol_composition\",\"title\":\"Protocol composition\",\"order\":6,\"has_content\":true},{\"pattern_id\":\"swift_protocol_as_types\",\"title\":\"Protocol as types\",\"order\":7,\"has_content\":true},{\"pattern_id\":\"swift_protocol_extensions_with_default_implementations\",\"title\":\"Protocol extensions with default implementations\",\"order\":8,\"has_content\":true},{\"pattern_id\":\"swift_equatable_hashable_comparable_protocols\",\"title\":\"Equatable, Hashable, Comparable protocols\",\"order\":9,\"has_content\":true},{\"pattern_id\":\"swift_codable_protocol_for_encoding_decoding\",\"title\":\"Codable protocol for encoding/decoding\",\"order\":10,\"has_content\":true}]},{\"topic_id\":\"swift_p2_t6\",\"title\":\"Enumerations\",\"order\":6,\"pattern_count\":8,\"patterns\":[{\"pattern_id\":\"swift_why_enums_are_powerful_in_swift\",\"title\":\"Why enums are powerful in Swift\",\"order\":1,\"has_content\":true},{\"pattern_id\":\"swift_enum_declaration_and_cases\",\"title\":\"Enum declaration and cases\",\"order\":2,\"has_content\":true},{\"pattern_id\":\"swift_iterating_enum_cases_with_caseiterable\",\"title\":\"Iterating enum cases with CaseIterable\",\"order\":3,\"has_content\":true},{\"pattern_id\":\"swift_raw_values_for_enums\",\"title\":\"Raw values for enums\",\"order\":4,\"has_content\":true},{\"pattern_id\":\"swift_associated_values_per_case\",\"title\":\"Associated values per case\",\"order\":5,\"has_content\":true},{\"pattern_id\":\"swift_enum_methods_and_computed_properties\",\"title\":\"Enum methods and computed properties\",\"order\":6,\"has_content\":true},{\"pattern_id\":\"swift_recursive_enumerations\",\"title\":\"Recursive enumerations\",\"order\":7,\"has_content\":true},{\"pattern_id\":\"swift_enum_with_switch_pattern_matching\",\"title\":\"Enum with switch pattern matching\",\"order\":8,\"has_content\":true}]},{\"topic_id\":\"swift_p2_t7\",\"title\":\"Error Handling\",\"order\":7,\"pattern_count\":8,\"patterns\":[{\"pattern_id\":\"swift_why_error_handling_is_explicit_in_swift\",\"title\":\"Why error handling is explicit in Swift\",\"order\":1,\"has_content\":true},{\"pattern_id\":\"swift_error_protocol_conformance\",\"title\":\"Error protocol conformance\",\"order\":2,\"has_content\":true},{\"pattern_id\":\"swift_throwing_functions_with_throws\",\"title\":\"Throwing functions with throws\",\"order\":3,\"has_content\":true},{\"pattern_id\":\"swift_dotrycatch_execution_flow\",\"title\":\"Do-try-catch execution flow\",\"order\":4,\"has_content\":true},{\"pattern_id\":\"swift_try_for_optional_result\",\"title\":\"Try? for optional result\",\"order\":5,\"has_content\":true},{\"pattern_id\":\"swift_try_for_forced_unwrap\",\"title\":\"Try! for forced unwrap\",\"order\":6,\"has_content\":true},{\"pattern_id\":\"swift_rethrowing_functions\",\"title\":\"Rethrowing functions\",\"order\":7,\"has_content\":true},{\"pattern_id\":\"swift_defer_statement_for_cleanup\",\"title\":\"Defer statement for cleanup\",\"order\":8,\"has_content\":true}]},{\"topic_id\":\"swift_p2_t8\",\"title\":\"Extensions\",\"order\":8,\"pattern_count\":8,\"patterns\":[{\"pattern_id\":\"swift_why_extensions_add_capability_without_modifying\",\"title\":\"Why extensions add capability without modifying\",\"order\":1,\"has_content\":true},{\"pattern_id\":\"swift_extension_syntax\",\"title\":\"Extension syntax\",\"order\":2,\"has_content\":true},{\"pattern_id\":\"swift_adding_computed_properties\",\"title\":\"Adding computed properties\",\"order\":3,\"has_content\":true},{\"pattern_id\":\"swift_adding_methods\",\"title\":\"Adding methods\",\"order\":4,\"has_content\":true},{\"pattern_id\":\"swift_adding_initializers_via_extension\",\"title\":\"Adding initializers via extension\",\"order\":5,\"has_content\":true},{\"pattern_id\":\"swift_protocol_conformance_via_extension\",\"title\":\"Protocol conformance via extension\",\"order\":6,\"has_content\":true},{\"pattern_id\":\"swift_extensions_with_constraints\",\"title\":\"Extensions with constraints\",\"order\":7,\"has_content\":true},{\"pattern_id\":\"swift_extending_builtin_types\",\"title\":\"Extending built-in types\",\"order\":8,\"has_content\":true}]},{\"topic_id\":\"swift_p2_t9\",\"title\":\"Generics\",\"order\":9,\"pattern_count\":8,\"patterns\":[{\"pattern_id\":\"swift_why_generics_provide_type_safety\",\"title\":\"Why generics provide type safety\",\"order\":1,\"has_content\":true},{\"pattern_id\":\"swift_generic_function_declaration\",\"title\":\"Generic function declaration\",\"order\":2,\"has_content\":true},{\"pattern_id\":\"swift_generic_type_declaration\",\"title\":\"Generic type declaration\",\"order\":3,\"has_content\":true},{\"pattern_id\":\"swift_type_constraints_with_protocol_conformance\",\"title\":\"Type constraints with protocol conformance\",\"order\":4,\"has_content\":true},{\"pattern_id\":\"swift_associated_types_in_protocols\",\"title\":\"Associated types in protocols\",\"order\":5,\"has_content\":true},{\"pattern_id\":\"swift_where_clauses_for_complex_constraints\",\"title\":\"Where clauses for complex constraints\",\"order\":6,\"has_content\":true},{\"pattern_id\":\"swift_generic_subscripts\",\"title\":\"Generic subscripts\",\"order\":7,\"has_content\":true},{\"pattern_id\":\"swift_opaque_types_with_some_keyword\",\"title\":\"Opaque types with some keyword\",\"order\":8,\"has_content\":true}]}]},\"modeCode\":\"PMC\",\"productId\":\"codefly\"}]]\n"])</script><script>self.__next_f.push([1,"18:[[\"$\",\"meta\",\"0\",{\"name\":\"viewport\",\"content\":\"width=device-width, initial-scale=1\"}],[\"$\",\"meta\",\"1\",{\"charSet\":\"utf-8\"}],[\"$\",\"title\",\"2\",{\"children\":\"Methods in structs in Swift Mini Project - Build \u0026 Apply | Leyaa.ai\"}],[\"$\",\"meta\",\"3\",{\"name\":\"description\",\"content\":\"Apply Methods in structs in Swift in a hands-on mini project. Build something real with step-by-step guidance. Learn by doing, not just reading.\"}],[\"$\",\"meta\",\"4\",{\"name\":\"author\",\"content\":\"Leyaa.ai\"}],[\"$\",\"link\",\"5\",{\"rel\":\"manifest\",\"href\":\"/manifest.json\",\"crossOrigin\":\"use-credentials\"}],[\"$\",\"meta\",\"6\",{\"name\":\"keywords\",\"content\":\"learning intelligence,AI learning,personalized learning,adaptive learning,study guide,exam preparation,learning platform,education technology,edtech,smart learning\"}],[\"$\",\"meta\",\"7\",{\"name\":\"creator\",\"content\":\"Leyaa.ai\"}],[\"$\",\"meta\",\"8\",{\"name\":\"publisher\",\"content\":\"Leyaa.ai\"}],[\"$\",\"meta\",\"9\",{\"name\":\"robots\",\"content\":\"index, follow\"}],[\"$\",\"meta\",\"10\",{\"name\":\"googlebot\",\"content\":\"index, follow, max-image-preview:large, max-snippet:-1\"}],[\"$\",\"meta\",\"11\",{\"name\":\"mobile-web-app-capable\",\"content\":\"yes\"}],[\"$\",\"meta\",\"12\",{\"name\":\"content-language\",\"content\":\"en\"}],[\"$\",\"link\",\"13\",{\"rel\":\"canonical\",\"href\":\"https://leyaa.ai/codefly/learn/swift/part-2/swift-methods-in-structs/project\"}],[\"$\",\"link\",\"14\",{\"rel\":\"alternate\",\"hrefLang\":\"en\",\"href\":\"https://leyaa.ai/codefly/learn/swift/part-2/swift-methods-in-structs/project\"}],[\"$\",\"link\",\"15\",{\"rel\":\"alternate\",\"hrefLang\":\"x-default\",\"href\":\"https://leyaa.ai/codefly/learn/swift/part-2/swift-methods-in-structs/project\"}],[\"$\",\"meta\",\"16\",{\"name\":\"apple-mobile-web-app-capable\",\"content\":\"yes\"}],[\"$\",\"meta\",\"17\",{\"name\":\"apple-mobile-web-app-title\",\"content\":\"Leyaa\"}],[\"$\",\"meta\",\"18\",{\"name\":\"apple-mobile-web-app-status-bar-style\",\"content\":\"default\"}],[\"$\",\"meta\",\"19\",{\"property\":\"og:title\",\"content\":\"Methods in structs in Swift Mini Project - Build \u0026 Apply | Leyaa.ai\"}],[\"$\",\"meta\",\"20\",{\"property\":\"og:description\",\"content\":\"Apply Methods in structs in Swift in a hands-on mini project. Build something real with step-by-step guidance. Learn by doing, not just reading.\"}],[\"$\",\"meta\",\"21\",{\"property\":\"og:url\",\"content\":\"https://leyaa.ai/codefly/learn/swift/part-2/swift-methods-in-structs/project\"}],[\"$\",\"meta\",\"22\",{\"property\":\"og:locale\",\"content\":\"en_US\"}],[\"$\",\"meta\",\"23\",{\"property\":\"og:image\",\"content\":\"https://leyaa.ai/Assets/metaImages/leyaa-preview-image.png\"}],[\"$\",\"meta\",\"24\",{\"property\":\"og:image:width\",\"content\":\"1200\"}],[\"$\",\"meta\",\"25\",{\"property\":\"og:image:height\",\"content\":\"630\"}],[\"$\",\"meta\",\"26\",{\"property\":\"og:image:alt\",\"content\":\"Methods in structs in Swift Mini Project - Build \u0026 Apply\"}],[\"$\",\"meta\",\"27\",{\"property\":\"og:type\",\"content\":\"article\"}],[\"$\",\"meta\",\"28\",{\"name\":\"twitter:card\",\"content\":\"summary_large_image\"}],[\"$\",\"meta\",\"29\",{\"name\":\"twitter:site\",\"content\":\"@leyaaai\"}],[\"$\",\"meta\",\"30\",{\"name\":\"twitter:creator\",\"content\":\"@leyaaai\"}],[\"$\",\"meta\",\"31\",{\"name\":\"twitter:title\",\"content\":\"Methods in structs in Swift Mini Project - Build \u0026 Apply | Leyaa.ai\"}],[\"$\",\"meta\",\"32\",{\"name\":\"twitter:description\",\"content\":\"Apply Methods in structs in Swift in a hands-on mini project. Build something real with step-by-step guidance. Learn by doing, not just reading.\"}],[\"$\",\"meta\",\"33\",{\"name\":\"twitter:image\",\"content\":\"https://leyaa.ai/Assets/metaImages/leyaa-preview-image.png\"}],[\"$\",\"link\",\"34\",{\"rel\":\"icon\",\"href\":\"/leyaa-logo.png\"}],[\"$\",\"link\",\"35\",{\"rel\":\"apple-touch-icon\",\"href\":\"https://leyaa.ai/Assets/Favicons/apple-touch-icon.png\"}],[\"$\",\"meta\",\"36\",{\"name\":\"next-size-adjust\"}]]\n"])</script><script>self.__next_f.push([1,"a:null\n"])</script></body></html>