Updated 19 July 2024
Hello Readers, today in our swift learning journey we will explore a very powerful tool of Swift “Extensions”.
With modern swift programming, extensions stand as a powerful feature that allows developers to augment the functionality of existing types without altering their original implementation. Even if you’re building iOS, macOS, watchOS, or tvOS applications, understanding the effectiveness of extensions can significantly improve your codebase, its reusability and maintainability.
In general terms, Extension in Swift enable developers to add new functionalities to existing class, struct, enum, or protocol types without altering their source code or their purposes. This feature is mostly used for extending the behaviour of types that developers may not have direct access to or wish to keep separate from their primary codebase.
In swift programming, the extensions are so flexible to use and you can define the extensions anywhere in your project but outside the class. You can define the extension in swift like below:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Extension of UIColor extension UIColor { } // Extension of UITextField extension UITextField { } // Extension of UINavigationBar extension UINavigationBar { } // Extension of UIView extension UIView { } |
For instance, if we want to add some extra functionality or add default properties on the above extension. We can simply do this like below:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// Extension for setting bottom border in UITextField extension UITextField { func bottomBorder(texField: UITextField) { let topBorder = CALayer() topBorder.frame = CGRect(x: 0, y: 49, width: texField.frame.size.width - 10, height: 1) topBorder.backgroundColor = UIColor.gray.cgColor texField.layer.addSublayer(topBorder) } } // Extension for converting Hexadecimal color codes to UIColor in UIColor extension UIColor { func HexToColor(hexString: String, alpha: CGFloat? = 1.0) -> UIColor { // Convert hex string to an integer let hexint = Int(self.intFromHexString(hexStr: hexString)) let red = CGFloat((hexint & 0xff0000) >> 16) / 255.0 let green = CGFloat((hexint & 0xff00) >> 8) / 255.0 let blue = CGFloat((hexint & 0xff)) / 255.0 let alpha = alpha! // Ensure alpha value is non-nil // Create color object, specifying alpha as well let color = UIColor(red: red, green: green, blue: blue, alpha: alpha) return color } func intFromHexString(hexStr: String) -> UInt32 { var hexInt: UInt32 = 0 // Create scanner let scanner: Scanner = Scanner(string: hexStr) // Tell scanner to skip the # character scanner.charactersToBeSkipped = NSCharacterSet(charactersIn: "#") as CharacterSet // Scan hex value scanner.scanHexInt32(&hexInt) return hexInt } } |
Once you define extensions in Swift, you can use their added functions or properties just like you would with any methods or properties of standard classes.
1 2 3 4 5 6 7 |
// Color Extension: Setting text color using hexadecimal code title.textColor = UIColor().HexToColor(hexString: "#333333") // Border Extension: Adding a bottom border to a UITextField UITextField().bottomBorder(texField: name) |
In conclusion, extension in swift represent a pivotal feature that provides an extra boost and opportunity for developers to expand the capabilities of existing types without altering their original source code.
Throughout this exploration, we’ve seen how extensions enhance code modularity, promote reusability, and improve clarity in Swift applications.
You can explore out Flutter Developement services here.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.