<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Agoi Abel Adeyemi</title>
    <description>The latest articles on DEV Community by Agoi Abel Adeyemi (@abelagoi).</description>
    <link>https://dev.to/abelagoi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F47943%2Ffae519bb-d3a8-4911-b034-f6845640cf15.jpg</url>
      <title>DEV Community: Agoi Abel Adeyemi</title>
      <link>https://dev.to/abelagoi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abelagoi"/>
    <language>en</language>
    <item>
      <title>Array and Map in Solidity</title>
      <dc:creator>Agoi Abel Adeyemi</dc:creator>
      <pubDate>Wed, 01 Apr 2020 04:34:43 +0000</pubDate>
      <link>https://dev.to/abelagoi/array-and-map-in-solidity-31cp</link>
      <guid>https://dev.to/abelagoi/array-and-map-in-solidity-31cp</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qZ_mwY3T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1292/1%2A4DgMmkpqsMPF8Akl8AMsUg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qZ_mwY3T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1292/1%2A4DgMmkpqsMPF8Akl8AMsUg.png" alt="alt array and map in solidity"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An array allows us to represent a collection of data, but it is often more useful to think of an array as a collection of variables of the same type with keys having sequential order based on the order they were added.&lt;/p&gt;

&lt;p&gt;Instead of declaring individual variables, such as number1, number2, number3, ….., and number99, you can declare one array variable such as numbers and use numbers[0], numbers[1], …, numbers[99] to represent individual variables.&lt;/p&gt;

&lt;p&gt;Having understood how the array works in JAVASCRIPT and PHP, I was going to rush through it in solidity but to my greatest surprise, it is quite different here in the solidity blockchain world :). I decided to write about the array and map since both of them seem similar to me and can be used together.&lt;/p&gt;

&lt;p&gt;Unlike in javascript where we could have an array of integer together with strings or any other data type, our array must have the variable of the same data type and this can be enforced when declaring it like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uint[] myArray; //declaring an array of integer variables
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Before I dive fully into the array in solidity, I want to clarify that there are two types of arrays in solidity, which are the storage array and the memory array.&lt;/p&gt;
&lt;h2&gt;
  
  
  Storage Array:
&lt;/h2&gt;

&lt;p&gt;These are arrays that are stored inside the blockchain after you execute your function, you can declare them like normal variables without our contract like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;where unit/bool/address is the variable type, followed by [] and then the name of the array which can be any name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Manipulation of the storage array:
&lt;/h3&gt;

&lt;p&gt;To add a new element to the array, we need to reference the array and use the .push(value), we can also update an array position by referencing the element key like myArray[0] = ‘new value’;, we also can also get a particular array element position by simple using myArray[0] where 0 is the index or key of the array element&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We can also use the for loop to access the element of the array as we would array in javascript (also in the snippet above). To delete an element in the array, we will use delete myArray[1] where 1 is the array key, please note that this will not affect the array length but will only reset the array value at the index of one or second element in the array to the default value of the data type. i.e 0 if it’s a uint type and false if bool. When we try to access an array key that does not exist, we will have an error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Memory Array:
&lt;/h3&gt;

&lt;p&gt;These arrays are not stored into the blockchain after calling the function, you can only declare them within a function like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;where unit[] is the type of the variables, memory keyword declares it has a memory array followed by the name which can be an acceptable name, it must be equated to new unit&lt;a href="https://dev.to10"&gt;&lt;/a&gt; type where the unit[] must be the variable type again and the number of elements that will be in the array in the bracket ().&lt;/p&gt;

&lt;h3&gt;
  
  
  Manipulation of the memory array:
&lt;/h3&gt;

&lt;p&gt;To add value to the memory array, we cannot use the .push() method, as we need to use the index notation instead like newArray[0] = 1 , newArray[1] = 1 etc. We can read a value, update a value, delete a value from the memory array just like the storage array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Passing an array as a function argument:
&lt;/h3&gt;

&lt;p&gt;We can pass an array as a function argument in solidity, the function visibility determines if the keyword before the array name should be calldata or memory. We use calldata if the function visibility is external and memory if the function visibility is public and internal. See example below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Seems we have covered all we need to do in an array, how about mapping.&lt;/p&gt;

&lt;p&gt;A Map is something like an array with a collection of similar variables that uses key look-ups meaning you’re referencing values with an object instead of an integer in short.&lt;/p&gt;

&lt;p&gt;Basically, mapping is equivalent to a dictionary or a map in other programming languages. It’s key-to-value storage. A standard array is an index look-up e.g. if there are 10 elements in the array the indexes are 0–9 and it would look like below as an array of integers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0] 555
[1] 123
...
[8] 22
[9] 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The map works slightly different and the easiest way to describe it is that it uses key lookups. So if this was a map of addresses to integers then it would look something like:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0x000000000000000A] 555
[0x0000000000000004] 123
....
[0x0000000000000006] 22
[0x0000000000000003] 6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So basically, you’re referencing values with an object instead of an integer. The keys also don’t have to be in sequence. It consists of two main parts: a _KeyType and a _ValueType; they appear in the following syntax below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mapping (_KeyType =&amp;gt; _ValueType) mapName.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Think of the _KeyType as the key you'll pass through a function to return the desired value, or _ValueType. By default, a mapping is initially empty, so a new _KeyType will first need to be mapped to a _ValueType.&lt;/p&gt;

&lt;p&gt;It could be&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mapping(string =&amp;gt; string) mapName;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Or&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mapping(int =&amp;gt; bool) mapName;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Manipulation of a map:
&lt;/h3&gt;

&lt;p&gt;We can manipulate a map within a function like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We assigned a value to the map here balances[msg.sender] = 100 by equating it to a value. We read the value also by balances[msg.sender] without assigning any value. When we use a non-existence key to read a value from a map, it will return the default data value used to initialize the value type when declaring it unlike undefined in javascript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Assign an array as a map value:
&lt;/h3&gt;

&lt;p&gt;We can assign an array as a value type to a map and can access them as we would an array like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Assign another map as a map value:
&lt;/h3&gt;

