<?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>Forem: Jaume Viñas Navas</title>
    <description>The latest articles on Forem by Jaume Viñas Navas (@jaumevn).</description>
    <link>https://forem.com/jaumevn</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%2F195706%2F0f53349d-e676-472c-9857-0978ba641677.jpg</url>
      <title>Forem: Jaume Viñas Navas</title>
      <link>https://forem.com/jaumevn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jaumevn"/>
    <language>en</language>
    <item>
      <title>Parsing JSON with Swift 5</title>
      <dc:creator>Jaume Viñas Navas</dc:creator>
      <pubDate>Thu, 03 Oct 2019 10:51:47 +0000</pubDate>
      <link>https://forem.com/jaumevn/parsing-json-with-swift-5-2m40</link>
      <guid>https://forem.com/jaumevn/parsing-json-with-swift-5-2m40</guid>
      <description>&lt;p&gt;There are great libraries out there that help you parse JSON data, but it is great to see how Swift 5 implements a fully-native solution to make your data types encodable and decodable for compatibility with JSON.&lt;/p&gt;

&lt;p&gt;There are three new protocols in the Swift standard library that define an standardized approach to data encoding and decoding. These new protocols are &lt;code&gt;Encodable&lt;/code&gt;, &lt;code&gt;Decodable&lt;/code&gt; and &lt;code&gt;Codable&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Encoding and Decoding
&lt;/h1&gt;

&lt;p&gt;The easiest way to encode or decode a type is to use standard properties that are already &lt;code&gt;Codable&lt;/code&gt;, which can be types like &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;Double&lt;/code&gt;, &lt;code&gt;Data&lt;/code&gt;, &lt;code&gt;Date&lt;/code&gt; and &lt;code&gt;URL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let’s consider a &lt;code&gt;User&lt;/code&gt; structure that stores the name, the username and the phone number of a user.&lt;/p&gt;


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


&lt;p&gt;We can make it automatically decodable and encodable by conforming to the &lt;code&gt;Codable&lt;/code&gt; protocol, which is a type alias of Encodable and Decodable.&lt;/p&gt;


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


&lt;p&gt;We can also use custom types that also conform to the &lt;code&gt;Codable&lt;/code&gt; protocol, and use built-in types such as &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Dictionary&lt;/code&gt; and &lt;code&gt;Optional&lt;/code&gt; whenever they contain codable types.&lt;/p&gt;

&lt;p&gt;Let’s add to the previous example a list of devices that the user owns. It is represented by an &lt;code&gt;Array&lt;/code&gt; of &lt;code&gt;Device&lt;/code&gt;. The &lt;code&gt;Device&lt;/code&gt; stores the name and the manufacturer of a device.&lt;/p&gt;


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


&lt;p&gt;We can now deserialize a JSON document into our &lt;code&gt;User&lt;/code&gt; instance, and serialize that instance back into a JSON document.&lt;/p&gt;


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


&lt;h1&gt;
  
  
  Encode or Decode Exclusively
&lt;/h1&gt;

&lt;p&gt;In some cases, you may not need to decode and encode the same type. For example, you may only need to read data and decode them into an instance, or you just need to make API calls and there is no need to decode the response containing the same type.&lt;/p&gt;

&lt;p&gt;You can achieve that by conforming &lt;code&gt;Encodable&lt;/code&gt; and &lt;code&gt;Decodable&lt;/code&gt; protocols. The example below shows how the &lt;code&gt;User&lt;/code&gt; structure only encode data:&lt;/p&gt;


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


&lt;h1&gt;
  
  
  Custom Key Names
&lt;/h1&gt;

&lt;p&gt;It is often the case that we parse JSON documents with properties that doesn’t match our property names. In that case, we need to define custom key names in order to parse these properties into our instances.&lt;/p&gt;

&lt;p&gt;In order to define the custom list of key names we need to define an enumeration named &lt;code&gt;CodingKeys&lt;/code&gt; which conforms to the &lt;code&gt;CodingKeys&lt;/code&gt; protocol. The names of the enumeration cases should match the names you’ve given to the corresponding properties in your type.&lt;/p&gt;

&lt;p&gt;Let’s update the property &lt;code&gt;phoneNumber&lt;/code&gt; of our &lt;code&gt;User&lt;/code&gt; structure, which is defined in the JSON document using snake-case style. The enumeration &lt;code&gt;CodingKeys&lt;/code&gt; provide the &lt;code&gt;phoneNumber&lt;/code&gt; alternative key specified by a &lt;code&gt;String&lt;/code&gt; as the raw-value type for the &lt;code&gt;CodingKeys&lt;/code&gt; enumeration.&lt;/p&gt;


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





&lt;p&gt;If you find this post helpful, please recommend it for others to read.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>mobile</category>
      <category>json</category>
    </item>
    <item>
      <title>Swift Enumerations (Part 3)</title>
      <dc:creator>Jaume Viñas Navas</dc:creator>
      <pubDate>Mon, 30 Sep 2019 07:01:52 +0000</pubDate>
      <link>https://forem.com/jaumevn/swift-enumerations-part-3-137o</link>
      <guid>https://forem.com/jaumevn/swift-enumerations-part-3-137o</guid>
      <description>&lt;p&gt;Swift enumerations are a first-class types in their own right, that means they can adopt many features traditionally supported only by classes. In this chapter we will cover how enumeration can define custom initializers to provide an initial case value, and how they can be extended and conform to protocols.&lt;/p&gt;

