{"id":2616,"date":"2026-02-05T07:40:15","date_gmt":"2026-02-05T07:40:15","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2616"},"modified":"2026-02-05T07:40:15","modified_gmt":"2026-02-05T07:40:15","slug":"chapter-17-data-types","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-17-data-types\/","title":{"rendered":"Chapter 17: Data Types"},"content":{"rendered":"<h3 dir=\"auto\">1. What is a data type? (very simple explanation)<\/h3>\n<p dir=\"auto\">A data type tells the computer:<\/p>\n<ul dir=\"auto\">\n<li><strong>What kind of value<\/strong> this is (number, text, true\/false, list, \u2026)<\/li>\n<li><strong>How much memory<\/strong> it needs<\/li>\n<li><strong>What operations<\/strong> are allowed on it (+, ==, .count, \u2026)<\/li>\n<\/ul>\n<p dir=\"auto\">Swift is a <strong>strongly typed<\/strong> language \u2014 it checks types very carefully (much stricter than Python or JavaScript).<\/p>\n<h3 dir=\"auto\">2. Two big families of types in Swift<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"sm\">Family<\/th>\n<th data-col-size=\"xl\">Examples<\/th>\n<th data-col-size=\"lg\">When you copy it, what happens?<\/th>\n<th data-col-size=\"lg\">Main feeling \/ usage<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"sm\"><strong>Value types<\/strong><\/td>\n<td data-col-size=\"xl\">Int, Double, Bool, String, Array, Dictionary, Set, struct, enum<\/td>\n<td data-col-size=\"lg\">A <strong>copy<\/strong> is made (independent)<\/td>\n<td data-col-size=\"lg\">Most everyday types \u2014 safe &amp; predictable<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"sm\"><strong>Reference types<\/strong><\/td>\n<td data-col-size=\"xl\">class, objects that inherit from NSObject, some closures<\/td>\n<td data-col-size=\"lg\">Only a <strong>reference<\/strong> is copied (shared)<\/td>\n<td data-col-size=\"lg\">Used for shared state (like view controllers)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Beginner rule (very important):<\/strong><\/p>\n<blockquote dir=\"auto\">\n<p dir=\"auto\">In 95% of beginner and intermediate code you will mostly meet <strong>value types<\/strong> You will see class later (UIKit, SwiftUI view models, networking models sometimes)<\/p>\n<\/blockquote>\n<h3 dir=\"auto\">3. The most important built-in data types (with examples)<\/h3>\n<p dir=\"auto\">Let\u2019s go through them one by one \u2014 with code you can run right now.<\/p>\n<h4 dir=\"auto\">3.1 Integer types (Int, UInt, Int64, \u2026)<\/h4>\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                 = 25                    \/\/ Int (most common)\r\nlet likes               = 1_245_678             \/\/ underscore makes big numbers readable\r\nlet negativeBalance     = -150\r\nlet veryBigNumber       = 9_223_372_036_854_775_807     \/\/ Int.max on 64-bit\r\n\r\nlet smallCounter: UInt8 = 255                   \/\/ only 0...255\r\nlet fileSizeBytes: Int64 = 8_589_934_592        \/\/ when you need exactly 64-bit<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Quick table \u2013 integer types you might meet<\/strong><\/p>\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\">Range (approx)<\/th>\n<th data-col-size=\"sm\">Signed?<\/th>\n<th data-col-size=\"lg\">Typical use case<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\">Int<\/td>\n<td data-col-size=\"lg\">-9 quintillion \u2026 +9 quintillion<\/td>\n<td data-col-size=\"sm\">Yes<\/td>\n<td data-col-size=\"lg\">Normal counting, IDs, indices (default)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">UInt<\/td>\n<td data-col-size=\"lg\">0 \u2026 18 quintillion<\/td>\n<td data-col-size=\"sm\">No<\/td>\n<td data-col-size=\"lg\">Rarely \u2014 when you really want non-negative<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Int64<\/td>\n<td data-col-size=\"lg\">-9 quintillion \u2026 +9 quintillion<\/td>\n<td data-col-size=\"sm\">Yes<\/td>\n<td data-col-size=\"lg\">Large file sizes, timestamps<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">UInt8<\/td>\n<td data-col-size=\"lg\">0 \u2026 255<\/td>\n<td data-col-size=\"sm\">No<\/td>\n<td data-col-size=\"lg\">Colors (RGB), small counters<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">3.2 Floating-point types (Double vs Float)<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let height              = 1.72                  \/\/ Double \u2013 most common choice\r\nlet temperature         = 36.6\r\nlet pi                  = 3.141592653589793\r\nlet verySmallNumber     = 1.23e-6               \/\/ scientific notation\r\n\r\nlet opacity: Float      = 0.75                  \/\/ 32-bit float \u2013 less precise<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Very important rule most beginners miss:<\/strong><\/p>\n<blockquote dir=\"auto\">\n<p dir=\"auto\">Use <strong>Double<\/strong> for almost everything Use <strong>Float<\/strong> only when:<\/p>\n<ul dir=\"auto\">\n<li>You need to save memory (very rare)<\/li>\n<li>You are working with old APIs \/ graphics that expect Float<\/li>\n<\/ul>\n<\/blockquote>\n<h4 dir=\"auto\">3.3 Money \u2013 special case: Decimal (not Double!)<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>import Foundation\r\n\r\nlet itemPrice: Decimal  = 1499.99\r\nlet quantity: Int       = 3\r\nlet taxRate: Decimal    = 0.18\r\n\r\nlet subtotal            = itemPrice * Decimal(quantity)\r\nlet tax                 = subtotal * taxRate\r\nlet total               = subtotal + tax\r\n\r\nprint(total)            \/\/ 5311.9702<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Why not Double for money?<\/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 bad = 0.1 + 0.2     \/\/ 0.30000000000000004   \u2190 floating point error!\r\nlet good: Decimal = 0.1 + 0.2   \/\/ exactly 0.3<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Rule:<\/strong> Double \u2192 measurements, coordinates, animations Decimal \u2192 money, prices, financial calculations<\/p>\n<h4 dir=\"auto\">3.4 Boolean (Bool)<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let isLoggedIn          = true\r\nlet hasPremium          = false\r\nlet isRaining           = false\r\nlet canSubmitForm       = emailIsValid &amp;&amp; passwordMeetsRequirements<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Very often used in conditions:<\/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 isLoggedIn {\r\n    showDashboard()\r\n} else {\r\n    showLoginScreen()\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">3.5 Text (String \u2013 very important!)<\/h4>\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                = \"Rahul Verma\"\r\nlet greeting            = \"\u0c28\u0c2e\u0c38\u0c4d\u0c24\u0c47 Hyderabad! \ud83c\udf36\ufe0f\"\r\nlet multiline = \"\"\"\r\n    First line\r\n    Second line\r\n    Third line with emoji \ud83d\ude3a\r\n    \"\"\"\r\n\r\nlet empty               = \"\"\r\nlet singleCharString    = \"A\"               \/\/ still String, not Character<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Useful things you do every day:<\/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(name.count)                   \/\/ 11\r\nprint(name.uppercased())            \/\/ RAHUL VERMA\r\nprint(name.hasPrefix(\"Rah\"))        \/\/ true\r\nprint(name.contains(\"ul\"))          \/\/ true\r\nprint(greeting.contains(\"\ud83c\udf36\ufe0f\"))     \/\/ true<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">3.6 Single character (Character) \u2013 rare<\/h4>\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: Character        = \"A\"\r\nlet currency: Character     = \"\u20b9\"\r\nlet smile: Character        = \"\ud83d\ude0a\"\r\n\r\n\/\/ This is NOT a Character \u2014 it's a String!\r\nlet wrong = \"A\"             \/\/ String with one character<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Rule of thumb:<\/strong> You almost never need Character \u2014 use String even for single letters\/symbols.<\/p>\n<h3 dir=\"auto\">4. Quick real-life examples \u2013 how types appear in apps<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>struct Product {\r\n    let id: String                  \/\/ or UUID\r\n    let name: String\r\n    let price: Decimal              \/\/ money!\r\n    let rating: Double              \/\/ 4.78\r\n    let reviewCount: Int\r\n    let inStock: Bool\r\n    let tags: [String]              \/\/ we'll talk about Array later\r\n}\r\n\r\nlet iphone = Product(\r\n    id: \"MNH63HN\/A\",\r\n    name: \"iPhone 16 Pro\",\r\n    price: 119900,\r\n    rating: 4.85,\r\n    reviewCount: 12450,\r\n    inStock: true,\r\n    tags: [\"smartphone\", \"apple\", \"5G\"]\r\n)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">5. Very common beginner mistakes<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">Mistake<\/th>\n<th data-col-size=\"lg\">Wrong code<\/th>\n<th data-col-size=\"lg\">Correct approach<\/th>\n<th data-col-size=\"md\">Why?<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">Using Float for normal decimals<\/td>\n<td data-col-size=\"lg\">let price: Float = 99.99<\/td>\n<td data-col-size=\"lg\">let price = 99.99 \/\/ Double<\/td>\n<td data-col-size=\"md\">Double is much more precise<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Using Double for money<\/td>\n<td data-col-size=\"lg\">let total = 123.45 + 0.01<\/td>\n<td data-col-size=\"lg\">let total: Decimal = 123.45 + 0.01<\/td>\n<td data-col-size=\"md\">Floating-point rounding errors<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Forgetting String is value type<\/td>\n<td data-col-size=\"lg\">Expecting shared mutation<\/td>\n<td data-col-size=\"lg\">var a = b; a += &#8220;!&#8221; \u2192 b unchanged<\/td>\n<td data-col-size=\"md\">Value types are copied<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Using Int32 \/ UInt32 everywhere<\/td>\n<td data-col-size=\"lg\">let count: Int32 = 100<\/td>\n<td data-col-size=\"lg\">let count = 100 \/\/ Int<\/td>\n<td data-col-size=\"md\">Int is the normal choice<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Declaring single letters as Character<\/td>\n<td data-col-size=\"lg\">let letter: Character = &#8220;A&#8221; everywhere<\/td>\n<td data-col-size=\"lg\">let letter = &#8220;A&#8221;<\/td>\n<td data-col-size=\"md\">String is more flexible<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">6. Summary table \u2013 most common types in real apps<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">Purpose<\/th>\n<th data-col-size=\"sm\">Recommended type<\/th>\n<th data-col-size=\"md\">Example value<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">Counting, indices, IDs<\/td>\n<td data-col-size=\"sm\">Int<\/td>\n<td data-col-size=\"md\">userCount = 1420<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Money, prices, financial<\/td>\n<td data-col-size=\"sm\">Decimal<\/td>\n<td data-col-size=\"md\">price = 2499.99<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Measurements, coordinates, science<\/td>\n<td data-col-size=\"sm\">Double<\/td>\n<td data-col-size=\"md\">temperature = 36.6<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">True \/ false flags<\/td>\n<td data-col-size=\"sm\">Bool<\/td>\n<td data-col-size=\"md\">isPremium = true<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Text, names, messages, JSON<\/td>\n<td data-col-size=\"sm\">String<\/td>\n<td data-col-size=\"md\">username = &#8220;sneha_dev&#8221;<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Single symbol (rare)<\/td>\n<td data-col-size=\"sm\">Character<\/td>\n<td data-col-size=\"md\">currency = &#8220;\u20b9&#8221;<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Very large numbers<\/td>\n<td data-col-size=\"sm\">Int64 \/ UInt64<\/td>\n<td data-col-size=\"md\">fileSizeBytes = 8_589_934_592<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Small counters \/ bytes<\/td>\n<td data-col-size=\"sm\">UInt8<\/td>\n<td data-col-size=\"md\">red: UInt8 = 255<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Which data type or group would you like to explore <strong>much deeper<\/strong> next?<\/p>\n<ul dir=\"auto\">\n<li>String in much more detail (very important)<\/li>\n<li>Decimal vs Double \u2013 with real money examples<\/li>\n<li>Collections (Array, Dictionary, Set)<\/li>\n<li>Optionals (Int?, String?, nil)<\/li>\n<li>Or start with structs &amp; enums<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me \u2014 we\u2019ll continue in the same slow, clear, teacher-like style \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What is a data type? (very simple explanation) A data type tells the computer: What kind of value this is (number, text, true\/false, list, \u2026) How much memory it needs What operations are&#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-2616","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2616","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=2616"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2616\/revisions"}],"predecessor-version":[{"id":2617,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2616\/revisions\/2617"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}