&lt;p&gt;We can also assign another map as a map value and can access them as we would access a map like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We can assign a Struct as a map value type too, there are occasions when you want to iterate over a map and do other things on a map, I would advise you check out the resources below when the need arises:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ethereum.stackexchange.com/questions/12145/how-to-&amp;lt;br&amp;gt;%0Aloop-through-mapping-in-solidity"&gt;Guide&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/48898355/soldity-iterate-through-address-mapping"&gt;Guide&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/46466792/how-mapping-works-in-solidity-and-is-mapping-analogous-to-another-concept-in-ot"&gt;Guide&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>ethereum</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>The complete guide to Forms in React</title>
      <dc:creator>Agoi Abel Adeyemi</dc:creator>
      <pubDate>Fri, 07 Sep 2018 11:34:04 +0000</pubDate>
      <link>https://dev.to/abelagoi/the-complete-guide-to-forms-inreact-2c4o</link>
      <guid>https://dev.to/abelagoi/the-complete-guide-to-forms-inreact-2c4o</guid>
      <description>&lt;h2&gt;
  
  
  a letter about react forms to me in the future
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Forms are very useful in any web application. Unlike angular and angularjs, that gives form validation out of the box. You have to handle forms yourself in React. This brought about many complications like how to get form values, how do I manage form state, how do I validate my form on the fly and show validation messages. There are different methods and libraries out there to help with this but if you are like me that hates dependent on too many libraries, welcome on board, We are going to bootstrap our own form from the ground up.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are two types of form input in &lt;code&gt;react&lt;/code&gt;. We have the &lt;code&gt;uncontrolled input&lt;/code&gt; and the &lt;code&gt;controlled input&lt;/code&gt;. The &lt;code&gt;uncontrolled input&lt;/code&gt; are like traditional HTML form inputs, they remember what you typed. We will use &lt;code&gt;ref&lt;/code&gt; to get the form values.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We added the &lt;code&gt;ref="name"&lt;/code&gt; to the input tag so that we can access the value with &lt;code&gt;this.refs.name.value&lt;/code&gt; when the form is submitted. The downside to this is that you have to “pull” the value from the field when you need it and this can happen when the form is submitted.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;controlled input&lt;/code&gt; is when the react component that renders a form also controls what happens in that form on subsequent user input. Meaning, as form value changes, the component that renders the form saves the value in its state.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Of course, another component can handle the form state. The goal is that each time the input changes, the method &lt;code&gt;changeHandler&lt;/code&gt; is called and will store the input state. Hence the component always has the current value of the input without needing to ask for it. This means that the form component can respond to input changes immediately; for example&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in-place feedback, like validation&lt;/li&gt;
&lt;li&gt;disabling the button unless all fields have valid data&lt;/li&gt;
&lt;li&gt;enforcing a specific input format&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Handling multiple forms inputs
&lt;/h3&gt;

&lt;p&gt;In most situations, we are going to have more than one form input. We need a way to capture the input with a method instead of declaring multiple methods to do this. Hence we are going to modify the &lt;code&gt;changeHandler&lt;/code&gt; to below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Because of the way the changeHandler has been modified above, our form input can reference it to update it states dynamically.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Create a TextInput Component
&lt;/h3&gt;

&lt;p&gt;There are different input elements e.g text, email, password, select option, checkbox, date, radio button, etc. I love to create a separate custom component for the input elements, let us start with the &lt;code&gt;text input type&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Notice the &lt;code&gt;{…props}&lt;/code&gt;, we use this to distribute the props to the input element. We can use the custom text input element like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Validating our Custom TextInput
&lt;/h3&gt;

&lt;p&gt;Since we are using the &lt;code&gt;controlled input&lt;/code&gt;, we can add more keys to our formControls state to help validate the input. We need the &lt;code&gt;valid&lt;/code&gt; property to denote if the input is valid or not, the &lt;code&gt;validationRules&lt;/code&gt; contains the list of the rules to be checked before the &lt;code&gt;input&lt;/code&gt; is valid.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Our aim is that each time the input changes. We make sure the validationRules for that input is checked for true or false, then update the valid key with the result of the check. We also added the touched property to denote that the user has touched the form input, this will help with displaying validation feedback when the input has been touched. The check will be done in the changeHandler method like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The valid is equated to the methodvalidate(value, prevState.formControls[name]).validationRules) which we will use to check if the valid status for a particular control is true or false.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I move the validate method to a separate class then import it. The validate method accepts two parameters, the value and the rules. We loop through the rules and check if each rule is valid, then return true when valid and false when invalid.&lt;/p&gt;

&lt;p&gt;Let assume we want to add another validation on name e.g we wantname to be required. All we need do is update the formControl for name validationRules, and write the logic for it in the validator class like below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Then we need to update the validator class to accommodate for the required validator.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We created a custom TextInput, we created a formControl that has a property named name with a validation rules of isRequired and minLength of 3. Below is the component that handles this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If we click the submit button after filling the TextInput, the formSubmitHandler will console the formControls value like below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AwTQSUDZKzev2j59WLoWqLg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2AwTQSUDZKzev2j59WLoWqLg.png"&gt;&lt;/a&gt;&lt;em&gt;valid = true or false&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The good thing is that we do not have to wait till the user click submit before we can know if the form input is valid or not. Since it is actually stored in the component state, so, therefore, we can use this to display error message or feedback when the user is typing. We can even disable the submit button until the validation passes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Displaying error feedback
&lt;/h3&gt;

&lt;p&gt;For us to be able to display error feedback on the input, we need to pass the touched and valid property for that particular input as a prop to it component. We will add the error style based on the valid status and we want to do this only when the input has been touched.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We also need to modify our TextInput component to display the style based on the value props.valid and props.touched.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Please note that you should have added the form-control and control-error style into the App.css.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You should see a screenshot like below if you TextInput is invalid and had been touched.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Aix96HRi05LglXVxIpbgt1A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2Aix96HRi05LglXVxIpbgt1A.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Disable Submit Button if the form is Invalid
&lt;/h1&gt;

&lt;p&gt;Html 5 has a disabled property on button input, we can equate our formControls property valid status to the disabled property. As long as the formControls is not valid.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The disabled={!this.state.formControls.name.valid} will work fine if we have only one form control but if we need to handle more than one form control, we can set a new property to the state which will keep track of when the validity status of the whole formControl object. So we need to update our state to accommodate for this&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We need to update the changeHandler method so we can loop through all form controls valid status, and when valid, update the formIsValid status to true.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;With this setup, it will be easier for us to set the disabled property to formIsValid status, and this will handle one or more form object.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Considering other form input type
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;TEXTAREA&lt;/strong&gt;: The textarea, email and password will work similar to a text input. We can create a TextArea component.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;EMAIL&lt;/strong&gt;: We can also create an Email component just like the TextInput&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;PASSWORD&lt;/strong&gt;: We can also create a Password component just like the TextInput&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The email, textarea and password form control will look similar to the text input form input&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;SELECT OPTION&lt;/strong&gt;: The Select Option form control is slightly different to the other form control because we have to accommodate for the select options. It will look like to below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;then the Select Option component will look like below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;RADIO&lt;/strong&gt;: The radio input is similar to the select option since it’s only one option that can be selected out of the available options, The form control will be similar to the select option form control. Below is how the radio button component looks like.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Putting it all together, Assuming we want to have an email input, name (TextInput), gender (Select Input), radio input all in a form control. Below is an example of what your component will look like&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>react</category>
      <category>forms</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Introducing ReactJs Terminologies With Next Generation Javascript</title>
      <dc:creator>Agoi Abel Adeyemi</dc:creator>
      <pubDate>Sun, 15 Apr 2018 07:48:45 +0000</pubDate>
      <link>https://dev.to/abelagoi/introducing-reactjs-terminologies-with-next-generation-javascript-3hop</link>
      <guid>https://dev.to/abelagoi/introducing-reactjs-terminologies-with-next-generation-javascript-3hop</guid>
      <description>&lt;h3&gt;
  
  
  Let &amp;amp; Const
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;let&lt;/code&gt; for &lt;code&gt;variable values&lt;/code&gt;, something you will assign and can always change at any point in time.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;const&lt;/code&gt; if you want to create a &lt;code&gt;constant value&lt;/code&gt;, something you will assign once and will never change.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Exports &amp;amp; Imports (Modules)
&lt;/h3&gt;