&lt;p&gt;Remember to read the first chapters (&lt;a href="https://dev.to/jaumevn/swift-enumerations-part-1-4057"&gt;Part 1&lt;/a&gt; and &lt;a href="https://dev.to/jaumevn/swift-enumerations-part-2-2nlf"&gt;Part 2&lt;/a&gt;) if you missed them or you want to refresh the basic concepts of Swift enums.&lt;/p&gt;

&lt;h1&gt;
  
  
  Protocols
&lt;/h1&gt;

&lt;p&gt;A protocol defines an interface of methods, properties and other requirements that can be adopted by a class, structure or enumeration. Any type that satisfies the requirement of a protocol must provide an implementation of those requirements.&lt;/p&gt;

&lt;p&gt;For demonstration purposes we use the &lt;code&gt;LayerActions&lt;/code&gt; enumeration declared in the previous &lt;a href=""&gt;chapter&lt;/a&gt;, which defines a set of actions that users can execute in order to modify a layer in a design tool. To execute and describe each action we declare the protocol &lt;code&gt;Taskable&lt;/code&gt;, which defines a method to execute a task and a description property that defines the task to be executed.&lt;/p&gt;


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


&lt;p&gt;As you can see, the enumeration now conforms to the protocol requirements in the enumeration by implementing the method &lt;code&gt;execute()&lt;/code&gt; and the computed property &lt;code&gt;description&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Extensions
&lt;/h1&gt;

&lt;p&gt;An extension adds new functionality to an existing class, structure, enumeration or protocol type. You can use an extension to add computed properties, define methods, define initializers, or make the existing type conform to a protocol among others.&lt;/p&gt;

&lt;p&gt;In our case, we will extend the &lt;code&gt;LayerActions&lt;/code&gt; enumeration to conform the protocol &lt;code&gt;Taskable&lt;/code&gt; and make it easier to read, understand and maintain the code.&lt;/p&gt;


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


&lt;p&gt;We can add as many extensions as we want, for instance, we can extend the previous enumeration in order to implement a different protocol. Here’s an example that extends the enumeration twice to separate the implementation of protocols &lt;code&gt;Taskable&lt;/code&gt; and &lt;code&gt;Serializable&lt;/code&gt;.&lt;/p&gt;


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


&lt;h1&gt;
  
  
  Initializers
&lt;/h1&gt;

&lt;p&gt;Enumerations with a raw-value type automatically receive an initializer that takes as a parameter a value of the raw value’s type. Here’s an example of an enumeration with a raw-value type that uses the initializer to create a new instance.&lt;/p&gt;


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


&lt;p&gt;Initilizers return either an enumeration case or &lt;code&gt;nil&lt;/code&gt;. As you can see in the following example, the variable &lt;code&gt;q&lt;/code&gt; is of type &lt;code&gt;Polygon?&lt;/code&gt; and the value returned by the raw initializer is &lt;code&gt;nil&lt;/code&gt;.&lt;/p&gt;


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


&lt;p&gt;You can also create custom initializers even if the enumeration is not a raw-value typed enumeration.&lt;/p&gt;


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





&lt;p&gt;If you find this post helpful, please recommend it for others to read.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Swift Enumerations (Part 2)</title>
      <dc:creator>Jaume Viñas Navas</dc:creator>
      <pubDate>Fri, 27 Sep 2019 09:26:44 +0000</pubDate>
      <link>https://forem.com/jaumevn/swift-enumerations-part-2-2nlf</link>
      <guid>https://forem.com/jaumevn/swift-enumerations-part-2-2nlf</guid>
      <description>&lt;p&gt;In this new chapter we will cover more advanced features about Swift enumerations, remember to &lt;a href="https://dev.to/jaumevn/swift-enumerations-part-1-4057"&gt;read&lt;/a&gt; the first chapter if you missed it or you want to refresh the basic concepts of Swift enums.&lt;/p&gt;

&lt;h1&gt;
  
  
  Associated Values
&lt;/h1&gt;

&lt;p&gt;Enumeration cases have their own value and type, and sometimes it is useful to store additional data of other types alongside an enum case. Associated values are a fantastic way to achieve that by attaching custom information along with the case value, allowing this information to be different each time you use that enum case in your code.&lt;/p&gt;

&lt;p&gt;Here’s an example of an enumeration that defines a set of actions that users can execute in order to modify a layer. A layer can be resized, rotated and filled with a color. The enum case &lt;code&gt;resize&lt;/code&gt; has two associated values that define the new width and height values of the layer, the enum case &lt;code&gt;rotate&lt;/code&gt; has one associated value which indicates the rotation degrees, and the enum case &lt;code&gt;fill&lt;/code&gt; has one associated value that indicates the color of the layer.&lt;/p&gt;


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


