{"id":2575,"date":"2026-02-02T11:36:48","date_gmt":"2026-02-02T11:36:48","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2575"},"modified":"2026-02-02T11:46:04","modified_gmt":"2026-02-02T11:46:04","slug":"swift-tutorial","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/swift-tutorial\/","title":{"rendered":"Swift Tutorial"},"content":{"rendered":"<h3 dir=\"auto\">Swift Tutorial \u2013 Like a Real Teacher Would Explain It (2026 style)<\/h3>\n<h4 dir=\"auto\">Lesson 1 \u2013 Getting Started \u2013 Where do I even write code?<\/h4>\n<p dir=\"auto\">Three realistic choices right now:<\/p>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"md\">Option<\/th>\n<th data-col-size=\"md\">Do you need a Mac?<\/th>\n<th data-col-size=\"sm\">Best for beginners?<\/th>\n<th data-col-size=\"md\">Speed to see first result<\/th>\n<th data-col-size=\"lg\">My recommendation<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\">Swift Playgrounds (app)<\/td>\n<td data-col-size=\"md\">Yes (Mac or iPad)<\/td>\n<td data-col-size=\"sm\">\u2605\u2605\u2605\u2605\u2605<\/td>\n<td data-col-size=\"md\">20 seconds<\/td>\n<td data-col-size=\"lg\">Best starting point<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Online playground<\/td>\n<td data-col-size=\"md\">No<\/td>\n<td data-col-size=\"sm\">\u2605\u2605\u2605\u2605<\/td>\n<td data-col-size=\"md\">10 seconds<\/td>\n<td data-col-size=\"lg\">Very good if no Mac<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Xcode<\/td>\n<td data-col-size=\"md\">Yes<\/td>\n<td data-col-size=\"sm\">\u2605\u2605\u2605<\/td>\n<td data-col-size=\"md\">2\u20135 minutes<\/td>\n<td data-col-size=\"lg\">After 1\u20132 weeks<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">VS Code + Swift<\/td>\n<td data-col-size=\"md\">Any computer<\/td>\n<td data-col-size=\"sm\">\u2605\u2605<\/td>\n<td data-col-size=\"md\">10\u201330 minutes setup<\/td>\n<td data-col-size=\"lg\">Later (scripting)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Today I recommend<\/strong>: Open <strong>Swift Playgrounds<\/strong> on Mac\/iPad OR go to https:\/\/swiftfiddle.com (online, no installation)<\/p>\n<p dir=\"auto\">Just press the <strong>+<\/strong> button \u2192 choose <strong>Blank<\/strong> playground \u2192 and you\u2019re ready.<\/p>\n<p dir=\"auto\">Let\u2019s write our first line.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>print(\"Hello friend!\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Run it (\u25b6\ufe0f button or Cmd+R).<\/p>\n<p dir=\"auto\">You should see:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>text<\/div>\n<div>\n<pre tabindex=\"0\"><code>Hello friend!<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">That\u2019s your first program. \ud83c\udf89<\/p>\n<h3 dir=\"auto\">Lesson 2 \u2013 Variables and Constants (the most important first concept)<\/h3>\n<p dir=\"auto\">Think of variables like <strong>labeled boxes<\/strong>.<\/p>\n<p dir=\"auto\">You can put something in the box, and later change what\u2019s inside.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var age = 22          \/\/ I can change this later\r\nage = 23              \/\/ ok\r\nage = age + 1         \/\/ now 24<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">But sometimes you want a value that <strong>should never change<\/strong> \u2014 like your birth year, your username, etc.<\/p>\n<p dir=\"auto\">That\u2019s what let is for:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let birthYear = 2001\r\n\/\/ birthYear = 2002     \u2190 this will NOT compile \u2192 good!<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Rule most beginners forget<\/strong>:<\/p>\n<ul dir=\"auto\">\n<li>Use let<strong>by default<\/strong><\/li>\n<li>Use var<strong>only when you know you will change the value<\/strong><\/li>\n<\/ul>\n<p dir=\"auto\">Bad habit example (very common):<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var name = \"Sara\"       \/\/ \u2190 should be let !\r\nname = \"Sara Smith\"     \/\/ actually never changed again<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Better:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let name = \"Sara Smith\"<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Lesson 3 \u2013 Basic Types (what kinds of things can live in boxes)<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"md\">Type<\/th>\n<th data-col-size=\"lg\">Example values<\/th>\n<th data-col-size=\"xl\">Real-life analogy<\/th>\n<th data-col-size=\"md\">Most common?<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\">Int<\/td>\n<td data-col-size=\"lg\">0, -5, 42, 1000000<\/td>\n<td data-col-size=\"xl\">Number of apples, age, ID<\/td>\n<td data-col-size=\"md\">Yes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Double<\/td>\n<td data-col-size=\"lg\">3.14, -0.001, 99.99<\/td>\n<td data-col-size=\"xl\">Money, height, temperature<\/td>\n<td data-col-size=\"md\">Yes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Bool<\/td>\n<td data-col-size=\"lg\">true, false<\/td>\n<td data-col-size=\"xl\">Light switch (on\/off)<\/td>\n<td data-col-size=\"md\">Yes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">String<\/td>\n<td data-col-size=\"lg\">&#8220;hello&#8221;, &#8220;\ud83d\ude3a&#8221;, &#8220;&#8221;<\/td>\n<td data-col-size=\"xl\">Text, names, messages<\/td>\n<td data-col-size=\"md\">Very yes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Character<\/td>\n<td data-col-size=\"lg\">&#8220;A&#8221;, &#8220;\ud83d\ude80&#8221;, &#8221; &#8220;<\/td>\n<td data-col-size=\"xl\">Single letter\/symbol<\/td>\n<td data-col-size=\"md\">Rarely<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Examples:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let age             = 27             \/\/ Int\r\nlet height          = 1.76           \/\/ Double\r\nlet isStudent       = true           \/\/ Bool\r\nlet username        = \"dark_knight42\" \/\/ String\r\nlet favoriteEmoji   : Character = \"\u26a1\" \/\/ rare but ok<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Swift usually <strong>guesses the type<\/strong> (type inference) \u2014 this is good!<\/p>\n<p dir=\"auto\">But sometimes you want to be very clear:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let score: Int = 100\r\nlet price: Double = 19.99<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Lesson 4 \u2013 Strings \u2013 You will use them every day<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let first = \"Emma\"\r\nlet last  = \"Wilson\"\r\n\r\n\/\/ Classic way (old-school)\r\nlet full = first + \" \" + last           \/\/ \"Emma Wilson\"\r\n\r\n\/\/ Modern &amp; best way (99% of the time)\r\nlet full2 = \"\\(first) \\(last)\"          \/\/ \"Emma Wilson\"\r\n\r\n\/\/ Very long text (multi-line)\r\nlet message = \"\"\"\r\nHello!\r\nI hope you're having a great day.\r\nWrite me back soon \ud83d\ude0a\r\n\"\"\"<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Useful things you do with strings all the time:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let text = \"Swift is actually quite nice!\"\r\n\r\nprint(text.count)               \/\/ 31\r\nprint(text.uppercased())        \/\/ SWIFT IS ACTUALLY QUITE NICE!\r\nprint(text.lowercased())        \/\/ swift is actually quite nice!\r\nprint(text.hasPrefix(\"Swift\"))  \/\/ true\r\nprint(text.hasSuffix(\"nice!\"))  \/\/ true\r\nprint(text.contains(\"quite\"))   \/\/ true<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Lesson 5 \u2013 Doing decisions \u2013 if and switch<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let temperature = 28\r\n\r\nif temperature &gt;= 30 {\r\n    print(\"It's really hot \ud83e\udd75\")\r\n} else if temperature &gt;= 22 {\r\n    print(\"Nice weather \ud83d\ude0e\")\r\n} else if temperature &gt;= 10 {\r\n    print(\"A bit chilly \ud83e\udde5\")\r\n} else {\r\n    print(\"Freezing! \ud83e\udd76\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Very important tip<\/strong>: Put the <strong>most common case first<\/strong> \u2014 code is easier to read.<\/p>\n<p dir=\"auto\">Now \u2014 switch (very loved in Swift):<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let grade = \"A\"\r\n\r\nswitch grade {\r\ncase \"A\", \"A+\":\r\n    print(\"Excellent! \ud83c\udf89\")\r\ncase \"B\":\r\n    print(\"Good job\")\r\ncase \"C\":\r\n    print(\"You passed\")\r\ncase \"D\", \"F\":\r\n    print(\"We need to talk...\")\r\ndefault:\r\n    print(\"Invalid grade \ud83e\udd14\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Modern pattern you will see a lot<\/strong>:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let status = 404\r\n\r\nswitch status {\r\ncase 200..&lt;300:\r\n    print(\"Everything ok\")\r\ncase 400..&lt;500:\r\n    print(\"You did something wrong\")\r\ncase 500...:\r\n    print(\"Server is crying\")\r\ndefault:\r\n    print(\"Weird status code\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Lesson 6 \u2013 Lists of things \u2192 Arrays<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Creating\r\nvar friends = [\"Mia\", \"Leo\", \"Zoe\", \"Noah\"]\r\n\r\n\/\/ Reading\r\nprint(friends[0])     \/\/ Mia\r\nprint(friends[2])     \/\/ Zoe\r\n\r\n\/\/ Adding\r\nfriends.append(\"Ava\")           \/\/ at the end\r\nfriends.insert(\"Liam\", at: 0)   \/\/ at the beginning\r\n\r\n\/\/ Removing\r\nfriends.remove(at: 1)           \/\/ removes Leo\r\nfriends.removeLast()            \/\/ removes last person\r\n\r\n\/\/ Checking\r\nif friends.contains(\"Zoe\") {\r\n    print(\"Zoe is still here!\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Looping over array (three common ways):<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Way 1 \u2013 simplest\r\nfor friend in friends {\r\n    print(\"Hello \\(friend)!\")\r\n}\r\n\r\n\/\/ Way 2 \u2013 with number\r\nfor (index, friend) in friends.enumerated() {\r\n    print(\"\\(index+1). \\(friend)\")\r\n}\r\n\r\n\/\/ Way 3 \u2013 only when you need index\r\nfor i in 0..&lt;friends.count {\r\n    print(friends[i])\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Lesson 7 \u2013 Optionals \u2013 The part that confuses everyone at first<\/h3>\n<p dir=\"auto\"><strong>Real-life analogy<\/strong>:<\/p>\n<p dir=\"auto\">Imagine you ask: \u201cDo you have a nickname?\u201d<\/p>\n<ul dir=\"auto\">\n<li>Some people say \u201cyes, it\u2019s Sunny\u201d \u2192 has value<\/li>\n<li>Some people say \u201cno\u201d \u2192 nil (no nickname)<\/li>\n<\/ul>\n<p dir=\"auto\">In Swift we write:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>var nickname: String? = \"Sunny\"\r\n\r\n\/\/ Later...\r\nnickname = nil   \/\/ now they have no nickname<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>You cannot just use it directly!<\/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>\/\/ This CRASHES if nickname is nil\r\nprint(nickname.count)     \/\/ \u2190 dangerous!<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Safe ways (choose these):<\/p>\n<p dir=\"auto\"><strong>Way 1 \u2013 if let<\/strong> (most common)<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>if let nick = nickname {\r\n    print(\"Your nickname is \\(nick)\")\r\n} else {\r\n    print(\"No nickname set\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Modern short version (very popular now):<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>if let nickname {\r\n    print(\"Hi \\(nickname)!\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Way 2 \u2013 default value<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let display = nickname ?? \"Friend\"\r\nprint(\"Hello \\(display)\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Way 3 \u2013 only when 100% sure<\/strong> (dangerous \u2013 use rarely)<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let forced = nickname!     \/\/ crashes if nil<\/code><\/pre>\n<\/div>\n<\/div>\n<div>Lesson 8 \u2013 Functions \u2013 Reusing code<\/div>\n<\/div>\n<\/div>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>func sayHello(to name: String) {\r\n    print(\"Hey \\(name), how are you?\")\r\n}\r\n\r\nsayHello(to: \"Julia\")\r\nsayHello(to: \"Mateo\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Functions that return something:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>func square(of number: Int) -&gt; Int {\r\n    return number * number\r\n}\r\n\r\nlet result = square(of: 8)      \/\/ 64<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Default values:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>func greet(name: String, politely: Bool = true) {\r\n    if politely {\r\n        print(\"Dear \\(name), welcome!\")\r\n    } else {\r\n        print(\"Yo \\(name)!\")\r\n    }\r\n}\r\n\r\ngreet(name: \"Sara\")               \/\/ polite version\r\ngreet(name: \"Max\", politely: false)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Where are we now?<\/h3>\n<p dir=\"auto\">You already understand ~70% of lines in most beginner\/intermediate Swift code!<\/p>\n<p dir=\"auto\">Checklist:<\/p>\n<ul dir=\"auto\">\n<li>let vs var<\/li>\n<li>Basic types (Int, Double, Bool, String)<\/li>\n<li>String interpolation + methods<\/li>\n<li>if, else, switch<\/li>\n<li>Arrays + looping<\/li>\n<li>Optionals (?, ??, if let)<\/li>\n<li>Functions<\/li>\n<\/ul>\n<p dir=\"auto\">Next steps \u2013 pick one path:<\/p>\n<ol dir=\"auto\">\n<li><strong>SwiftUI<\/strong> \u2192 make real iPhone\/iPad apps (most popular)<\/li>\n<li><strong>Enums + Structs + Protocols<\/strong> \u2192 better organize data<\/li>\n<li><strong>Closures<\/strong> \u2192 very important modern Swift feature<\/li>\n<li><strong>async\/await<\/strong> \u2192 modern way to do network, files, delays<\/li>\n<li><strong>Small project<\/strong> \u2192 Todo list, quiz game, unit converter&#8230;<\/li>\n<\/ol>\n<p dir=\"auto\">Which direction would you like to continue with?<\/p>\n<p dir=\"auto\">Tell me what feels most interesting to you right now \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swift Tutorial \u2013 Like a Real Teacher Would Explain It (2026 style) Lesson 1 \u2013 Getting Started \u2013 Where do I even write code? Three realistic choices right now: Option Do you need a&#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-2575","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2575","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=2575"}],"version-history":[{"count":2,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2575\/revisions"}],"predecessor-version":[{"id":2580,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2575\/revisions\/2580"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}