&lt;p&gt;We can write our JavaScript files in “modules” and then reference them as dependencies in other scripts. This allow us to split our code into different concern based on functionalities. Each file can be called a module.&lt;/p&gt;

&lt;p&gt;We need to use the &lt;code&gt;export&lt;/code&gt; keyword if we are going to use a particular module within another module. The &lt;code&gt;default&lt;/code&gt; keyword in the Person.js class below means that whenever we import Person from another class, the Person will just be the default export hence no need for &lt;code&gt;{}&lt;/code&gt; in the import statement.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;To use the above module within another module, we need to import it like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We can always export more than one module from a file, that was the reason why I didn’t use the &lt;code&gt;default&lt;/code&gt; keyword, this will affect the way we will import the module and use within another class.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;To use the above module within another modules, we need to import it like below and use the &lt;code&gt;{}&lt;/code&gt; to the extract the particular export we need from the module since there is no default export.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Functions
&lt;/h3&gt;

&lt;p&gt;There are two main ways to declare functions in javascript which are the &lt;code&gt;function expression&lt;/code&gt; and the &lt;code&gt;function declaration&lt;/code&gt; like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;From the &lt;code&gt;Let &amp;amp; Const&lt;/code&gt; lesson above, we can change the functionTwo variable declaration to &lt;code&gt;const&lt;/code&gt; since I am sure my there will never be a reason for me to change the value of the function. Hence functionTwo can be declared like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Again, there is something called &lt;code&gt;Arrow Function&lt;/code&gt; in javascript and the function above can be represented with arrow function like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If our function is going to receive parameters, we can represent it with the arrow function like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;There are situations whereby all our function does is to return something without doing some computation or whatsoever. This can allow us use a shorter syntax like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;An example of this within reactjs is with the use of a &lt;code&gt;functional component&lt;/code&gt; to return just a JSX like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Classes
&lt;/h3&gt;

&lt;p&gt;We can represent classes in javascript like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;this.name&lt;/code&gt; is the class property and &lt;code&gt;printMyName()&lt;/code&gt; is a class method. A class can also have inherit from another class like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;An example of this within reactjs is with the use of a &lt;code&gt;stateful component&lt;/code&gt; like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Introducing Component
&lt;/h3&gt;

&lt;p&gt;A sample react component look similar to what we have below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Remember, We imported &lt;em&gt;React, {Component} and ‘./App.css’&lt;/em&gt; because we need to use them inside the component, we also export the App because we are going to use it within another component. The App inherit from Component, this is common way of writing a stateful react component, unlike the functional component below. We won’t need to import &lt;em&gt;{Component}.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A stateful component must have the render() method but a functional component does not have to.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;A react component (stateful or functional) must always return an HTML like syntax to the dom called JSX.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That html been return within a react component is actually a &lt;em&gt;JSX&lt;/em&gt; which is not just an HTML but an HTML with Javascript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Introducing JSX
&lt;/h3&gt;

&lt;p&gt;A sample JSX below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;React.createElement()&lt;/code&gt; can take at least three parameters. The first been the parent element(i.e &amp;lt;div&amp;gt;), the second can represent an object which can contain styling(Note, we use &lt;code&gt;className&lt;/code&gt;instead of &lt;code&gt;class&lt;/code&gt; &lt;em&gt;within JSX).&lt;/em&gt; The third is the element or content that is going to be within the parent element. This can also be another &lt;code&gt;React.createElement()&lt;/code&gt;&lt;em&gt;.&lt;/em&gt; The above JSX will translate to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="App"&amp;gt;
  &amp;lt;h1&amp;gt;I am a React App&amp;lt;/h1&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Thanks to &lt;a href="https://www.npmjs.com/package/create-react-app"&gt;create-react-app&lt;/a&gt;. We can easily just write the above code like below&lt;/p&gt;

&lt;p&gt;And It will be translated for us. That is the more reason why we have to import React from ‘react’, because there is going to be a translation to the previous way of writing it behind the scene.&lt;/p&gt;

&lt;p&gt;Meanwhile, if we use create-react-app, The above will be translated for use hence we simple have to do below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;I feel Setting up webpack and scaffolding our own react setup is really something you should learn later after getting to understand reactjs better. Someone just getting into it should just use the &lt;a href="https://www.npmjs.com/package/create-react-app"&gt;create-react-app&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is just my way of introducing ReactJs, thanks you for taking the time to read this article.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>View Properties use by Auto Layout</title>
      <dc:creator>Agoi Abel Adeyemi</dc:creator>
      <pubDate>Sun, 11 Feb 2018 07:26:54 +0000</pubDate>
      <link>https://dev.to/abelagoi/view-properties-use-by-auto-layout--34l4</link>
      <guid>https://dev.to/abelagoi/view-properties-use-by-auto-layout--34l4</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Intrinsic content size, Compression Resistance Priority, Content Hugging Priority&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fq3THKeV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AReQ7-rRbMO-moiBe6rwRNA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fq3THKeV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AReQ7-rRbMO-moiBe6rwRNA.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are some views that automatically size themselves base on the content they hold, This is referred to as their intrinsic content size. E.G A button’s intrinsic content size is the size of its title plus a small margin. Not all views have an intrinsic content size. For views that do, the intrinsic content size can define the view’s height, it’s width or both.&lt;/p&gt;

&lt;p&gt;Let say we have a button with the text “Click Me”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aVfSI-wh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AiKuxxcLzxADnwKAq0PzTIA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aVfSI-wh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AiKuxxcLzxADnwKAq0PzTIA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We definitely don’t want the button to be smaller than the text inside it, Otherwise the text would be clipped. This is know as Compression Resistance Priority.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The higher a view’s Compression Resistance Priority, the more the view resists growing larger than its intrinsic content size.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Still on the “Click Me” button, If we don’t want the button to be really bigger than the content inside it like the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8OJOjMUa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2Ad1zQJ8tQ7TeM_0HnG1HdfQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8OJOjMUa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2Ad1zQJ8tQ7TeM_0HnG1HdfQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we want the button to hug its content without too much padding, we have to increase the Content Hugging Priority.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The higher a view’s Content Hugging Priority is, the more the view resists growing larger than its intrinsic content size.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can set the content hugging and compression resistance priority programmatically like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The two methods take two parameters which are the UILayoutPriority and the UILayoutConstraintAxis. The priority level can be required, defaultHigh, defaultLow, fittingSizeLevel where required is the highest and fittingSizeLevel is the lowest. The UILayoutConstraintAxis can either be a vertical or horizontal axis.&lt;/p&gt;

&lt;p&gt;I will write another article on UIStackview, after which I will show ways to programmatically code different views. Thanks for reading.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>autolayout</category>
    </item>
    <item>
      <title>Programmatically approach Auto Layout</title>
      <dc:creator>Agoi Abel Adeyemi</dc:creator>
      <pubDate>Sun, 11 Feb 2018 07:21:06 +0000</pubDate>
      <link>https://dev.to/abelagoi/programmatically-approach-auto-layout--3998</link>
      <guid>https://dev.to/abelagoi/programmatically-approach-auto-layout--3998</guid>
      <description>&lt;p&gt;Layout is the way in which the parts of something are arranged or laid out. When designing user interfaces in swift, we have to consider how the elements are arranged, and how it will be rearranged on different screen sizes. To layout user interfaces such that they can adapt to changes based on different screen sizes in swift, we have to use autolayout.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Auto layout simple means we describe the position where our interfaces should be, then the system will automatically position the interfaces for us.&lt;br&gt;
