<?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 Yadhukrishnan E on Medium]]></title>
        <description><![CDATA[Stories by Yadhukrishnan E on Medium]]></description>
        <link>https://medium.com/@yadhuekambaram1992?source=rss-cce8b302926a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VKJv1xIGzqLElpBfkLpBJw.jpeg</url>
            <title>Stories by Yadhukrishnan E on Medium</title>
            <link>https://medium.com/@yadhuekambaram1992?source=rss-cce8b302926a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 13:58:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yadhuekambaram1992/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[Swift: Optional- ?, !, ??]]></title>
            <link>https://medium.com/@yadhuekambaram1992/swift-optional-5c9cdf0c4655?source=rss-cce8b302926a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5c9cdf0c4655</guid>
            <category><![CDATA[optional-binding]]></category>
            <category><![CDATA[optionals]]></category>
            <category><![CDATA[if-let-statement]]></category>
            <category><![CDATA[guard-let]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Yadhukrishnan E]]></dc:creator>
            <pubDate>Wed, 16 Oct 2019 03:59:29 GMT</pubDate>
            <atom:updated>2019-10-16T03:59:29.505Z</atom:updated>
            <content:encoded><![CDATA[<h3>Swift: Optional- ?, !, ??</h3><p>Variables are used to store data. When a variable is declared, compiler needs to know about the data that it would contain.</p><p><strong>var a</strong></p><p>“When we use this piece of code in a program, compiler doesn’t know about the data that it would store and it won’t be able to allocate a space for this.”</p><p>There are two common scenario for a variable declaration<br> — developer knows the value of the variable at the time of declaration<br> — developer doesn’t know the value of the variable at the time of declaration.<br> <br> for the first scenario — it is pretty straight forward.<br> declare the variable with a value. So the compiler won’t be worrying about the data type.<br> Eg: var a = 10;<br> <br> the condition gets worse when the scenario two comes into picture.</p><p><strong>var a</strong><br> <br> “Here developer don’t know about the value then the compiler pops up an error “<br> <br> <strong>Error — <br> “error: type annotation missing in pattern var a”</strong><br> <br> Idea!!!<br> just specify the data type, problem solved.<br> <br> Both Developer and Compiler are happy :-)<br> <br> What if the developer wants to declare a variable, which can contain a “nil” value.</p><p><strong>By default swift won’t allow a variable to get assigned with the nil.</strong><br> <br> Here the “Optional” enumeration comes into picture.<br> It is a kind of type. To write a variable optional we can use the “Optional” keyword.<br> <br> <strong>Syntax: var &lt;variable name&gt;: Optional&lt;Data type&gt;</strong> <br> Eg: var a: Optional&lt;Int&gt;<br> <br> This means the variable ‘a’ either hold a value or it holds no value.<br> <br> The above syntax can even be shortened to <br> <br> <strong>Syntax: var &lt;variable name&gt;: &lt;Data Type&gt;?</strong><br> Eg: var a: Int?<br> <br> Look at the difference:<br> The second syntax just used the “int?” instead of writing “Optional&lt;Int&gt;”.<br> <br> Now ‘a’ has got the permision to hold nil value.<br> <br> But what will happen a if we try to print the value of variable ‘a’.<br> <br> Xcode won’t let you do that. It will show a warning……:-(<br> <br> So how to check whether the variable holds a value or not?<br> <br> There are two was to do that…<br> <br> <strong>1. if let</strong></p><blockquote>if let tempVariable = a {<br> print(tempVariable)<br> } else {<br> print(“a has no value”)<br> }</blockquote><p>In the above code, the value of ‘a’ is assigned to the constant ‘tempVariable’ then it will be checked for nil,<br> if tempVariable has a value then the if block gets executed or the else block gets executed.<br> (that logic is written below, it is just the logic, Don’t try to execute this code)<br> tempVariable = a<br> if tempVariable != nil {<br> print(tempVariable)<br> } else {<br> print(“a has no value”)<br> }</p><p>Another more interesting thing of ‘if let’ is that you can add more conditions.</p><p>var a: Int?<br>a = 19<br>if let tempVariable = a, tempVariable == 19 {<br>print(tempVariable)<br>} else {<br>print(“temp has no value or the second condition not met”)<br>}</p><p>In the above code, the if block will get executed only when both the conditions are true.</p><p><strong>2. guard let</strong> <br> It is same as if let. The only difference i noticed is that it doesn’t have an ‘if’ block.<br> ie.</p><blockquote>func printTheNumber(number: Int?) {<br> guard let tempVariable = number else {<br> print(“number has no value”)<br> return<br> }<br> <br> print(“ The number is — \(tempVariable)”)<br> }</blockquote><p>In here, the ‘guard let’ assigns the value to tempVariable and check whether it has a value or not. <br> if it has, then it ignores the gurad block and executes the print statement or it will execute the else block.<br> Again :- (that logic is written below, it is just the logic, Don’t try to execute this code)<br> tempVariable = number<br> if tempVariable == nil {<br> print(“number has no value”)<br> return<br> }<br> <br> print(“ The number is — \(tempVariable)”)<br> <br> Did you notice that i tried to print tempVariable if the condition of guard is true.<br> that is because<br> <br><strong> “Unlike if let statements, any variables or constants that were assigned values using an optional binding <br> as part of the condition are available for the rest of the code block that the guard statement appears in.”</strong><br> <br> So these two ways are called “safe unwrapping”.<br> <br> Then there must be an unsafe unwrapping, right? Yes, there exist.<br> <br> <strong>Unsafe unwrapping or force unwrapping is done using the exclamation(!) symbol.</strong><br> <br> var a: Int?<br> a = 10<br> print(a!)<br> <br> This way the value of a can be printed. <br> <br> What if we dont use !, <br> Then the compiler will print <br> <br> Optional(10).</p><p>There is an option to provide default values if the value of the variable is nil, and ‘??’ operator is used for providing default values.</p><p><strong>syntax: &lt;variableName&gt; ?? &lt;default value&gt;</strong></p><p>Eg. var c = a ?? 10</p><p>Here variable is checked for nil and if ‘a’ holds a value then it will return that value otherwise the default value of 10 is returned.</p><p>var c = (a == nil) ? 10 : a</p><p>this is what happens behind the scene.</p><p>We can also chain multiple ?? operator.</p><p>var a: String? = nil</p><p>var b: String? = nil</p><p>var c = a ?? b ?? 10</p><p>Here, value of ‘c’ is value of ‘a’ if a holds a value otherwise value of b is assigned but if the value of b is nil then 10 is assigned to ‘c’.</p><p>Hope this helped some of you guys, Thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5c9cdf0c4655" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift: Variable]]></title>
            <link>https://medium.com/@yadhuekambaram1992/swift-variable-62cc095c0a49?source=rss-cce8b302926a------2</link>
            <guid isPermaLink="false">https://medium.com/p/62cc095c0a49</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[swift-5]]></category>
            <category><![CDATA[variables]]></category>
            <category><![CDATA[swift-programming]]></category>
            <dc:creator><![CDATA[Yadhukrishnan E]]></dc:creator>
            <pubDate>Fri, 11 Oct 2019 22:20:32 GMT</pubDate>
            <atom:updated>2019-10-11T22:20:32.750Z</atom:updated>
            <content:encoded><![CDATA[<p>Swift is a new programming language for iOS, macOS, watchOS, and tvOS app development. Nonetheless, many parts of Swift will be familiar from your experience of developing in C and Objective-C.</p><p>Swift provides its own versions of all fundamental C and Objective-C types, including Int for integers, Double and Float for floating-point values, Bool for Boolean values, and String for textual data.</p><p>“While declaring a variable use meaningful variable names and more over use cameCase ”</p><p><strong><em>Let’s directly jump into the topic</em></strong></p><p><strong>Variable declaration</strong><br> — — — — — — — — — — — -</p><p>Like every other language, Data stored in swift is using <strong>variables</strong> and <strong>constants</strong>. Developer needs to declare the variable before use.</p><p><strong><em>For declaring</em></strong><br> Two keywords are “var” and “let”<br> — <strong>var</strong> for variables<br> —<strong> let</strong> for Constants</p><p>To declare a variable — like every other language, swift has a syntax</p><p><strong>var &lt;variable name&gt;: data type</strong><br> Eg: var x: Int</p><p>To assign a value to the variable, use the = operator and assign the desired value.</p><p>var x: Int = 10</p><p>When user assigns a value of different type than the variable type, then swift makes a compilation error<br> <br> Eg: var x: Int= “10”<br> <strong>“error: cannot convert value of type ‘String’ to specified type ‘Int’”</strong><br> <br> Here, variable x is declared as Int but tired to assign a String value.<br> <br> <br><strong> var &lt;variable name&gt;: data type = &lt;value&gt;</strong><br> Eg: var x: Int = 10</p><p>when user declares a variable with a value then swift automatically identifies the data type of the variable. <br>So user can ignore the data type part from the declaration part.</p><p><strong>var &lt;variable name&gt; = &lt;value&gt;</strong><br>Eg: var x = 10</p><p>This looks more cleaner! :)</p><p>All the primitive type variables are defined this way.</p><p>In the case of “let”, It is used for declaring constants. Swift recommends the usage of let <em>more than</em> var for performance and efficiency.<br> User needs to provide a value at the time of declaration</p><p><strong>Syntax : let &lt;variable name&gt;: &lt;data type&gt; = &lt;value&gt;</strong><br>Eg: let a: Int = 10</p><p>this time user won’t be able to change the value of the variable “a”, ie a constant.<br>If user changes the value of a constant, then swift will prompt an error<br> <br> let a : String = “10”<br> a = “user”<br> <br> Error — <br> — — — -<br> <strong>cannot assign to value: ‘a’ is a ‘let’ constant<br> a = “user”<br> <br> note: change ‘let’ to ‘var’ to make it mutable<br> let a : String = “10”</strong><br> <br> <br>Hope i have helped some of them, Thank you.</p><p><strong>References</strong></p><p><a href="https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html">The Basics - The Swift Programming Language (Swift 5.7)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62cc095c0a49" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>