{"id":76,"date":"2016-03-11T02:46:43","date_gmt":"2016-03-11T02:46:43","guid":{"rendered":"http:\/\/luduscella.com\/blog\/\/?p=76"},"modified":"2016-03-11T03:06:47","modified_gmt":"2016-03-11T03:06:47","slug":"swift-fundamentals-constants-and-variables","status":"publish","type":"post","link":"http:\/\/luduscella.com\/blog\/swift\/swift-fundamentals-constants-and-variables","title":{"rendered":"Swift Fundamentals :  Constants and Variables"},"content":{"rendered":"<p>In Swift there are two ways of storing data, one is using variables and the other is constants.<\/p>\n<h2>Variables<\/h2>\n<p>Swift Variables can be modified after you assign an initial value, but you can not change their type. The reason being, the variables are mutable. When you declare a variable they can infer the variable type.<br \/>\nFor example:<\/p>\n<pre class=\"lang:swift decode:true \">var car = \u201cvw\u201d<\/pre>\n<p>the variable car is infered to be a string. Because the car is mutable the value can change:<\/p>\n<pre class=\"lang:swift decode:true \">car = \u201cbmw\u201d<\/pre>\n<p>After variables are defined you can not change their type:<\/p>\n<pre class=\"lang:swift decode:true \">car = 2016 \/\/ it would give an error<\/pre>\n<h2>Constants<\/h2>\n<p>Swift Constants are inmutable and they infer their type when you initialize the constant. For example:<\/p>\n<pre class=\"lang:swift decode:true \" >let year = 2016<\/pre>\n<p>year is inferred to be Int., year can not be modified either in the value or the type.<\/p>\n<h2>Static Typing and Type Inference<\/h2>\n<p>When you create a variable or a constant they can be a strong type or use type inference to determine the initial type.<\/p>\n<h3>Statically Typed<\/h3>\n<p>Being statically typed means that all your variables and constants MUST have their types declared in advance. Once you have declare (inferred) your variables and constants type cannot be change.<\/p>\n<pre class=\"lang:swift decode:true \" >var book:String\r\nbook = \"Swift book\"<\/pre>\n<h3>Type Inference \/ Strongly Typed <\/h3>\n<p>You can explicitly declare the type of your variable\/constant but you don\u2019t need to do this in Swift, it will infer the type if you assign the initial value. For example this is how you explicitly declare its type:<\/p>\n<pre class=\"lang:default decode:true \" >var message: String\r\nmessage = \u201cgreetings\u201d<\/pre>\n<p>Another way of doing it would be<\/p>\n<pre class=\"lang:default decode:true \" >var message: String  = \u201cgreetings\u201d<\/pre>\n<p>Please let me know if you have any questions by leaving a comment below or on twitter <a href=\"https:\/\/twitter.com\/luisemedr\" target=\"_blank\">@luisemedr<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Swift there are two ways of storing data, one is using variables and the other is constants. Variables Swift Variables can be modified after you assign an initial value, but you can not change their type. The reason being, the variables are mutable. When you declare a variable they can infer the variable type. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[11,14,13,10,12],"_links":{"self":[{"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/posts\/76"}],"collection":[{"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":15,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":91,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/posts\/76\/revisions\/91"}],"wp:attachment":[{"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}