&lt;p&gt;The associated values can be accessed using a switch statement, and they can be extracted as constants (with the &lt;code&gt;let&lt;/code&gt; prefix) or as variables (with the &lt;code&gt;var&lt;/code&gt; prefix).&lt;/p&gt;


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


&lt;h1&gt;
  
  
  Methods
&lt;/h1&gt;

&lt;p&gt;Enumerations in Swift are first-class types in their own right. They adopt many features traditionally supported only by classes, for example, instance methods to provide functionality related to the values the enumeration represents.&lt;/p&gt;

&lt;p&gt;Methods on enumerations exist for every enum case. If you want to provide specific code for every enum case, you need a switch statement to determine each case.&lt;/p&gt;


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


&lt;h1&gt;
  
  
  Mutating Methods
&lt;/h1&gt;

&lt;p&gt;By default, the properties of an enumeration cannot be modified from within its instance methods. However, if you need to modify your enumeration you can use mutating methods. Mutating methods for enumerations can set the implicit self parameter to be a different case from the same enumeration.&lt;/p&gt;

&lt;p&gt;Here’s an example that defines an enumeration for a temperature alarm with two states, &lt;code&gt;enabled&lt;/code&gt; and &lt;code&gt;disabled&lt;/code&gt;. The enumeration changes its state when the temperature reaches an specific threshold.&lt;/p&gt;


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


&lt;h1&gt;
  
  
  Properties
&lt;/h1&gt;

&lt;p&gt;Event though enumerations can’t have stored properties, they can have computed properties to provide additional information about the enumeration’s current value.&lt;/p&gt;

&lt;p&gt;The example below modifies the &lt;code&gt;Polygon&lt;/code&gt; enumeration in order to add a new property &lt;code&gt;edges&lt;/code&gt; . This new property returns the number of edges of the enum case. The method &lt;code&gt;description()&lt;/code&gt; has also been updated in order to use the new property.&lt;/p&gt;


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





&lt;p&gt;If you find this post helpful, please recommend it for others to read. In the next chapter we will cover more advanced enumeration usages such as protocols, extensions, generics and custom initializers.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Swift Enumerations (Part 1)</title>
      <dc:creator>Jaume Viñas Navas</dc:creator>
      <pubDate>Thu, 26 Sep 2019 09:57:28 +0000</pubDate>
      <link>https://forem.com/jaumevn/swift-enumerations-part-1-4057</link>
      <guid>https://forem.com/jaumevn/swift-enumerations-part-1-4057</guid>
      <description>&lt;p&gt;An enumeration defines a custom data type for a set of related values. Swift enumerations are much more flexible and powerful than we got used to in other languages. Enumerations in Swift are first-class type in their own right, they support computed properties, instance methods, custom initializers, extensions and can conform to protocols.&lt;/p&gt;

&lt;h1&gt;
  
  
  Syntax
&lt;/h1&gt;

&lt;p&gt;Swift enumerations are introduced with the enum keyword and writing their entire definition within a pair of braces. Here's an example that defines some different types of polygons:&lt;/p&gt;


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


&lt;p&gt;Multiple cases can be arranged on a single line separated by comas:&lt;/p&gt;


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


&lt;p&gt;You can match enumerations with various pattern matching constructs to retrieve the value of the enum, or act upon a specific case. Here's an example that prints the number of sides of the polygon for an specific case:&lt;/p&gt;


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


&lt;p&gt;A switch declaration must be exhaustive when considering an enumeration's cases. If one of the cases of the enum is omitted in the switch declaration the code will not compile. If you don't need to provide a case for every enumeration case, you can provide a default case:&lt;/p&gt;


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


&lt;p&gt;When a variable is initialized with one of the possible values of an enumeration, the type is automatically inferred. In the following example, the variable figure contains the enum value triangle and so it automatically infers its type, Polygon. We can then set it to a different Polygon value using dot syntax:&lt;/p&gt;


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


&lt;h1&gt;
  
  
  Raw Values
&lt;/h1&gt;

&lt;p&gt;Enumerations can have a value assigned to each case. Swift supports Integer, Floating Point, String and Boolean types for the value of an enum. You can access to the value assigned to each enumeration case with the rawValue property.&lt;/p&gt;


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


&lt;h1&gt;
  
  
  Nested enums
&lt;/h1&gt;

&lt;p&gt;Enums can be nested in order to specify sub types. To nest a type within another type you must declare its entire definition within the outer pair of braces.&lt;/p&gt;

&lt;p&gt;Following the previous example, we can consider two new specific sub types for the triangle enum case. Triangles can be classified by various properties relating to their angles and sides. Here's an example that classifies triangles by their sides:&lt;/p&gt;


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


&lt;p&gt;Nested types can be used outside its definitions context using the dot syntax. You must prefix its name with the name of the type it is nested within:&lt;/p&gt;


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





&lt;p&gt;If you find this post helpful, please recommend it for others to read. In the next chapter we will cover more advanced features such as associated values, methods and properties.&lt;/p&gt;

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