Auto layout is a constraint base descriptive system whereby the relationship between two views can be represented as a linear equation&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A1 = (M * A2) + C
where the A1 and A2 refer to views' attribute, 
M refer to multiplier and 
C refer to as a constant
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;A constraint allows us to establish a relationship between the attributes of a single view or to the attribute of another view.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is an attribute?
&lt;/h3&gt;

&lt;p&gt;An attribute is a position on a view’s alignment rectangle. In the equation above, A1 will be an attribute on view 1, and A2 will be an attribute on view 2. A view’s attribute(s) is use to establishes relationship with another view’s attributes. The first set of attributes correspond to the edges of the view’s alignment rectangle which are the &lt;code&gt;top&lt;/code&gt;, &lt;code&gt;bottom&lt;/code&gt;, &lt;code&gt;right&lt;/code&gt; and &lt;code&gt;left&lt;/code&gt; attributes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MYw_BEBF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AxgIcNDwXpd9oKXbPtRNiXg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MYw_BEBF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AxgIcNDwXpd9oKXbPtRNiXg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The second set of attributes are the leading, trailing, left and right attributes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_c_BSzxE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AE3v9wIwNoj1X_wmCoryumA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_c_BSzxE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AE3v9wIwNoj1X_wmCoryumA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next set of attributes are the height and width, the height and width attributes allow us to create relationship with the height and width of our view or another view.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yKYlz7pc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A1NsvVc2DGtHDnryiGQYR9w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yKYlz7pc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A1NsvVc2DGtHDnryiGQYR9w.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next set are the CenterX and CenterY attributes which are use to align a view horizontal and vertical axis.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n9sotuH6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AavlfslqOMkNH_PrpO6COpQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n9sotuH6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AavlfslqOMkNH_PrpO6COpQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We also have the baseline attribute, first baseline and last baseline. The baseline is like an imaginary line where text sits on, if there are multiple baselines, the first one is the first baseline and the last is the last baseline.&lt;/p&gt;

&lt;p&gt;Every view contain margins that allow you to modify how much space exists between the edges of the view and any subview added, the attributes that govern this relationship are mostly the top, right, bottom and left margin. In addition to the margin attributes, we also have the leading margin, trailing margin, centerX with margin, centerY with margin. We also have NotAnAttribute that allow us to not use constrain on other view named . This attributes are the options we can use when establishing relationship between views.&lt;/p&gt;
&lt;h3&gt;
  
  
  Type of relationship
&lt;/h3&gt;

&lt;p&gt;Since we already know we have to use attributes to create relationship between two views, the next decision is the type of relationship that should exist between these views. There are three possible relationship that can exist between the views, but we can only use one at a time: Equal, Less than or equal and Greater than or equal.&lt;/p&gt;

&lt;p&gt;Equal means the viewA’s particular attribute should be equal to viewB’s particular attribute.&lt;/p&gt;

&lt;p&gt;Less than or equalmeans the viewA’s particular attribute should only be less than or equal to viewB’s particular attribute.&lt;/p&gt;

&lt;p&gt;Greater than or equal means the viewA’s particular attribute should only be greater than or equal to viewB’s particular attribute.&lt;/p&gt;
&lt;h3&gt;
  
  
  Multiplier
&lt;/h3&gt;

&lt;p&gt;The multiplier which is represented as M in the equation is use to multiply (adjust) the relation by a certain of floating point value. We can say viewA.height should be *2 (two times) of viewB.height.&lt;/p&gt;
&lt;h3&gt;
  
  
  Constant
&lt;/h3&gt;

&lt;p&gt;The constant is use to offset a view away from another, it is basically use to create space between two views.&lt;/p&gt;
&lt;h3&gt;
  
  
  Adding Auto Layout in Code
&lt;/h3&gt;

&lt;p&gt;We have three choices when it comes to programmatically creating constraints, you can use NSLayoutConstraint class, layout anchors, or you can use the Visual Format Language. We are going to use NSLayoutConstraint class and layout anchors.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;let horizontalConstraint = NSLayoutConstraint()&lt;/code&gt; is use to initialise a new constraint, where item: newValue is the element we want to add the constraint on, attribute: NSLayoutAttribute.centerX on line 4 tells us which attribute on the item in line 1 we wish to add the constraint on. The relatedBy: NSLayoutRelation.equal is use to tell the type of relationship, toItem: view is use to tell the other view’s attribute we want to establish the constraint with. In our case, it is the window view. attribute: NSLayoutAttribute.centerX on line 7 is use to tell the attribute on the other view we want to create the relationship with. Then the multiplier and the constant. Immediately after creating the constraints, we need to add the constraint(s) to the parent view which was done on line 12. We can use Layout anchor to repeat the same like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;where newView.centerXAnchor is the view we want to add a centerXAnchor constraint that is equal to window view centerXAnchor.&lt;/p&gt;

&lt;p&gt;If you do not understand some of the concept explained here yet, please be patient. I am writing another article on some view properties used by auto layout, then UIStackview, after which I will show ways to programmatically code different views.&lt;/p&gt;

</description>
      <category>autolayout</category>
      <category>swift</category>
      <category>ios</category>
    </item>
    <item>
      <title>Protocol in Swift with Practical Examples</title>
      <dc:creator>Agoi Abel Adeyemi</dc:creator>
      <pubDate>Fri, 19 Jan 2018 12:20:29 +0000</pubDate>
      <link>https://dev.to/abelagoi/protocol-in-swift-with-practical-examples-3iif</link>
      <guid>https://dev.to/abelagoi/protocol-in-swift-with-practical-examples-3iif</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2A80_c-wJSKSVcTIc4hi7KMQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1600%2F1%2A80_c-wJSKSVcTIc4hi7KMQ.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;protocol&lt;/em&gt; defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be &lt;em&gt;adopted&lt;/em&gt; by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to &lt;em&gt;conform&lt;/em&gt; to that protocol.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;So, keeping it simple, a protocol says a struct , class or enum that if you want to be THAT, do THIS, this and This. Example: if you want to be a developer, you have to READ, and WRITE CODE.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Protocol Creation and Adoption
