SwifterSwift: Extensions for all

Tram Ho

I. Introduction

Extensions allow us to add new methods to an existing class, without creating a derived class. Swift Extensions are like Categories in Objective-C, but more concise and more powerful. So when do I need to use Extensions?

  1. The most common and basic situation is when we want to add methods (Instance Methods & Type Methods) to an existing class.
  2. Provide some additional ways to initialize objects (Initializer).
  3. Meet one or more protocols (Adapt Protocol).
  4. Add computed properties.
  5. Extending the Protocol (Protocol)

In which cases 1 and 4 are 2 cases that we have used quite a lot. The effects of Extensions are undeniable, moreover in each project we always pay special attention to Extensions and the proof is always a separate folder to manage it. Over the years I have had an Extensions folder with quite a large number of files, for all different objects. NSLayoutContraints, UILabel, UIButton, UIColor, UIView … Because all the methods and properties are very useful and can be used many times, I brought the folder from project to another project to add the required methods. set. But nothing is enough, true to the name Extentions ? (extend). It keeps on forever, wide and forever but has not ended. Every time I add a new project, I go to that folder to add methods, properties, and objects. For a moment of 5 minutes in my life, I thought that was enough for that folder so I could share it on github or anywhere for my colleagues to use. But my perfectionism hasn’t allowed me to do that. Because as I said: I feel that it is never enough! Unexpectedly detected SwifterSwift on github. I came in and there was a slight jealousy. Jealous because it resembles my idea, it shattered my intentions all along. The next jealousy is that SwitfterSwift is a pretty comprehensive collection of Extensions. It consists of 62 files corresponding to 62 different objects.

Inside the files I checked and used. There are many methods like me but there are many methods and properties I do not have. When using SwifterSwift, I feel that my other folder is so valuable ? . Everyone has the same ideas and cherished as me, or kick through SwifterSwift to experience. You must feel the same way I do right now ? ). What’s more special is that when I installed SwifterSwift, I saw that in addition to the Extensions files I mentioned above, there were folders like AppKit, CoreGraphics, CoreLocation, … But there were no files in this version. The author is definitely intending to develop more in the future and promises usefulness for iOS devs.

II. Setting

To install and use SwifterSwift is very simple. Like other frameworks:

  • If you use CocoaPod just add pod ‘SwifterSwift’ to Podfile and then install it.
  • Carthage: Add the “SwifterSwift / SwifterSwift” github to Cartfile
  • Swift Package Manager: You add

import PackageDescription

let package = Package (name: “YOUR_PROJECT_NAME”, targets: [], dependencies: [.package (url: ” https://github.com/SwifterSwift/SwifterSwift.git “, from: “4.0.0”)])

go to Package.swift.

III. Conclude

  • Although there is a downside: Because there are so many objects as well as methods or properties, the fact that we cannot know all the methods or properties we want to add is not yet available.
  • But just need to check and read, the memorization is very simple. I firmly believe that when checking methods in SwifterSwift you will not be surprised because there are many methods that we repeatedly rewrite into a func in a class and forget that we can minimize it by putting them in Extensions. .
  • Wish you have a good experience using SwifterSwift. Here is the swifterSwift github link: https://github.com/SwifterSwift/SwifterSwift
Share the news now

Source : Viblo