<?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: Giuseppe Travasoni</title>
    <description>The latest articles on DEV Community by Giuseppe Travasoni (@neobeppe).</description>
    <link>https://dev.to/neobeppe</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%2F94895%2F90494f2f-e3a4-4387-830d-48b848f8f52a.png</url>
      <title>DEV Community: Giuseppe Travasoni</title>
      <link>https://dev.to/neobeppe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/neobeppe"/>
    <language>en</language>
    <item>
      <title>Exploring Dynamic Method Injection in Swift</title>
      <dc:creator>Giuseppe Travasoni</dc:creator>
      <pubDate>Wed, 06 Dec 2023 15:25:07 +0000</pubDate>
      <link>https://dev.to/neobeppe/exploring-dynamic-method-injection-in-swift-5bno</link>
      <guid>https://dev.to/neobeppe/exploring-dynamic-method-injection-in-swift-5bno</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hello Swift enthusiasts! In today's article, we're diving into an intriguing aspect of Swift programming - dynamic method injection. This technique allows developers to add methods to classes or objects at runtime, offering a flexible approach to managing functionalities. We'll explore this concept through a code snippet that injects a method from one class into another.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Method Injection?
&lt;/h2&gt;

&lt;p&gt;Method Injection is a process where you dynamically add new methods to a class or an instance of a class at runtime. This is not typically a Swift-native approach, as Swift emphasizes type safety and compile-time checks. However, Swift's interoperability with Objective-C runtime offers a backdoor to achieve this.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Code Breakdown
&lt;/h2&gt;

&lt;p&gt;Here's a brief overview of the code snippet we're examining:&lt;/p&gt;