&lt;/h3&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can create a &lt;code&gt;Protocol&lt;/code&gt; like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protocol SomeProtocol { //protocol definition goes here}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Classes, structs, and enums can adopt these protocol by placing protocol’s name after the type’s name, separated by a colon, as part of their definition. Multiple protocols can be listed, and are separated by commas.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct SomeStruct: FirstProtocol, SecondProtocol { //struct definition goes here}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If a class has a superclass, list the superclass name before any protocols it adopts, followed by a comma.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SomeClass: SomeSuperClass, FirstProtocol, AnotherProtocol { //class definition goes here}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Example one: Property Requirement
&lt;/h3&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;The above protocol simply says any class/struct/enum that want to adopt me must have a property called &lt;code&gt;fullName&lt;/code&gt; which must be a type of &lt;code&gt;String&lt;/code&gt; and the property must be a &lt;em&gt;gettable property which was represented with { get }&lt;/em&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;First Implementation&lt;/code&gt; and &lt;code&gt;Second Implementation&lt;/code&gt; both have a property called &lt;code&gt;fullName&lt;/code&gt;, hence they both conform to the &lt;code&gt;FullNameable protocol&lt;/code&gt; though they have different implementation of it. What our &lt;code&gt;FullNameable&lt;/code&gt; protocol says is that we need to have a &lt;code&gt;fullName&lt;/code&gt; property, it does not say how we should get this property. Wait, what then makes a property &lt;code&gt;gettable&lt;/code&gt;?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A gettable property is a property we can only read from after we have given it an initial value.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;When we try to assign a new value to the &lt;code&gt;fullName&lt;/code&gt; property, it will fail simply because of the &lt;code&gt;let&lt;/code&gt; keyword we used when initializing the Lecturer struct.&lt;/p&gt;

&lt;p&gt;The reason for this example is to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;show how to use a protocol and conform to it&lt;/li&gt;
&lt;li&gt;let us understand that classes that conform to a property does not have to have the same implementation.&lt;/li&gt;
&lt;li&gt;Understand what &lt;code&gt;{ get }&lt;/code&gt; stands for&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example Two: Connecting Loose End Class
&lt;/h3&gt;

&lt;p&gt;Let us assume we want to make a smoothie and the requirements needed to make the smoothie are &lt;code&gt;Milk&lt;/code&gt; and &lt;code&gt;Dairy&lt;/code&gt; products. The make a smoothie, we have to blend some fruits, as well as some Dairy. So we are going to need a blend method that all the classes that will make a smoothie must have, hence we have to enforce this by using protocol like below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;To make the smoothie, we need to call the blend method on each classes that conform to the Blendable protocol and we can achieve&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The makeSmoothie method will accept an array of classes that conform to the Blendable protocol, then use the for loop to access the blend method in each class. The application will throw an error if a class that does not conform to the Blendable protocol is passed in as an array.&lt;/p&gt;

&lt;p&gt;The reason for this example is to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;show how to use method as protocol requirement&lt;/li&gt;
&lt;li&gt;Understand how to access loose end class that conform to a protocol&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example Three: When to use a protocol
&lt;/h3&gt;

&lt;p&gt;A problem is to decide when to use a &lt;code&gt;protocol&lt;/code&gt; to &lt;code&gt;class inheritance&lt;/code&gt;. The best cheat to determine when to use a &lt;code&gt;protocol&lt;/code&gt; compare to &lt;code&gt;class inheritance&lt;/code&gt; is to know when the type of relationship that determine between your classes. Is it an &lt;code&gt;IS-A&lt;/code&gt; relationship or a &lt;code&gt;HAS-A&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;IS-A&lt;/code&gt; relationship is a type of relationship where a child will have every thing the parent has as well as have additional properties. A good example is an Airplane and a JetPlane. A &lt;code&gt;JetPlane is basically an Airplane&lt;/code&gt; with additional properties / methods&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;HAS-A&lt;/code&gt; relationship is a type of relationship where two classes are not the same but share similar properties / method. A good example is a Bird and an Airplane. A Bird can fly as well as an Airplane. This is the best time to use a protocol.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The reason for this example is to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;show when to use protocol over class inheritance&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example Four: Protocol Inheritance
&lt;/h3&gt;

&lt;p&gt;A protocol can inherit one or more other protocols and can add further requirements on top of the requirements it inherits. The syntax for protocol inheritance is similar to the syntax for class inheritance, but with the option to list multiple inherited protocols, separated by commas:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Any class/struct /enum that inherit the Pet protocol must conform to the Animal protocol as well because the Pet protocol inherit from the Animal protocol.&lt;/p&gt;

&lt;p&gt;I am happy to share this article with you. If you’ve enjoyed this article, do show support by loving it . Thanks for your time and make sure to follow me or drop your comment below. You can read more about Protocol here👇&lt;br&gt;
&lt;a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
    </item>
    <item>
      <title>Enums in Swift with Real Life Examples</title>
      <dc:creator>Agoi Abel Adeyemi</dc:creator>
      <pubDate>Tue, 16 Jan 2018 02:12:03 +0000</pubDate>
      <link>https://dev.to/abelagoi/enums-in-swift-with-real-life-examples-58k4</link>
      <guid>https://dev.to/abelagoi/enums-in-swift-with-real-life-examples-58k4</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A1OixPCgpE5pbhvnhB2DPkw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A1OixPCgpE5pbhvnhB2DPkw.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Introduction
&lt;/h4&gt;

&lt;p&gt;Enumeration (Enum) defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Assuming we have a method named &lt;code&gt;dayType&lt;/code&gt; that accept any day of the week then return &lt;code&gt;Weekend&lt;/code&gt; for &lt;code&gt;Saturday and Sunday&lt;/code&gt;, &lt;code&gt;Weekday&lt;/code&gt; for &lt;code&gt;Monday, Tuesday, Wednessday, Thursday and Friday&lt;/code&gt; and return &lt;code&gt;This is not a valid date&lt;/code&gt; when it fails to recognised what was been passed into it. The function will work as aspected but what happens when you actually want to pass let say a &lt;code&gt;friday&lt;/code&gt; but you mistakingly type &lt;code&gt;fridaysy&lt;/code&gt; even though what we actually wanted to type was &lt;code&gt;friday&lt;/code&gt;. The switch above goes to the default &lt;code&gt;This is not a valid date&lt;/code&gt;. That is the type-safty &lt;code&gt;enum&lt;/code&gt; brings into the table. It help us to avoid such risk by providing common type for group of related values like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We have been able to eliminate the use of String by defining an enum to hold the days. We don’t need to use the append the &lt;code&gt;Day&lt;/code&gt; before every declaration of the day enum within the switch statement because we already assigned day to be a type of Day. Hence the dayType function can be reduce to below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We have to understand enum is best suited for declaring types with finite sets of possible states, like Direction (north, south, west, east), Movement (Up, down, left, right) etc.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Enum Values&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;We can assign value to each &lt;code&gt;enum&lt;/code&gt; case. This is useful if the &lt;code&gt;enum&lt;/code&gt; itself indeed relates to something&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We can access the value assigned to an enum with the &lt;code&gt;rawValue&lt;/code&gt; keyword. To access the Week enum above, we can use &lt;code&gt;Week.Monday.rawValue&lt;/code&gt; which will return &lt;code&gt;Weekday&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Enum Associate Values
&lt;/h4&gt;

&lt;p&gt;Associate values are a fantastic way of attaching additional information to an &lt;code&gt;enum&lt;/code&gt; case. Say you’re writing a trade engine, there are two different possible trade types, Buy and Sell that will have a specific stock and amount. We can represent this using associate enum values like below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Associate values are a fantastic way of attaching additional information to an &lt;code&gt;enum&lt;/code&gt; case. Say you’re writing a trade engine, there are two different possible trade types, Buy and Sell that will have a specific stock and amount. We can represent this using associate enum values like below&lt;/p&gt;

&lt;h4&gt;
  
  
  Enum Methods
&lt;/h4&gt;

&lt;p&gt;We can also define methods on an &lt;code&gt;enum&lt;/code&gt; like below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Notice the code snippet above, the func description will return "This is an apple device" for every case in the &lt;code&gt;enum&lt;/code&gt;. To avoid this, we can make use of switch statement within the description method like below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I am happy to share this article with you. If you’ve enjoyed this article, do show support loving it 👏 . Thanks for your time and make sure to follow me or drop your comment below 👇&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
    </item>
    <item>
      <title>Optionals in Swift</title>
      <dc:creator>Agoi Abel Adeyemi</dc:creator>
      <pubDate>Mon, 15 Jan 2018 18:45:44 +0000</pubDate>
      <link>https://dev.to/abelagoi/optionals-in-swift-3ncf</link>
      <guid>https://dev.to/abelagoi/optionals-in-swift-3ncf</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AK_ASFaz5nY8Ft1f2ZtV3bA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AK_ASFaz5nY8Ft1f2ZtV3bA.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Absent of Data
&lt;/h3&gt;

&lt;p&gt;There are situations where we wish to accept data from users and also provide an opportunity for some of the data to be optionals, meaning they are not required but should be provided if available.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The constant &lt;code&gt;me&lt;/code&gt; instance of a person provided all the data but the constant &lt;code&gt;anotherPerson&lt;/code&gt; cannot provide all the data because he probably does not have a middlename. Hence he passed &lt;code&gt;nil&lt;/code&gt; as the value of the &lt;code&gt;middlename&lt;/code&gt;. Sounds cool right but the &lt;code&gt;nil&lt;/code&gt; passed will cause an error, Why?, because we have not told swift to make the middlename &lt;code&gt;optional&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is an Optional in Swift?
&lt;/h3&gt;

&lt;p&gt;An optional in Swift is a type that can hold either a value or no value. Optionals are written by appending a &lt;code&gt;?&lt;/code&gt;to any type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var middlename: String?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The above means that middlename can either be a string or does not contain anything which is represented by the &lt;code&gt;?&lt;/code&gt;. &lt;em&gt;An optional is a kind of container. An optional String is a container which might contain a string. An optional Int is a container which might contain an Int&lt;/em&gt;. Think of an optional as a kind of parcel. Before you open it (or “unwrap” in the language of optionals) you won’t know if it contains something or nothing. Its only an &lt;code&gt;Optional&lt;/code&gt; value that can be set to &lt;code&gt;nil&lt;/code&gt;, and that was the reason for the error when we passed the &lt;code&gt;middlename&lt;/code&gt; a value of &lt;code&gt;nil&lt;/code&gt; without specifying its a type of &lt;code&gt;Optional&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var red: String = "Red"
red = nil // error: nil cannot be assigned to type 'String'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  How to create an Optional?
&lt;/h3&gt;

&lt;p&gt;The simplest way to create an &lt;code&gt;optional&lt;/code&gt; in swift is to add &lt;code&gt;?&lt;/code&gt; in front of the type like below&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var middlename: String?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Another way is using the Optional keyword like below&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var middlename: Optional&amp;lt;String&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Using (unwraping) an optional
&lt;/h3&gt;

&lt;p&gt;The simplest way to unwrap an optional is to add a &lt;code&gt;!&lt;/code&gt;after the optional name.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Notice the &lt;code&gt;\(middlename!)&lt;/code&gt;, the &lt;code&gt;!&lt;/code&gt; there is use to unwrap the middlename so we can get the value “abel”. The problem is that when we passed a &lt;code&gt;nil&lt;/code&gt; value to the middlename, we will get a runtime crash which says &lt;code&gt;nil is Unexpected&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Before unwrapping, we need to be sure that there’s a value. So therefore we are going to continue the rest of the article with how to check before using (unwrapping) optionals.&lt;/p&gt;

&lt;h4&gt;
  
  
  Use If to Check for nil before force unwrapping:
&lt;/h4&gt;

&lt;p&gt;Since it was &lt;code&gt;nil&lt;/code&gt; that was passed to the variable when we initialized the Person struct, we can use an if statement to check for &lt;code&gt;nil&lt;/code&gt; before unwrapping the &lt;code&gt;Person&lt;/code&gt; struct like below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Whenever we use the &lt;code&gt;!&lt;/code&gt; symbol, it is denoted as force unwrapping the optional value, but it a common practice to avoid force unwrapping it, because we can write our code in such a way that swift will automatically unwrap it for us&lt;/p&gt;

&lt;h4&gt;
  
  
  Using Optional binding:
&lt;/h4&gt;

&lt;p&gt;With optional binding, though we are still using if, swift provide a way for us to use the &lt;code&gt;if let&lt;/code&gt; statement then automatically unwrap it for us like below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Using Guard for early exit:
&lt;/h4&gt;

&lt;p&gt;Guard provide a way to quickly exit when checking for the &lt;code&gt;optional&lt;/code&gt; string and make our code much more cleaner hence there wont be need for &lt;code&gt;if else&lt;/code&gt; statement.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We have covered how to use optionals in swift, let us look at some cool secondary use cases of optionals which include using &lt;code&gt;multiply optionals&lt;/code&gt;, &lt;code&gt;method chaining&lt;/code&gt; and &lt;code&gt;Nil Coalescing&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;streetName&lt;/code&gt; in the Street class is an optional string, the &lt;code&gt;House&lt;/code&gt; class has a street variable which is also a Optional Street. The Person class too has a variable house which is an Optional house. When we have to access the person street. To access the person street (myStreet), we ain’t sure if the person gave us the house information (myHouse). That was the reason why we use the &lt;code&gt;if let myHouse = Person.house&lt;/code&gt;. If the &lt;code&gt;Person.house&lt;/code&gt;is empty, the if statement will fail to execute, else the variable will be assigned to the &lt;code&gt;myHouse&lt;/code&gt; property. Next, we have to access the &lt;code&gt;myHouse.street&lt;/code&gt; and if its available, the variable will be assigned to the &lt;code&gt;myStreet&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The pattern used to access the myStreet can be improved with the use of optional chaining like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I am happy to share this article with you. If you’ve enjoyed this article, do show support by loving it . Thanks for your time and make sure to follow me or drop your comment below 👇&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
    </item>
    <item>
      <title>Inheritance in Swift</title>
      <dc:creator>Agoi Abel Adeyemi</dc:creator>
      <pubDate>Fri, 12 Jan 2018 08:24:29 +0000</pubDate>
      <link>https://dev.to/abelagoi/inheritance-inswift-pag</link>
      <guid>https://dev.to/abelagoi/inheritance-inswift-pag</guid>
      <description>&lt;p&gt;A class can &lt;em&gt;inherit&lt;/em&gt; methods, properties, and other characteristics from another class. When one class inherits from another, the inheriting class is known as a &lt;em&gt;subclass&lt;/em&gt;, and the class it inherits from is known as its &lt;em&gt;superclass&lt;/em&gt;. A class that does not inherit from another class is known as a base class.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The above code snippet shows how the &lt;code&gt;someSubclass&lt;/code&gt; inherited from the &lt;code&gt;someClass&lt;/code&gt; class. Classes in Swift can call and access methods and properties belonging to their superclass and can provide their own overriding versions of those methods and properties to refine or modify their behavior. Classes can also add property observers to inherited properties in order to be notified when the value of a property changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Accessing Superclass Properties and Methods
&lt;/h3&gt;

&lt;p&gt;Assuming we have a base class named &lt;code&gt;Person&lt;/code&gt; and a &lt;code&gt;Youtuber&lt;/code&gt; class that inherit from the Person class&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The first thing I want us to notice is the &lt;code&gt;class Youtuber: Person{}&lt;/code&gt;. We simply said the &lt;code&gt;Youtuber&lt;/code&gt; be a type of &lt;code&gt;Person&lt;/code&gt; which make it inherit all the properties and method of the &lt;code&gt;Person&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;Secondly, when we initialize the &lt;code&gt;Subclass&lt;/code&gt;, we use the &lt;code&gt;super.init()&lt;/code&gt; method to pass the parameters needed by the &lt;code&gt;superclass&lt;/code&gt; constructor since a &lt;code&gt;Youtuber&lt;/code&gt; is actually still a &lt;code&gt;Person&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can access the &lt;code&gt;celebrateBirthday()&lt;/code&gt; method of the &lt;code&gt;Person&lt;/code&gt; class from the &lt;code&gt;Youtuber&lt;/code&gt; subclass like below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Overriding Methods
&lt;/h3&gt;

&lt;p&gt;At times, the &lt;code&gt;subclass&lt;/code&gt; might wish to override a method contained in a &lt;code&gt;superclass&lt;/code&gt;. We will make use of the &lt;code&gt;override&lt;/code&gt; keyword within the &lt;code&gt;subclass&lt;/code&gt; like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I initialized the &lt;code&gt;Person&lt;/code&gt; class as well as the &lt;code&gt;Youtuber&lt;/code&gt; class, the celebrateBirthday method in the Person class will return a different value to the Youtuber class because of the &lt;code&gt;override&lt;/code&gt; function that print a different content in the Youtuber class.&lt;/p&gt;

&lt;h3&gt;
  
  
  Overriding Properties
&lt;/h3&gt;

&lt;p&gt;we have seen how to override &lt;code&gt;methods&lt;/code&gt; in swift, but there are cases where we actually need to override properties.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Immediately after the &lt;code&gt;super.init()&lt;/code&gt; in the inside &lt;code&gt;init&lt;/code&gt; method, we set the country variable. Note that, we can only do this after we have initialize the superclass.&lt;/p&gt;

&lt;h3&gt;
  
  
  Preventing Override
&lt;/h3&gt;

&lt;p&gt;We can prevent a method or property from been overridden by a subclass by using the keyword &lt;code&gt;final func&lt;/code&gt; or &lt;code&gt;final var&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is more to inheritance like observers. Didn’t cover that here because am not pretty sure how to use it now. Will update this article when I fully understand the concept. Lemme go read up &lt;code&gt;enum and optionals&lt;/code&gt;. Yes, will write about it when done.&lt;/p&gt;

&lt;p&gt;_I am happy to share this article with you.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
    </item>
    <item>
      <title>Structs and Classes in Swift — What, When and How</title>
      <dc:creator>Agoi Abel Adeyemi</dc:creator>
      <pubDate>Thu, 11 Jan 2018 16:32:38 +0000</pubDate>
      <link>https://dev.to/abelagoi/structs-and-classes-in-swiftwhat-when-andhow-2p38</link>
      <guid>https://dev.to/abelagoi/structs-and-classes-in-swiftwhat-when-andhow-2p38</guid>
      <description>&lt;p&gt;Structs and Classes are object oriented programming concept in swift, the main reason for using them is so you can more closely associate behaviours together. Assuming I have a dog, it must have a name, belongs to a breed and has an age. I can group this property of a dog using either a class or a structure like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The name, breed and age are the properties of a dog represented by the Struct above can also be represented using a class like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Struct and Class has a lot of similarities which include defining properties to store value, define methods to provide functionalities, define initializers to set up their initial state, can be extended to expand their functionality beyond a default implementation etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Instance of a Struct and Class
&lt;/h3&gt;

&lt;p&gt;To access a struct, we have to call it and pass the default properties they need like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Struct&lt;/code&gt; automatically bind the properties which are the name, breed and age in our Dog Struct when we create a new instance of the Dog but we have to manually do that in classes by defining the &lt;code&gt;init&lt;/code&gt; method.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Once we add properties to a class, we are require to define the &lt;code&gt;init()&lt;/code&gt; method which in other languages represent the &lt;code&gt;constructor()&lt;/code&gt; method but &lt;code&gt;struct&lt;/code&gt; does that automatically for us.&lt;/p&gt;

&lt;h3&gt;
  
  
  Methods in Structs and Classes
&lt;/h3&gt;

&lt;p&gt;We can declare functions within our class/struct. Functions within class/struct are called &lt;code&gt;methods&lt;/code&gt;. In our dog object, we can define a method to get the dog’s details like below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Once we have declared our method, we can access them by creating an instance of them then we use the &lt;code&gt;.&lt;/code&gt; notation to access the &lt;code&gt;method&lt;/code&gt; name. We can access the &lt;code&gt;Dog Struct&lt;/code&gt; or &lt;code&gt;Class&lt;/code&gt; like below&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Differences btw Structs and Classes
&lt;/h3&gt;

&lt;p&gt;Though we have used the &lt;code&gt;Struct&lt;/code&gt; and &lt;code&gt;Class&lt;/code&gt; almost the same way but I need us to know that they are different to each other and there are use cases when we have to use one over the other. Lets outline the differences:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Struct&lt;/code&gt; is a value type while &lt;code&gt;Classes&lt;/code&gt; are reference type.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Value type&lt;/code&gt; is a type whose value is copied when it is assigned to a variable or constant, or when it is passed to a function.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Reference type&lt;/code&gt; is not copied when assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used instead.&lt;/p&gt;

&lt;p&gt;I will show some code snippet below to make you understand better&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We created a new instance of Dog and called it &lt;code&gt;dog1&lt;/code&gt;, and assign the &lt;code&gt;dog1&lt;/code&gt; instance to &lt;code&gt;dog2&lt;/code&gt; which means &lt;code&gt;dog1&lt;/code&gt; should be equal to &lt;code&gt;dog2&lt;/code&gt;. We then changed the value of &lt;code&gt;dog1&lt;/code&gt; name to &lt;code&gt;JaneDony&lt;/code&gt;. This automatically changes the name of &lt;code&gt;dog2&lt;/code&gt; too, since they are equals to each other and are &lt;code&gt;reference type&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;struct&lt;/code&gt; example above act differently to the &lt;code&gt;class&lt;/code&gt; in that after we made &lt;code&gt;dog1&lt;/code&gt; equal &lt;code&gt;dog2&lt;/code&gt;. Whenever we change any value of either of &lt;code&gt;dog1&lt;/code&gt; or &lt;code&gt;dog2&lt;/code&gt;, it will not cause a change on the other one. Hence &lt;code&gt;dog1.name&lt;/code&gt; in the above code will not change when &lt;code&gt;dog2&lt;/code&gt; name changes.&lt;/p&gt;

&lt;p&gt;Another major difference is that we can not use &lt;code&gt;inheritance&lt;/code&gt; when we use &lt;code&gt;struct&lt;/code&gt; but we can with &lt;code&gt;class&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I am happy to share this article with you. If you’ve enjoyed this article, do show support by giving a few claps&lt;/em&gt; 👏&lt;em&gt; . Thanks for your time and make sure to follow me or drop your comment below&lt;/em&gt; 👇&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
    </item>
    <item>
      <title>Functions in Swift</title>
      <dc:creator>Agoi Abel Adeyemi</dc:creator>
      <pubDate>Tue, 09 Jan 2018 10:40:46 +0000</pubDate>
      <link>https://dev.to/abelagoi/functions-in-swift-94g</link>
      <guid>https://dev.to/abelagoi/functions-in-swift-94g</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy827jpy36hyj662hfw2b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy827jpy36hyj662hfw2b.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Functions are self-contained chunks of code that perform specific tasks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining and Calling a Function
&lt;/h3&gt;

&lt;p&gt;Right below is a simple function, that actually does nothing at the moment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func emptyFunction() { //your code should go inside here}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;func&lt;/code&gt; keyword is use to define a function while the &lt;code&gt;emptyFunction&lt;/code&gt; is the name given to the function. You must use the &lt;code&gt;func&lt;/code&gt; anytime you create a function, and the function name can vary base on what the function will do.&lt;/p&gt;

&lt;p&gt;The above function can be called within my code like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;emptyFunction()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function Parameters
&lt;/h3&gt;

&lt;p&gt;There are times when we need to pass parameters &lt;br&gt;
to a function, we can pass parameters to a function like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func functionWithParameter(parameterOne: Int) { 
    print("This is my parameter: \(parameter)") 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;paramterOne&lt;/code&gt; is the parameter passed into the function while the &lt;code&gt;Int&lt;/code&gt; denote the data type of the parameter. The &lt;code&gt;data type (Int)&lt;/code&gt;serves as a kind of contract that must be adhere to when we call the function meaning, the parameter must be a type of &lt;code&gt;Int.&lt;/code&gt;We need to include the parameter we passed when calling the function like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;functionWithParameter(parameterOne: 32)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use more than one parameter in a function by separating the parameters with a comma&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//declaring the function
functionWithParameters(parameterOne: Int, parameterTwo: String) {
}

//calling the function
functionWithParameters(parameterOne: 32, parameterTwo: "Hello")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function Return Types
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;return type&lt;/code&gt; is use to denote the data type of the value our function should return. Just like the parameter type, Xcode will enforce us to make sure that the function returns the type it specified.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func functionWithReturnType() -&amp;gt; String { 
    return "I must return a string"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are many data types in swift which include Integer, Boolean, String, Float, Double. The &lt;code&gt;-&amp;gt; String&lt;/code&gt; denotes that the function must return a type of String. Any function that does not return a value has a special return type named &lt;code&gt;Void&lt;/code&gt;. When we do not specify a function return type, we have simply told swift that the return type is a void hence our function must not return anything.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Different ways to declare Void

func noReturnType() { 
    //swift will automatically take this function as void 
    //hence we must not return anything
}

function returnWithReturnType() -&amp;gt; Void { 
    //same as above, but we declare the void ourselves
}

function returnWithReturnType() -&amp;gt; () { 
    //this is another way to declare void
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function With More Than One Return Value &amp;amp; Type
&lt;/h3&gt;

&lt;p&gt;There are times when we need our function to return more than one value, We can do this in swift with the help of a tuple like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//declaring the function
myFunction () -&amp;gt; (value1: Int, value2: String) {
    return (value1, value2)
}

//calling the function
let result = myFunction(value1: 12, value2: "Hello from me")

//we can access the return value like below
result.value1
result.value2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function Parameter With Default Value
&lt;/h3&gt;

&lt;p&gt;We can define a default value for any function parameter should in case we do not want to pass the value at all time&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func someFunction(parameterWithDefault: Int = 32) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we call the &lt;code&gt;someFunction(parameterWithDefault: 50)&lt;/code&gt; and pass in a parameter of 50, it will override 32 which is the default value assigned to the function, we can also call the function without a parameter.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function Parameter External And Local Name
&lt;/h3&gt;

&lt;p&gt;There are times when we need to give our function an external and local name, mostly basically for readability. The external name is use to pass the parameter into the function while the internal name is use to access the parameter inside of the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func myFunction(havingValue value: Int) { 
    //we can access the havingValue parameter with value 
    print(value)
}

//calling the function
myFunction(havingValue: 50)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that we use the &lt;code&gt;havingValue&lt;/code&gt; to pass the parameter when we call the function but to access the parameter inside of the function, we use &lt;code&gt;value&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Omitting Argument Labels
&lt;/h3&gt;

&lt;p&gt;Each time we have called a function that has a parameter, we have been defining the parameter name like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myFunction(label: Int) {}

//calling the function
myFunction(label: 32)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can omit the &lt;code&gt;label&lt;/code&gt; when calling the function by adding a &lt;code&gt;_&lt;/code&gt; as the function external name like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myFunction(_ label: Int) {}

//calling the function without the label definition
myFunction(32)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is more to function in swift than what I have covered here which is just an introduction to it. You can read more about swift function &lt;a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html" rel="noopener noreferrer"&gt;here&lt;/a&gt; on apple documentation&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I am happy to share this article with you. If you’ve enjoyed this article, do show support by giving a few claps&lt;/em&gt; 👏&lt;em&gt; . Thanks for your time and make sure to follow me or drop your comment below&lt;/em&gt; 👇&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
    </item>
    <item>
      <title>The “let” keyword in swift</title>
      <dc:creator>Agoi Abel Adeyemi</dc:creator>
      <pubDate>Mon, 08 Jan 2018 08:12:05 +0000</pubDate>
      <link>https://dev.to/abelagoi/the-let-keyword-inswift-2ha5</link>
      <guid>https://dev.to/abelagoi/the-let-keyword-inswift-2ha5</guid>
      <description>&lt;p&gt;In swift, we use the &lt;code&gt;let&lt;/code&gt; keyword to declare a constant variable, a constant is a variable that once declared, the value can not be changed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Declaring constants
&lt;/h3&gt;

&lt;p&gt;We can declare a variable in swift like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myAge: Int = 27
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the &lt;code&gt;let&lt;/code&gt; keyword above, we use it to denote that &lt;code&gt;myAge&lt;/code&gt; is a constant and should hold the integer &lt;code&gt;27&lt;/code&gt;. Unlike variable declared using the &lt;code&gt;var&lt;/code&gt; keyword, we cannot change the value of a constant once it has been assigned.&lt;/p&gt;

&lt;p&gt;A constant is very similar to a variable except it value cannot be changed when declared.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;It’s important that you keep the following in mind. You can declare a constant with the &lt;code&gt;let&lt;/code&gt; keyword, constant have the same properties as a variable (var) which include type annotation, naming convention etc., except once a value has been assigned, the value cannot be changed&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
    </item>
  </channel>
</rss>
