0
0
Swiftprogramming~15 mins

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

Choose your learning style9 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="hint-toggle-btn"><span class="new-material-symbols icon-hw-24"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -960 960 960" fill="#d97706"><path d="M480-80q-26.67 0-47.33-12.83Q412-105.67 404-128h-9.33q-27 0-46.84-19.83Q328-167.67 328-194.67v-142.66Q264.33-379 227.17-445 190-511 190-590q0-121 84.5-205.5T480-880q121 0 205.5 84.5T770-590q0 79.67-37.17 145.33Q695.67-379 632-337.33v142.66q0 27-19.83 46.84Q592.33-128 565.33-128H556q-8 22.33-28.67 35.17Q506.67-80 480-80Zm-85.33-114.67h170.66V-236H394.67v41.33Zm0-81.33h170.66v-39.33H394.67V-276Zm110.66-106v-127.33L579-583q7.67-7.67 7.33-17.33-.33-9.67-8-17.34-7.66-7.66-17-7.66-9.33 0-17 7.66L480-553.33l-64.33-64.34q-7.67-7.66-17-7.33-9.34.33-17 8-7.67 7.67-7.67 17t7.67 17l73 73.67V-382h50.66Z"></path></svg></span>Need a 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="hint-toggle-btn"><span class="new-material-symbols icon-hw-24"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -960 960 960" fill="#d97706"><path d="M480-80q-26.67 0-47.33-12.83Q412-105.67 404-128h-9.33q-27 0-46.84-19.83Q328-167.67 328-194.67v-142.66Q264.33-379 227.17-445 190-511 190-590q0-121 84.5-205.5T480-880q121 0 205.5 84.5T770-590q0 79.67-37.17 145.33Q695.67-379 632-337.33v142.66q0 27-19.83 46.84Q592.33-128 565.33-128H556q-8 22.33-28.67 35.17Q506.67-80 480-80Zm-85.33-114.67h170.66V-236H394.67v41.33Zm0-81.33h170.66v-39.33H394.67V-276Zm110.66-106v-127.33L579-583q7.67-7.67 7.33-17.33-.33-9.67-8-17.34-7.66-7.66-17-7.66-9.33 0-17 7.66L480-553.33l-64.33-64.34q-7.67-7.66-17-7.33-9.34.33-17 8-7.67 7.67-7.67 17t7.67 17l73 73.67V-382h50.66Z"></path></svg></span>Need a 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="hint-toggle-btn"><span class="new-material-symbols icon-hw-24"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -960 960 960" fill="#d97706"><path d="M480-80q-26.67 0-47.33-12.83Q412-105.67 404-128h-9.33q-27 0-46.84-19.83Q328-167.67 328-194.67v-142.66Q264.33-379 227.17-445 190-511 190-590q0-121 84.5-205.5T480-880q121 0 205.5 84.5T770-590q0 79.67-37.17 145.33Q695.67-379 632-337.33v142.66q0 27-19.83 46.84Q592.33-128 565.33-128H556q-8 22.33-28.67 35.17Q506.67-80 480-80Zm-85.33-114.67h170.66V-236H394.67v41.33Zm0-81.33h170.66v-39.33H394.67V-276Zm110.66-106v-127.33L579-583q7.67-7.67 7.33-17.33-.33-9.67-8-17.34-7.66-7.66-17-7.66-9.33 0-17 7.66L480-553.33l-64.33-64.34q-7.67-7.66-17-7.33-9.34.33-17 8-7.67 7.67-7.67 17t7.67 17l73 73.67V-382h50.66Z"></path></svg></span>Need a 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="hint-toggle-btn"><span class="new-material-symbols icon-hw-24"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -960 960 960" fill="#d97706"><path d="M480-80q-26.67 0-47.33-12.83Q412-105.67 404-128h-9.33q-27 0-46.84-19.83Q328-167.67 328-194.67v-142.66Q264.33-379 227.17-445 190-511 190-590q0-121 84.5-205.5T480-880q121 0 205.5 84.5T770-590q0 79.67-37.17 145.33Q695.67-379 632-337.33v142.66q0 27-19.83 46.84Q592.33-128 565.33-128H556q-8 22.33-28.67 35.17Q506.67-80 480-80Zm-85.33-114.67h170.66V-236H394.67v41.33Zm0-81.33h170.66v-39.33H394.67V-276Zm110.66-106v-127.33L579-583q7.67-7.67 7.33-17.33-.33-9.67-8-17.34-7.66-7.66-17-7.66-9.33 0-17 7.66L480-553.33l-64.33-64.34q-7.67-7.66-17-7.33-9.34.33-17 8-7.67 7.67-7.67 17t7.67 17l73 73.67V-382h50.66Z"></path></svg></span>Need a hint?</div><div class="hint-content"><p>Use <code>print(myBook.description())</code> to show the description.</p></div></div></div></article></div></div></main><div style="position:fixed;bottom:24px;right:24px;z-index:50"><button style="background:rgba(255,255,255,0.95);border:1px solid rgba(108,99,255,0.18);border-radius:10px;padding:10px 16px;color:#5f56fe;font-size:14px;cursor:pointer;display:flex;align-items:center;gap:7px;backdrop-filter:blur(12px);box-shadow:0 2px 12px rgba(0,0,0,0.08), 0 0 0 1px rgba(108,99,255,0.06);transition:all 0.2s"><span style="font-size:14px">⚑</span>Report Issue</button></div></div> <script src="/_next/static/chunks/webpack-fd24bd8e19d2841a.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/b133bdac07c7940e.css\",\"style\"]\n2:HL[\"/_next/static/css/837a603cb1a59856.css\",\"style\"]\n3:HL[\"/_next/static/css/725c7861d1898ba8.css\",\"style\"]\n4:HL[\"/_next/static/css/caf3ca742c7945f9.css\",\"style\"]\n"])</script><script>self.__next_f.push([1,"5:I[95751,[],\"\"]\n8:I[39275,[],\"\"]\ne:I[61343,[],\"\"]\nf:I[84080,[\"8726\",\"static/chunks/8726-583188341cbc1496.js\",\"3185\",\"static/chunks/app/layout-7a1373330f6a4c98.js\"],\"\"]\n10:I[88726,[\"8726\",\"static/chunks/8726-583188341cbc1496.js\",\"3185\",\"static/chunks/app/layout-7a1373330f6a4c98.js\"],\"Toaster\"]\n11:I[20154,[\"8422\",\"static/chunks/66ec4792-a0fc378024be0c7b.js\",\"6648\",\"static/chunks/6648-fff0cf0e0a1f8d25.js\",\"9160\",\"static/chunks/app/not-found-c4181ddc3e64e5f3.js\"],\"default\"]\n12:I[70548,[\"8726\",\"static/chunks/8726-583188341cbc1496.js\",\"3185\",\"static/chunks/app/layout-7a1373330f6a4c98.js\"],\"default\"]\n14:I[76130,[],\"\"]\n9:[\"lang\",\"en\",\"d\"]\na:[\"subject\",\"swift\",\"d\"]\nb:[\"part\",\"part-2\",\"d\"]\nc:[\"pattern\",\"swift-methods-in-structs\",\"d\"]\nd:[\"mode\",\"project\",\"oc\"]\n15:[]\n"])</script><script>self.__next_f.push([1,"0:[[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/b133bdac07c7940e.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}]],[\"$\",\"$L5\",null,{\"buildId\":\"hN8t5By7h5nzsrdSose07\",\"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__\",{},[[\"$L6\",\"$L7\"],null],null]},[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$9\",\"children\",\"codefly\",\"children\",\"learn\",\"children\",\"$a\",\"children\",\"$b\",\"children\",\"$c\",\"children\",\"$d\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$Le\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/837a603cb1a59856.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/caf3ca742c7945f9.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}]]}],null]},[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$9\",\"children\",\"codefly\",\"children\",\"learn\",\"children\",\"$a\",\"children\",\"$b\",\"children\",\"$c\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$Le\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":null}],null]},[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$9\",\"children\",\"codefly\",\"children\",\"learn\",\"children\",\"$a\",\"children\",\"$b\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$Le\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":null}],null]},[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$9\",\"children\",\"codefly\",\"children\",\"learn\",\"children\",\"$a\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$Le\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":null}],null]},[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$9\",\"children\",\"codefly\",\"children\",\"learn\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$Le\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":null}],null]},[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$9\",\"children\",\"codefly\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$Le\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\",\"styles\":null}],null]},[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"$9\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$Le\",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\"}],[\"$\",\"$Lf\",null,{\"src\":\"https://www.googletagmanager.com/gtag/js?id=G-N2NY2DMMDW\",\"strategy\":\"afterInteractive\"}],[\"$\",\"$Lf\",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-17928224938\"}],[\"$\",\"$Lf\",null,{\"children\":\"\\n window.dataLayer = window.dataLayer || [];\\n function gtag() {\\n dataLayer.push(arguments);\\n }\\n gtag('js', new Date());\\n gtag('config', 'AW-17928224938');\\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);})();\"}}],[\"$\",\"$Lf\",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\\\"}}\"}}],[\"$\",\"link\",null,{\"href\":\"https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css\",\"rel\":\"stylesheet\",\"integrity\":\"sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB\",\"crossOrigin\":\"anonymous\"}],[\"$\",\"$Lf\",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\":[[\"$\",\"$L10\",null,{\"containerStyle\":{\"top\":70}}],[\"$\",\"div\",null,{\"className\":\"bg-grid\"}],[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$Le\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":[\"$\",\"$L11\",null,{}],\"notFoundStyles\":[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/_next/static/css/250d3fff07338fa3.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}]],\"styles\":null}],[\"$\",\"$L12\",null,{}],\" \"]}]]}],null],null],\"couldBeIntercepted\":false,\"initialHead\":[false,\"$L13\"],\"globalErrorComponent\":\"$14\",\"missingSlots\":\"$W15\"}]]\n"])</script><script>self.__next_f.push([1,"13:[[\"$\",\"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\":\"content-language\",\"content\":\"en\"}],[\"$\",\"link\",\"12\",{\"rel\":\"canonical\",\"href\":\"https://leyaa.ai/codefly/learn/swift/part-2/swift-methods-in-structs/project\"}],[\"$\",\"link\",\"13\",{\"rel\":\"alternate\",\"hrefLang\":\"en\",\"href\":\"https://leyaa.ai/codefly/learn/swift/part-2/swift-methods-in-structs/project\"}],[\"$\",\"link\",\"14\",{\"rel\":\"alternate\",\"hrefLang\":\"x-default\",\"href\":\"https://leyaa.ai/codefly/learn/swift/part-2/swift-methods-in-structs/project\"}],[\"$\",\"meta\",\"15\",{\"property\":\"og:title\",\"content\":\"Methods in structs in Swift Mini Project - Build \u0026 Apply | Leyaa.ai\"}],[\"$\",\"meta\",\"16\",{\"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\",\"17\",{\"property\":\"og:url\",\"content\":\"https://leyaa.ai/codefly/learn/swift/part-2/swift-methods-in-structs/project\"}],[\"$\",\"meta\",\"18\",{\"property\":\"og:locale\",\"content\":\"en_US\"}],[\"$\",\"meta\",\"19\",{\"property\":\"og:image\",\"content\":\"https://leyaa.ai/Assets/metaImages/leyaa-preview-image.png\"}],[\"$\",\"meta\",\"20\",{\"property\":\"og:image:width\",\"content\":\"1200\"}],[\"$\",\"meta\",\"21\",{\"property\":\"og:image:height\",\"content\":\"630\"}],[\"$\",\"meta\",\"22\",{\"property\":\"og:image:alt\",\"content\":\"Methods in structs in Swift Mini Project - Build \u0026 Apply\"}],[\"$\",\"meta\",\"23\",{\"property\":\"og:type\",\"content\":\"article\"}],[\"$\",\"meta\",\"24\",{\"name\":\"twitter:card\",\"content\":\"summary_large_image\"}],[\"$\",\"meta\",\"25\",{\"name\":\"twitter:site\",\"content\":\"@leyaaai\"}],[\"$\",\"meta\",\"26\",{\"name\":\"twitter:creator\",\"content\":\"@leyaaai\"}],[\"$\",\"meta\",\"27\",{\"name\":\"twitter:title\",\"content\":\"Methods in structs in Swift Mini Project - Build \u0026 Apply | Leyaa.ai\"}],[\"$\",\"meta\",\"28\",{\"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\",\"29\",{\"name\":\"twitter:image\",\"content\":\"https://leyaa.ai/Assets/metaImages/leyaa-preview-image.png\"}],[\"$\",\"link\",\"30\",{\"rel\":\"icon\",\"href\":\"/leyaa-logo.png\"}],[\"$\",\"link\",\"31\",{\"rel\":\"apple-touch-icon\",\"href\":\"/leyaa-logo.png\"}],[\"$\",\"meta\",\"32\",{\"name\":\"next-size-adjust\"}]]\n"])</script><script>self.__next_f.push([1,"6:null\n"])</script><script>self.__next_f.push([1,"17:I[51766,[\"8422\",\"static/chunks/66ec4792-a0fc378024be0c7b.js\",\"522\",\"static/chunks/94730671-fd9628eddbd5107b.js\",\"7240\",\"static/chunks/53c13509-506edbde2b5b3f55.js\",\"7699\",\"static/chunks/8e1d74a4-a085c2fbc868135a.js\",\"5706\",\"static/chunks/9c4e2130-11ecd4bfc78e4568.js\",\"1779\",\"static/chunks/0e762574-6b3bda54d2fd2e14.js\",\"6648\",\"static/chunks/6648-fff0cf0e0a1f8d25.js\",\"3463\",\"static/chunks/3463-09ee572e3d7819a2.js\",\"4889\",\"static/chunks/4889-956a916919971629.js\",\"9985\",\"static/chunks/9985-b39235669d2563e2.js\",\"7627\",\"static/chunks/7627-224bb765a4decf1d.js\",\"7652\",\"static/chunks/7652-412e201fe52797ee.js\",\"8935\",\"static/chunks/8935-c1c159349bf7da40.js\",\"9663\",\"static/chunks/9663-fdcd080af3916e3e.js\",\"7029\",\"static/chunks/app/%5Blang%5D/codefly/learn/%5Bsubject%5D/%5Bpart%5D/%5Bpattern%5D/%5B%5B...mode%5D%5D/page-1fc9c577a9450e06.js\"],\"default\"]\n16: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://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,"7:[[[\"$\",\"script\",\"0\",{\"type\":\"application/ld+json\",\"dangerouslySetInnerHTML\":{\"__html\":\"$16\"}}]],[\"$\",\"$L17\",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\"},\"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></body></html>