<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amit Pandey on Medium]]></title>
        <description><![CDATA[Stories by Amit Pandey on Medium]]></description>
        <link>https://medium.com/@amit.3117?source=rss-c4eb99259314------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*NWXHgN4uz1fAVRV2zsuvbQ.png</url>
            <title>Stories by Amit Pandey on Medium</title>
            <link>https://medium.com/@amit.3117?source=rss-c4eb99259314------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 01:37:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amit.3117/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Functions in Swift]]></title>
            <link>https://medium.com/@amit.3117/functions-in-swift-49621121566b?source=rss-c4eb99259314------2</link>
            <guid isPermaLink="false">https://medium.com/p/49621121566b</guid>
            <dc:creator><![CDATA[Amit Pandey]]></dc:creator>
            <pubDate>Thu, 16 Jan 2020 14:02:54 GMT</pubDate>
            <atom:updated>2020-01-16T14:07:55.495Z</atom:updated>
            <content:encoded><![CDATA[<p>Swift unifies the function syntax to declare a simple C style function which just takes parameters to Objective-C style syntax which includes labels and parameters. Syntax:</p><pre>func nameOfFunction(optionalLabel paramName:&lt;type&gt;)-&gt;&lt;returnType&gt;</pre><pre>//calling <br>let returnType = nameOfFunction(optionalLabel:param)</pre><p>Some basic examples of function declaration and calling them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*73QckgZfJSIoP1I3EQVOFg.png" /></figure><p>Tuples: In Swift functions can return multiple values using tuples. Tuples group multiple values in a single compound value. Syntax:</p><pre>var tupleName = (Value1, value2, value3)</pre><p>Some examples for tuples declaration and usage:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NyCpJxFdsvIn45gz11p8FA.png" /></figure><p>Function can return more than one value using tuples:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZgxBzoS61W9BfeB28I9bXw.png" /></figure><p>Variadic Functions — It can take any number of arguments of same type.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*EVv2_-JqKt02CaRkQHCJIA.png" /></figure><p>In/Out parameters — By default function parameters passed are constants within scope of function. By using in/out parameters we can take an argument and change its value within function. Use <strong>inout </strong>keyword for same.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N3YBQAhpZkCEuRwMX56jtA.png" /></figure><p>Functions can be stored as variables and can be passed around as parameters. As we declare any other variable with a type, a function type will be declared with syntax: <strong>var</strong> varName: (params) -&gt; returnType.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ocyrFOnwYOb5Dcs-tVJe7A.png" /></figure><p>A nested function can capture any of its outer function’s arguments and can also capture any constants and variables defined within the outer function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cZ1PmDdJRM9dlMsHCx_YQg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49621121566b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Strings and Characters]]></title>
            <link>https://medium.com/@amit.3117/swift-strings-and-characters-7827889992c5?source=rss-c4eb99259314------2</link>
            <guid isPermaLink="false">https://medium.com/p/7827889992c5</guid>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Amit Pandey]]></dc:creator>
            <pubDate>Fri, 03 Jan 2020 11:07:14 GMT</pubDate>
            <atom:updated>2020-01-16T14:06:00.731Z</atom:updated>
            <content:encoded><![CDATA[<ul><li>Swift strings are unicode compliant.</li></ul><pre>//empty strings<br>let emptyString = &quot;&quot;<br>OR<br>let emptyString = String()</pre><pre>//unicode string<br>let str: String = &quot;I want to visit 🌍&quot;</pre><pre>//using hexadecimals to set values<br>let atom: String = &quot;\u{61}\u{5927}\u{1F34E}\u{3C0}&quot; // a大🍎π</pre><ul><li>Strings concatenation can be done by using + operator.</li></ul><pre>let helloWorld = &quot;Hello&quot; + &quot;World&quot;</pre><ul><li>As per documentation :</li></ul><pre>Swift’s String type is bridged with Foundation’s NSString class.Foundation also extends String to expose methods defined by NSString. <strong>This means, if you import Foundation, you can access those </strong><strong>NSString methods on </strong><strong>String without casting.</strong></pre><ul><li>String Literals: It is the way to define String variable or constant using double quotation marks. Thats how we define it usually, e.g. above example for helloWorld. We can also include special characters in string literals.</li></ul><pre><strong>let</strong> alphabets = &quot;A \nB \nC \nD&quot; //each alphabet goes in a new line</pre><p>Multiline Literals is another form of string literal which lets you define a string with line breaks. Becomes handy to represent JSON/XML string. We can also include special characters within multiline literals.</p><pre>let jsonString = &quot;&quot;&quot;  // multiline literals use <strong>&quot;&quot;&quot;</strong><br>{<br>  &quot;employee&quot;: {<br>    &quot;name&quot;: &quot;John&quot;,<br>    &quot;salary&quot;: 56000,<br>    &quot;married&quot;: true<br>  }<br>}<br>&quot;&quot;&quot;</pre><p>In Swift you can create mutable strings by declaring them as var, let strings are immutable.</p><ul><li>String interpolation: Using interpolation we can create a new string by using a mix of constants, variables, literals etc within string literal.</li></ul><pre><strong>let</strong> name = &quot;John&quot;<br><strong>let</strong> welcomeMessage = &quot;Welcome \(name)!&quot;</pre><ul><li>Characters: We can define character variables with type annotation and string literals. We can iterate through a string to access individual characters by using for-in syntax.</li></ul><pre>let char:Character = &quot;$&quot;</pre><pre><strong>let</strong> message = &quot;welcome&quot;<br><strong>for</strong> char <strong>in</strong> message {<br>print(char)<br>}</pre><pre>//Strings can be constructed by passing array of characters <br><strong>let</strong> cat: [Character] = [&quot;C&quot;, &quot;a&quot;, &quot;t&quot;]<br><strong>let</strong> catStr = String(cat)</pre><p><strong>Swift provides many useful methods on String. Let’s have a look at some of them:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aYyMbYOKMR52505s66By-w.png" /></figure><p>Substrings can be created from a string by using subscript or methods like prefix, the result is an instance of substring, which is part of same string. Substring has a region of memory where the characters that make up the substring are stored. Use substrings for short amount of time, in case you want to store them for long term operations, create a String with Substring.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c_5i7QNZX49srslm1ekr8A.png" /></figure><p>Thats not it, String has a lot other methods like suffix, prefix, sorted, reverse etc. Check out <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_String_Structure/index.html#//apple_ref/swift/struct/s:SS">Apple docs</a> or <a href="http://swiftdoc.org/type/String/">SwiftDoc</a> for a detailed list.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7827889992c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optionals in Swift]]></title>
            <link>https://medium.com/@amit.3117/optionals-in-swift-ee0e1dc7a5f1?source=rss-c4eb99259314------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee0e1dc7a5f1</guid>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Amit Pandey]]></dc:creator>
            <pubDate>Thu, 02 Jan 2020 12:47:23 GMT</pubDate>
            <atom:updated>2020-01-02T12:47:23.314Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tw8MgrOukero9O5y" /><figcaption>Photo by <a href="https://unsplash.com/@emilymorter?utm_source=medium&amp;utm_medium=referral">Emily Morter</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If you want to define a variable in Swift which may contain a value or nil, you need to define them as optionals. The person reading the program and compiler understands that those variables may not contain value at any given point of time and need to handle them carefully.</p><p>From docs:<em>Optionals say either “there is a value, and it equals x” or “there isn’t a value at all”. Using optionals is similar to using </em><em>nil with pointers in Objective-C, but they work for any type, not just classes.</em></p><p>Swift type safety makes sure you are using optionals properly and are not passing accidentally to the code which expects non-optional value.Optionals are declared with ‘?’ mark as compared to normal declaration:</p><p><em>let name:String? = nil //you cant assign nil unless you marked it as optional</em></p><p>We can summarise optionals requirement as :</p><ul><li>it can be nil or have an value</li><li>We need to unwrap optionals to safely use them</li></ul><h4>How optionals are implemented in Swift:</h4><p>You can check this <a href="https://github.com/apple/swift/blob/master/stdlib/public/core/Optional.swift">link</a> for Optionals code.These are implemented as enum in swift. Below is important part of definition:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*AuELbwuZefB3vpTDfEy8nQ.png" /></figure><p>There are two possible cases for this enum : .some or .none. .some will have an associated generic value, which we can access by using various ways to safely unwrap value.</p><h4>Unwrap value from optionals:</h4><p>We will go through all possible ways:</p><ul><li><strong>Forced unwrapping</strong>: We can unwrap the value from optionals by using ‘!’, next to variable. We can use this safely in if condition blocks after comparing value with nil. if you have multiple lines of code which uses optional in the if block, you will request as many number of unwrapping request. If we use it directly and no value is found, this method will end in fatal error causing crash.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*BD1v08_9BJi81M-2go08aw.png" /><figcaption>forced unwrapping</figcaption></figure><ul><li><strong>Optional Binding</strong> : In this method we create a temp constant containing value of optional, if it’s not nil and then go ahead using it in the block. The syntax is :</li></ul><p><em>if let newConst = optionalVar {</em></p><p><em>//Use newConst</em></p><p><em>}</em></p><p>Multiple ways of using optional binding:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eCOcnKUljVI2kmeqzHSXAw.png" /></figure><p>We can also use guard let statement as another form of optional binding. If an optional contains nil value this statement should return the scope, else proceed using the value. Using guard let statement tells the person reading your code the clear intention to return if the value is not contained by optional. <em>Unlike let statement where constant defined will be available in the block, constant declared from guard let statement is available in the defined scope.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rjGHeaPejsyCapH1Vi3xNw.png" /></figure><ul><li><strong>Optional chaining: </strong>If you have to access a property/method on optional type, you can use optional chaining. the syntax looks similar to forced unwrapping but with ? instead of !, and this is a safe way to access value unlike forced unwrapping. This becomes handy in those scenarios where we have to access the property on optionals a number of times, avoiding a lot of if let or guard let statements. Consider an example where we have to access text property of TextField UI component many times in a given code snippet:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L3B_HUQEhr5SI-gs7PWl0Q.png" /></figure><ul><li><strong>Nil Coalescing operator</strong>: We can use nil coalescing operator to either unwrapping an optional if it has a value, or providing a default if the optional is empty. It comes handy for quick use of optional value.</li></ul><pre>print(&quot;Hello, \(name ?? &quot;Anonymous&quot;)!&quot;) //print name or print Anonymous</pre><ul><li><strong>Implicitly unwrapped optionals: </strong>Use case for this is little edgy and suitable scenario should be carefully considered as it will not be safe otherwise. Defining an implicitly unwrapped variable provided the permission to unwrap the value every-time it is used. You can see the use of these types, while declaring IBOutlet variables.</li></ul><p>Think of the method which fits in your use case considering the unique capabilities.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee0e1dc7a5f1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Basics]]></title>
            <link>https://medium.com/@amit.3117/swift-basics-5ec6c86e0d29?source=rss-c4eb99259314------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ec6c86e0d29</guid>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Amit Pandey]]></dc:creator>
            <pubDate>Thu, 02 Jan 2020 12:43:15 GMT</pubDate>
            <atom:updated>2020-01-06T21:48:17.561Z</atom:updated>
            <content:encoded><![CDATA[<p>Swift is a multi-paradigm language, created by Apple which employs modern programming-language theory concepts and presents a simpler syntax. It is an alternate to Objective-C for iOS devices, it also supports Linux.During it’s introduction, it was described as “<strong>Objective-C without the C</strong>”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YN9rpysB0lWJ24Uj" /><figcaption>Photo by <a href="https://unsplash.com/@maxcodes?utm_source=medium&amp;utm_medium=referral">Max Nelson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Some interesting features about Swift:</p><ul><li>Open Source : <a href="https://github.com/apple/swift">https://github.com/apple/swift</a></li><li>Safer: Swift compiler provides multiple safeguards to prevent errors and improve readability.</li><li>Built with development in mind. It takes less effort to code in swift because of simpler syntax and easy to use API’s</li><li>Swift is unified with memory management. The support for Automatic Reference Counting (ARC) is complete across the procedural and object-oriented code paths.</li></ul><h4>Declaring constants and variables:</h4><p>Keywords let and var are used to define constants and variables. We can define these with or without the types, which is known as type annotation in swift. Some example of declarations:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JnjxLY4tw6jkl0HUbyFyuw.png" /></figure><h4>Swift primitive types:</h4><p>Although we use Int, Double etc as primitive types, they are defined as struct in Swift. From documentation:</p><p><em>Data types that are normally considered basic or primitive in other languages — such as types that represent numbers, characters, and strings — are actually named types, defined and implemented in the Swift standard library using structures.</em></p><p>Because of the implementation of basic types as structure we can pass message and call methods to these types:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*csvoVwfmFk5nABFUEBpBTQ.png" /></figure><h4>Basic types in Swift:</h4><p><strong>Int</strong> : In most cases rely on Int which will become 32 or 64 bits based on platform’s native word size.</p><p><strong>UInt</strong> : Similar to Int it will consider platform’s word size to act as UInt32 or UInt64. Use only when you need an unsigned integer or else <strong>Int</strong> is preferred.</p><p><strong>Double</strong>: represents a 64-bit floating-point number with precision of at least 15 decimal digits.</p><p><strong>Float</strong>: represents a 32-bit floating-point number with precision of at 6 decimal digits.</p><p>For all numeric and Float types, swift provides methods to check min &amp; max limit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EGCFxK_wE5R3LytjYegHBg.png" /><figcaption>Min &amp; Max limit for some basic types</figcaption></figure><p>Double to Int and vice versa conversions must be made explicit by using Double(intVar) or Int(doubleVar) syntax.</p><p><strong>Bool</strong>: contains true or false. As Swift is type safe language, if statement has a check for Bool type, you will get an error in below scenarios:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HqxE9QIFrHkwcNqxFbDh4g.png" /></figure><p>That covers the numeric types in Swift.</p><p>Please continue for next story for more types like string, characters, collections (array, dictionary, map, set) and optionals. Also for compound types Functions, Tuples.</p><p>Next Story: <a href="https://medium.com/@amit.3117/optionals-in-swift-ee0e1dc7a5f1?sk=6dcc49c119107b44a7f1ba0c1a36acf2">Optionals</a></p><figure><a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_image"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iwthGEeUMHMu9QK60Q2mTA.png" /></a></figure><p>📝 Read this story later in <a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_read_later_text">Journal</a>.</p><p>👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. <a href="https://usejournal.com/newsletter/noteworthy-in-tech/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_text">Read the Noteworthy in Tech newsletter</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ec6c86e0d29" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Properties & Property Observers]]></title>
            <link>https://medium.com/@amit.3117/swift-properties-property-observers-5b6ff3870c83?source=rss-c4eb99259314------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b6ff3870c83</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[property-observer]]></category>
            <category><![CDATA[swift-properties]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Amit Pandey]]></dc:creator>
            <pubDate>Mon, 02 Dec 2019 13:49:19 GMT</pubDate>
            <atom:updated>2020-01-06T21:48:47.034Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="The observer is the observed" src="https://cdn-images-1.medium.com/max/1024/0*AwAiEJ75Tiv1gATd" /><figcaption>The observer is the observed</figcaption></figure><p>We can define different type of properties in swift. This post is trying to cover basic types and know how to use them efficiently.</p><pre>                       Swift Property Types<br>┌─────────────────────┬──────────────────────────────────────────┐<br>│    Property Type    │               Supported By               │<br>├─────────────────────┼──────────────────────────────────────────┤<br>│ Stored Properties   │ classes, structures                      │<br>│ Computed Properties │ classes, structures, enums               │<br>│ Type Properties     │ classes, structures, enums(only methods) │<br>└─────────────────────┴──────────────────────────────────────────┘</pre><p>Let’s go through these in details :</p><h4><strong>Stored Properties : </strong>Stored properties can be constants/variables as part of instance. We need to provide a default value to stored properties while declaration or implement intializer which provides initial values.</h4><pre>struct SolarSystem {</pre><pre>  let allPlanets:Int = 9 // declared with a value</pre><pre>  var currentPlanet:String // declared and assigned value in initializer</pre><pre>  init() {<br>  currentPlanet = &quot;Planet Earth&quot;<br>  }</pre><pre>}</pre><pre>var system = SolarSystem()<br>system.currentPlanet = &quot;Mars&quot; // access and modify stored property</pre><pre>/* alternatively we can declare instance as constant, no modification will be allowed to any property */<br>let world = SolarSystem() <br>world.currentPlanet = &quot;Venus&quot; //compilation error!!</pre><h4>Property Observers: If we need to observe any change in stored property we can implement <strong>property observers</strong>. We need to implement <strong>willSet and </strong>didSet for that property<strong>.</strong> Some important points about property observers :</h4><ul><li><strong>It can be applied only to a mutable property.</strong> Can’t use on let (no point in observing a value which will never change 🙂).</li><li><strong>Lazy properties can not have property observers.</strong> We need to have an initial value to monitor change. In case of lazy properties, initial value can be missing, which defeats the purpose of observing.</li><li><strong>inout params (which allows to mutate the passed value in a function) gets synced once the function scope ends.</strong> Observer methods for inout params will be notified only after the scope of function is over.</li><li><strong>Property observers will not be called during initial setting of value</strong>. It will be called only when the value changes.</li></ul><pre>struct SolarSystem {</pre><pre>  let allPlanets:Int = 9 </pre><pre>  // no notification to observer while initializing variable  <br>  var currentPlanet:String  = &quot;Earth&quot;{ <br>      //we can avoid newValue in param<br>      willSet(newValue){<br>        print(&quot;current planet will update to \(newValue)&quot;)<br>      }<br>      didSet{ <br>        print(&quot;current planet is updated from \(oldValue) to \     (currentPlanet)&quot;)<br>      }<br>  }</pre><pre>  //function with inout param<br>  func updateCurrentPlanet(planet:inout String)<br>  {<br>      planet = &quot;Mars&quot; // no notification to observers at this point<br>      <br>      planet = &quot;Pluto&quot; // same here <br>      <br>      planet = &quot;Mercury&quot;<br>      <br>      // observer will be notified after the scope is over<br>  }<br>}</pre><pre>var system = SolarSystem()<br>system.updateCurrentPlanet(planet:&amp;system.currentPlanet)</pre><h4><strong>Lazy Stored Properties</strong>: Initial value for these properties is not calculated until it’s accessed for the first time. <strong>Global properties are lazy by default</strong>. Only variable properties can be declared as lazy. Use it in below scenarios:</h4><ul><li>When initial value for a property needs a complex set up or computation. It can improve performance in some scenarios.</li><li>Can keep initializers more clean.</li></ul><pre>class NumbersList{</pre><pre>var maxRange:Int<br>    <br>/*Below is an example of property set with closure with lazy keyword<br>  Property set with closure syntax can be used to encapsulate the code to create a stored property, this will be called only once.<br>lazy will delay this call till first access. Remove lazy keyword to see what happens :)*/<br>    <br>     lazy var oddNumberList:Array&lt;Int&gt; = { <br>        var odds = [Int]()<br>        print(&quot;lazy var created!&quot;)<br>        for number in 0...maxRange where number % 2 == 1 {<br>            odds.append(number)   <br>        }<br>        return odds<br>    }()</pre><pre>    init(){//No code for oddNumberList calculation<br>            print(&quot;NumbersList initialized!&quot;)<br>            maxRange = 0<br>    }<br>}</pre><pre>var numList = NumbersList() //No iterations while creating instance<br>print(&quot;travelDistance created!&quot;)<br>numList.maxRange = 50 <br>print(&quot;maxRange set!&quot;)<br>_ = numList.oddNumberList</pre><ul><li>Can be used to avoid optionals.</li></ul><pre>class MyViewController: UIViewController {</pre><pre>    // Instead of declaring UILabel property as optional or as an implicitly unwrapped optional stored property<br>    <br>   lazy var label: UILabel = UILabel(frame: .zero)<br>    <br>    override func loadView() {<br>        super.loadView()<br>        <br>        self.view.addSubview(self.label)<br>    }</pre><pre>    override func viewDidLoad() {<br>          super.viewDidLoad()</pre><pre>          self.label.textColor = .black<br>          self.label.font = UIFont.systemFont(ofSize: 20, weight: .bold)<br>    }<br>}</pre><p>As per Swift documentation : <strong><em>If a property marked with the lazy modifier is accessed by multiple threads simultaneously and the property has not yet been initialized, there is no guarantee that the property will be initialized only once.</em></strong></p><h4><strong>Computed properties: </strong>These properties do not store values. They implement getter and an optional setter and calculate the value each whenever the property is accessed. Usually it uses other stored properties to calculate the value.</h4><pre>struct Distance{</pre><pre>    var distanceInMeter:Double = 0.0<br>    <br>    var distnaceInMile:Double {<br>        get{<br>           return distanceInMeter*0.000621371<br>         }<br>        //newValue passed in setter <br>        set(newValue){<br>            distanceInMeter = newValue * 1609.34 // we can access and modify other members<br>         }<br>    }</pre><pre>    var distanceInKm:Double {<br>        get{<br>        return distanceInMeter*0.001<br>        }</pre><pre>//short syntax , can avoid newValue in param        <br>        set{ <br>            //newValue is still available to be accessed<br>            distanceInMeter = newValue * 1000.0<br>        }<br>        <br>    }<br>    <br>    //only providing getter will make it readonly<br>    var distnaceInYards:Double {<br>        get{<br>        return distanceInMeter*1.09361<br>         }<br>    }<br>}</pre><pre>var travelDistance = Distance()<br>travelDistance.distanceInMeter = 25000 <br>print (&quot;\(travelDistance.distnaceInMile) Miles to go!&quot;)<br>travelDistance.distanceInKm = 15<br>print (&quot;\(travelDistance.distanceInMeter) Meters to go!&quot;)<br>print (&quot;\(travelDistance.distnaceInYards) Yards to go!&quot;)</pre><p>However it is recommended to use functions over computed properties, if calculation is not straightforward. e.g.</p><ul><li>computation can raise exceptions.</li><li>complexity of calculation is not O(1)</li><li>calculation is complex or have multiple steps</li></ul><h4><strong>Type properties: </strong>Unlike properties which belong to an instance, these belong to the type itself. These can be accessed by class/struct names. Enums can define only computed type properties. Can be used to define variable or constant properties which are universal/common to all instances.</h4><p>To create type properties, we can use <strong>static</strong> keyword on stored properties and <strong>static/class</strong> keyword on computed properties. <strong>class</strong> keyword will allow the property to be overridden in derived classes.</p><p><strong>static </strong>can be used in <strong>structs as well as classes</strong>. <strong>class keyword</strong> can only be used for <strong>class </strong>properties (at this point of time you can only use this keyword for methods).</p><pre>class File{<br>    var content = &quot;&quot;<br>    static var textLimit = 100 //overriding this in a subclass will give you error<br>}</pre><pre>File.textLimit = 20 //text limit remains common for all instances<br>var firstFile = File()<br>firstFile.content = &quot;This is a first file&quot;<br>var saved = firstFile.saveFile<br>var secondFile = File()<br>secondFile.content = &quot;This is a second file. Making sure to cross 20 char limit&quot;</pre><p>Thanks for reading. Comments and questions are welcome.</p><figure><a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_image"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iwthGEeUMHMu9QK60Q2mTA.png" /></a></figure><p>📝 Read this story later in <a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_read_later_text">Journal</a>.</p><p>👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. <a href="https://usejournal.com/newsletter/noteworthy-in-tech/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_text">Read the Noteworthy in Tech newsletter</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b6ff3870c83" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>