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?
- The most common and basic situation is when we want to add methods (Instance Methods & Type Methods) to an existing class.
- Provide some additional ways to initialize objects (Initializer).
- Meet one or more protocols (Adapt Protocol).
- Add computed properties.
- 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
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
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.
- And the manual is as simple as downloading it from the github link: https://github.com/SwifterSwift/SwifterSwift and then drag it to the project and use.
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