&lt;h3&gt;
  
  
  Class Definitions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ClassA&lt;/code&gt;: Contains a &lt;code&gt;hello()&lt;/code&gt; method that prints the class's name. &lt;code&gt;hello()&lt;/code&gt; method must have &lt;code&gt;@objc&lt;/code&gt; attribute because it will be user with Objective-C Runtime soon.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ClassB&lt;/code&gt;: An empty class without any methods.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ClassA&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;@objc&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, i'm "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;describing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ClassB&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Method Injection Function
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useMethod(sel:fromClass:inObject:)&lt;/code&gt;: Injects the method from &lt;code&gt;fromClass&lt;/code&gt; into &lt;code&gt;inObject&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It employs &lt;code&gt;class_getInstanceMethod&lt;/code&gt; and &lt;code&gt;unsafeBitCast&lt;/code&gt; to dynamically add the method to the object.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;useMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;sel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Selector&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;fromClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AnyClass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;inObject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AnyObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;meth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;class_getInstanceMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromClass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;typealias&lt;/span&gt; &lt;span class="kt"&gt;ClosureType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;@convention&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;AnyObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Selector&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;imp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;method_getImplementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;meth&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;callMethod&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ClosureType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;unsafeBitCast&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;imp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ClosureType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;callMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;An instance of &lt;code&gt;ClassA&lt;/code&gt; is created and its &lt;code&gt;hello()&lt;/code&gt; method is called.&lt;/li&gt;
&lt;li&gt;An instance of &lt;code&gt;ClassB&lt;/code&gt; is then created, and the &lt;code&gt;hello&lt;/code&gt; method from &lt;code&gt;ClassA&lt;/code&gt; is injected and executed on it using &lt;code&gt;useMethod&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;ClassA&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;ClassB&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;useMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;sel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;#selector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;ClassA&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nv"&gt;fromClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;ClassA&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;inObject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deep Dive into the Function
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useMethod&lt;/code&gt; function is where the magic happens. Let’s dissect it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It first checks if the method exists in the source class using &lt;code&gt;class_getInstanceMethod&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Once the method is obtained, it retrieves its implementation (&lt;code&gt;IMP&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;This implementation is then cast to a closure of type &lt;code&gt;@convention(c) (AnyObject, Selector) -&amp;gt; Void&lt;/code&gt; using &lt;code&gt;unsafeBitCast&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Finally, the method is invoked on the target object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find full playground in &lt;a href="https://gist.github.com/neobeppe/a3a9021299b1ee21afa687c65a762a59"&gt;this gist&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is This Important?
&lt;/h2&gt;

&lt;p&gt;This method showcases an advanced level of Swift's capabilities, blending Swift's modern features with the underlying Objective-C runtime. It demonstrates how Swift can handle dynamic behavior, a feature often associated with more dynamic languages like Python or JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cautions
&lt;/h2&gt;

&lt;p&gt;While powerful, method injection should be used judiciously. It bypasses Swift's type safety and can lead to unpredictable behaviors or crashes if not handled properly. Always ensure that the method and the target object are compatible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Dynamic method injection in Swift, while not a common practice, opens doors to a range of possibilities for developers willing to explore the depths of the language. It exemplifies Swift's versatility and its seamless blend with Objective-C runtime, offering a unique approach to solving complex programming challenges.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>advanced</category>
      <category>development</category>
    </item>
    <item>
      <title>Enhancing iOS App Usability: The Critical Role of Accessibility in Labels and Fonts</title>
      <dc:creator>Giuseppe Travasoni</dc:creator>
      <pubDate>Tue, 05 Dec 2023 16:01:10 +0000</pubDate>
      <link>https://dev.to/neobeppe/enhancing-ios-app-usability-the-critical-role-of-accessibility-in-labels-and-fonts-21oh</link>
      <guid>https://dev.to/neobeppe/enhancing-ios-app-usability-the-critical-role-of-accessibility-in-labels-and-fonts-21oh</guid>
      <description>&lt;p&gt;As developers, we often strive to create iOS apps that are not only functional but also visually appealing. However, it's crucial to remember that design is more than just aesthetics; it's about inclusivity and accessibility. In this regard, the choice of labels and fonts in our apps plays a pivotal role.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Understanding the Importance of Accessibility
&lt;/h2&gt;

&lt;p&gt;Accessibility ensures that our apps are usable by people with a wide range of abilities and disabilities. This includes individuals with visual impairments, learning disabilities, or those who face challenges in reading or comprehending text. By focusing on accessible labels and fonts, we make our apps more user-friendly for everyone.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Labels: More Than Just Text
&lt;/h2&gt;

&lt;p&gt;Labels in an app are not just placeholders for text; they convey critical information. For users relying on screen readers like VoiceOver, the labels need to be descriptive and clear. This means avoiding vague labels like 'button' and instead using specific descriptions like 'submit form button'.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The Power of Fonts
&lt;/h2&gt;

&lt;p&gt;The choice of font can greatly affect readability. Fonts that are too stylized may look appealing but can be hard to read for many users. Opting for clear, legible fonts with adjustable sizes allows users with visual impairments to read text comfortably. Additionally, providing options for high-contrast text can be a game-changer for those with low vision.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Best Practices for Accessible Labels and Fonts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use straightforward, descriptive language for labels.&lt;/li&gt;
&lt;li&gt;Choose fonts that are easy to read and support a range of sizes.&lt;/li&gt;
&lt;li&gt;Test your app with VoiceOver to ensure labels are read correctly.&lt;/li&gt;
&lt;li&gt;Consider the color contrast between text and background.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. The Impact on User Experience
&lt;/h2&gt;

&lt;p&gt;Focusing on accessibility in labels and fonts does more than just comply with guidelines; it enhances the overall user experience. Accessible design often leads to cleaner, more intuitive interfaces that benefit all users, not just those with disabilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Embracing Inclusivity in Design
&lt;/h2&gt;

&lt;p&gt;Incorporating accessibility into our design process is a step towards embracing inclusivity. It shows a commitment to providing an equitable user experience and acknowledges the diversity of our user base.&lt;/p&gt;

&lt;p&gt;In conclusion, the importance of accessibility in labels and fonts in iOS apps cannot be overstated. It's not just a best practice; it's a necessity for creating apps that are truly for everyone. As developers, let's commit to making accessibility a cornerstone of our design process.&lt;/p&gt;




&lt;p&gt;Are you implementing Dynamic Type in your app? In the next few days I will publish a post about using Dynamic Type with custom fonts!&lt;/p&gt;

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