{"id":2714,"date":"2026-02-05T11:14:21","date_gmt":"2026-02-05T11:14:21","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2714"},"modified":"2026-02-05T11:14:21","modified_gmt":"2026-02-05T11:14:21","slug":"chapter-64-overview","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-64-overview\/","title":{"rendered":"Chapter 64: Overview"},"content":{"rendered":"<h3 dir=\"auto\">Overview<br \/>\n1. The Big Picture: Why Swift gives you three main collections<\/h3>\n<p dir=\"auto\">Swift gives you <strong>three primary collection types<\/strong> because almost every real program needs to solve one (or more) of these three problems:<\/p>\n<ol dir=\"auto\">\n<li><strong>I have an ordered list of things<\/strong> \u2014 and order matters, duplicates are okay \u2192 <strong>Array<\/strong>[Element]<\/li>\n<li><strong>I have a group of unique things<\/strong> \u2014 order doesn\u2019t matter, I just want fast \u201cdoes it contain X?\u201d \u2192 <strong>Set<\/strong>&lt;Element&gt;<\/li>\n<li><strong>I have items that I want to find quickly by a label\/key<\/strong> \u2014 like a phone book or settings dictionary \u2192 <strong>Dictionary<\/strong>[Key: Value]<\/li>\n<\/ol>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">You want\u2026<\/th>\n<th data-col-size=\"md\">Almost always use<\/th>\n<th data-col-size=\"sm\">Ordered?<\/th>\n<th data-col-size=\"lg\">Duplicates allowed?<\/th>\n<th data-col-size=\"lg\">Fast \u201ccontains X\u201d?<\/th>\n<th data-col-size=\"md\">Fast \u201cget value for key X\u201d?<\/th>\n<th data-col-size=\"xl\">Typical real-life examples<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">Ordered list, duplicates ok<\/td>\n<td data-col-size=\"md\"><strong>Array<\/strong><\/td>\n<td data-col-size=\"sm\">Yes<\/td>\n<td data-col-size=\"lg\">Yes<\/td>\n<td data-col-size=\"lg\">Slow (linear search)<\/td>\n<td data-col-size=\"md\">No<\/td>\n<td data-col-size=\"xl\">shopping cart, messages, tasks, search results, history<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Unique items, fast \u201cis X in the group?\u201d<\/td>\n<td data-col-size=\"md\"><strong>Set<\/strong><\/td>\n<td data-col-size=\"sm\">No<\/td>\n<td data-col-size=\"lg\">No<\/td>\n<td data-col-size=\"lg\">Very fast (hash)<\/td>\n<td data-col-size=\"md\">No<\/td>\n<td data-col-size=\"xl\">tags, permissions, unique IDs, friends (no duplicates)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Key \u2192 value lookup<\/td>\n<td data-col-size=\"md\"><strong>Dictionary<\/strong><\/td>\n<td data-col-size=\"sm\">No<\/td>\n<td data-col-size=\"lg\">Yes (different keys)<\/td>\n<td data-col-size=\"lg\">No<\/td>\n<td data-col-size=\"md\">Very fast (hash)<\/td>\n<td data-col-size=\"xl\">user profile, settings, JSON data, lookup tables<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Golden rule you should memorize forever:<\/strong><\/p>\n<blockquote dir=\"auto\">\n<ul dir=\"auto\">\n<li>Need <strong>order<\/strong> or <strong>duplicates<\/strong> \u2192 almost always <strong>Array<\/strong><\/li>\n<li>Need <strong>uniqueness<\/strong> + fast \u201ccontains?\u201d \u2192 <strong>Set<\/strong><\/li>\n<li>Need <strong>fast lookup by key<\/strong> \u2192 <strong>Dictionary<\/strong><\/li>\n<\/ul>\n<\/blockquote>\n<h3 dir=\"auto\">2. Arrays \u2013 the workhorse (you will use this 70\u201380% of the time)<\/h3>\n<h4 dir=\"auto\">Creating arrays<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Most common ways\r\nlet days = [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\"]\r\n\r\nvar cart: [String] = []                    \/\/ empty mutable array\r\n\r\nvar scores = [Int]()                       \/\/ another empty style\r\n\r\nlet repeated = Array(repeating: 0, count: 6)   \/\/ [0, 0, 0, 0, 0, 0]<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Basic real operations<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var playlist = [\"Shape of You\", \"Blinding Lights\"]\r\n\r\nplaylist.append(\"Levitating\")               \/\/ add at end\r\nplaylist.append(contentsOf: [\"Bad Habits\", \"As It Was\"])\r\n\r\nplaylist.insert(\"Flowers\", at: 1)           \/\/ insert at position\r\n\r\nplaylist[0] = \"Stay\"                        \/\/ replace\r\n\r\nlet removed = playlist.remove(at: 3)        \/\/ remove &amp; get value\r\nlet lastSong = playlist.removeLast()\r\n\r\nprint(playlist.count)                       \/\/ current size\r\nprint(playlist.isEmpty)                     \/\/ false\r\nprint(playlist.contains(\"Levitating\"))      \/\/ true<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Real-life example: Shopping cart summary<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>struct CartItem {\r\n    let name: String\r\n    let price: Double\r\n    let quantity: Int\r\n}\r\n\r\nvar cart: [CartItem] = []\r\n\r\ncart.append(CartItem(name: \"Wireless Earbuds\", price: 3499, quantity: 1))\r\ncart.append(CartItem(name: \"Phone Case\",       price: 799,  quantity: 2))\r\ncart.append(CartItem(name: \"Screen Protector\", price: 499,  quantity: 1))\r\n\r\nvar total: Double = 0\r\n\r\nprint(\"Your Cart (\\(cart.count) items)\")\r\nfor (index, item) in cart.enumerated() {\r\n    let lineTotal = item.price * Double(item.quantity)\r\n    total += lineTotal\r\n    \r\n    print(\"\\(index + 1). \\(item.name) \u00d7 \\(item.quantity) = \u20b9\\(String(format: \"%.2f\", lineTotal))\")\r\n}\r\n\r\nprint(\"\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\")\r\nprint(\"Total: \u20b9\\(String(format: \"%.2f\", total))\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">3. Sets \u2013 when uniqueness matters<\/h3>\n<h4 dir=\"auto\">Creating &amp; basic operations<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var activeFeatures = Set&lt;String&gt;()\r\n\r\nactiveFeatures.insert(\"darkMode\")\r\nactiveFeatures.insert(\"notifications\")\r\nactiveFeatures.insert(\"darkMode\")          \/\/ ignored \u2013 already present\r\n\r\nprint(activeFeatures)                      \/\/ Set([\"darkMode\", \"notifications\"])\r\n\r\nactiveFeatures.insert(\"offlineMode\")\r\n\r\nprint(activeFeatures.contains(\"offlineMode\"))   \/\/ true\r\nprint(activeFeatures.contains(\"analytics\"))     \/\/ false\r\n\r\n\/\/ Remove\r\nactiveFeatures.remove(\"notifications\")\r\n\r\n\/\/ Combine sets\r\nlet adminFeatures = Set([\"editUsers\", \"viewAnalytics\"])\r\nlet allFeatures = activeFeatures.union(adminFeatures)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Real-life example: User permissions \/ roles<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var currentUserPermissions = Set&lt;String&gt;()\r\n\r\ncurrentUserPermissions.insert(\"read\")\r\ncurrentUserPermissions.insert(\"write\")\r\ncurrentUserPermissions.insert(\"moderate\")\r\n\r\nif currentUserPermissions.contains(\"moderate\") {\r\n    print(\"You can moderate content\")\r\n}\r\n\r\nif currentUserPermissions.isSuperset(of: [\"read\", \"write\"]) {\r\n    print(\"You have read &amp; write access\")\r\n}\r\n\r\n\/\/ Remove permission\r\ncurrentUserPermissions.remove(\"moderate\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. Dictionaries \u2013 key \u2192 value lookup<\/h3>\n<h4 dir=\"auto\">Creating &amp; basic operations<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var userInfo: [String: Any] = [\r\n    \"name\": \"Rahul Verma\",\r\n    \"age\": 27,\r\n    \"city\": \"Bengaluru\",\r\n    \"isPremium\": true,\r\n    \"points\": 14520\r\n]\r\n\r\n\/\/ Read\r\nif let name = userInfo[\"name\"] as? String {\r\n    print(\"Name: \\(name)\")\r\n}\r\n\r\n\/\/ Update\r\nuserInfo[\"points\"] = 15000\r\n\r\n\/\/ Add\r\nuserInfo[\"favoriteColor\"] = \"Blue\"\r\n\r\n\/\/ Remove\r\nuserInfo[\"age\"] = nil\r\n\r\n\/\/ Loop\r\nfor (key, value) in userInfo {\r\n    print(\"\\(key): \\(value)\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Better style (strongly typed keys)<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>enum UserKey: String {\r\n    case name\r\n    case age\r\n    case city\r\n    case isPremium\r\n}\r\n\r\nvar typedUser: [UserKey: Any] = [\r\n    .name: \"Sneha Reddy\",\r\n    .age: 24,\r\n    .isPremium: true\r\n]<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Real-life example: Settings \/ config<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var appSettings: [String: Any] = [\r\n    \"theme\": \"dark\",\r\n    \"fontSize\": 16,\r\n    \"notificationsEnabled\": true,\r\n    \"language\": \"en-IN\",\r\n    \"locationSharing\": false\r\n]\r\n\r\n\/\/ Apply settings\r\nif let theme = appSettings[\"theme\"] as? String {\r\n    applyTheme(theme)\r\n}\r\n\r\nif let fontSize = appSettings[\"fontSize\"] as? CGFloat {\r\n    setFontSize(fontSize)\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">5. Quick Comparison Table (keep this in your mind)<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"xl\">You want to\u2026<\/th>\n<th data-col-size=\"lg\">Choose this<\/th>\n<th data-col-size=\"sm\">Ordered?<\/th>\n<th data-col-size=\"lg\">Duplicates?<\/th>\n<th data-col-size=\"md\">Fast \u201ccontains X\u201d?<\/th>\n<th data-col-size=\"md\">Fast \u201cget value for key X\u201d?<\/th>\n<th data-col-size=\"xl\">Typical real-life examples<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"xl\">Keep order, allow duplicates<\/td>\n<td data-col-size=\"lg\"><strong>Array<\/strong><\/td>\n<td data-col-size=\"sm\">Yes<\/td>\n<td data-col-size=\"lg\">Yes<\/td>\n<td data-col-size=\"md\">Slow<\/td>\n<td data-col-size=\"md\">No<\/td>\n<td data-col-size=\"xl\">cart, messages, tasks, history, search results<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xl\">Ensure uniqueness, fast \u201cis X here?\u201d<\/td>\n<td data-col-size=\"lg\"><strong>Set<\/strong><\/td>\n<td data-col-size=\"sm\">No<\/td>\n<td data-col-size=\"lg\">No<\/td>\n<td data-col-size=\"md\">Very fast<\/td>\n<td data-col-size=\"md\">No<\/td>\n<td data-col-size=\"xl\">tags, permissions, unique IDs, friends (no dupes)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xl\">Fast lookup by key<\/td>\n<td data-col-size=\"lg\"><strong>Dictionary<\/strong><\/td>\n<td data-col-size=\"sm\">No<\/td>\n<td data-col-size=\"lg\">Yes (keys unique)<\/td>\n<td data-col-size=\"md\">No<\/td>\n<td data-col-size=\"md\">Very fast<\/td>\n<td data-col-size=\"xl\">user profile, settings, JSON, config, lookup tables<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">6. Small Practice \u2013 Try these right now<\/h3>\n<ol dir=\"auto\">\n<li><strong>Array<\/strong> Create array of 6 favorite songs \u2192 Print them numbered \u2192 Add one more at the beginning \u2192 Remove the last one<\/li>\n<li><strong>Set<\/strong> Create set of 7 tags (some duplicates) \u2192 Add 2 more \u2192 Check if &#8220;Swift&#8221; is present \u2192 Print all unique tags<\/li>\n<li><strong>Dictionary<\/strong> Create dictionary for a user profile \u2192 &#8220;name&#8221;, &#8220;age&#8221;, &#8220;city&#8221;, &#8220;isPremium&#8221; \u2192 Print all key-value pairs \u2192 Change age and add &#8220;favoriteColor&#8221;<\/li>\n<\/ol>\n<p dir=\"auto\">Paste your code here if you want feedback or want to see more polished versions!<\/p>\n<p dir=\"auto\">What would you like to explore next?<\/p>\n<ul dir=\"auto\">\n<li><strong>Array slicing<\/strong> &amp; memory behavior with ArraySlice<\/li>\n<li><strong>Sorting<\/strong> arrays (simple &amp; custom comparators)<\/li>\n<li><strong>Filtering<\/strong>, <strong>mapping<\/strong>, <strong>reducing<\/strong> in depth<\/li>\n<li><strong>Dictionaries<\/strong> with typed keys &amp; default values<\/li>\n<li><strong>Sets<\/strong> operations (union, intersection, symmetric difference)<\/li>\n<li>Collections in <strong>SwiftUI<\/strong> (List, ForEach, @State)<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me \u2014 we\u2019ll continue in the same clear, patient, detailed style \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview 1. The Big Picture: Why Swift gives you three main collections Swift gives you three primary collection types because almost every real program needs to solve one (or more) of these three problems:&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2714","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/comments?post=2714"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2714\/revisions"}],"predecessor-version":[{"id":2715,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2714\/revisions\/2715"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}