I want to create category of my existing swift class, but there is no option in IDE to do so.
Any idea if category exists in swift project? Or how to achieve similar functionality in swift project?
Category and extension both are basically made to handle large code base, but category is a way to extend class API in multiple source files while extension is a way to add required methods outside the main interface file.
In Swift, you define a structure or class in a single file, and the external interface to that class or structure is automatically made available for other code to use. An instance of a class is traditionally known as an object.
Swift classes that are inherited from OBJC classes are bridged automatically. That means any class inherited from, for example, UIViewController is automatically seen in the OBJC runtime. If you're creating a class that doesn't inherit from anything, then make it an NSObject subclass, as you would in OBJC.
We can't add the stored properties to extensions directly but we can have the computed variables . Extensions in Swift can: Add computed instance properties and computed type properties.
In Swift, you can use Extensions
to add new functionality to existing classes, structs and enumeration types.
They differ from Objective-C categories in a few ways, mainly:
As it stands today, Extensions can:
The basic syntax to declare an extension is as follows:
extension SomeType { // new functionality to add to SomeType goes here }
Check Apple's documentation for more information on how to use Extensions in Swift.
In Objective C they were called categories, but in Swift they are called extensions. The purpose of both of them are to give additional functionality to existing classes without having to create subclasses.
I had read about extensions in the documentation, but I didn't really understand how to use one in my project until I watched this tutorial video (YouTube version, github source).
Here is a summary taken from the video of how to do it.
Right click in the Project Navigator and choose "New File..."
Select "Swift File"
The convention is to save the file name as the class name you are extending plus (with a "+" sign) what you are doing to it. For example, "UIImage+Cropping".
Open the new Swift file that you just created.
You should import UIKit
(instead of Foundation
) if you are extending a UIView
. Then use the extension
keyword before the class name that you want to extend. You can then add your own new methods to the class. (Note, extensions are for adding new methods, not overriding existing methods--hence the name.)
In the video, the example was to add a method that crops a circle from the image and gives it a border.
import UIKit extension UIImage { func cropToCircleWithBorderColor(color: UIColor, lineWidth: CGFloat) -> UIImage { // code to create the cropped circle with colored border return newImage } }
See here for the full example.
Now you can use your new method for that class anywhere in your code, just like it was part of the standard class.
Here is the video's example (on github):
import UIKit class ViewController: UIViewController { @IBOutlet var imageView : UIImageView = nil override func viewDidLoad() { super.viewDidLoad() let img = imageView.image imageView.image = img.cropToCircleWithBorderColor(UIColor(red:0.91, green:0.34, blue:0.16, alpha:1.00), lineWidth: 20) } }
The method cropToCircleWithBorderColor
is not a standard part of UIImage
, but as you can see, it is used just like it were.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With