Updated 31 March 2021
Extensions in iOS provide help to add the new functionality of an existing class, structure, or enumeration type.
But overriding the functionality is not possible with extensions.
Extensions in iOS works the same as a category in objective-C.
So extension provides help for confirming the protocol.
Writing of extension is very simple and we can create an extension outside of class.
1 2 3 |
extension type { // new fetaure code } |
-> We can do the below things using the extensions.
A->Â Add compute instance and type work properties.
B-> We can create the type and instance method.
C-> Extension provides the new initializer.
D-> We can define subscripts and nested features.
The extension can adopt more than one protocol.
1 2 3 |
extension SomeType: Protocol, Protocol { // implementation of protocol } |
The extension can add computed type and computed instance property.
we can create multiple functionalities then compute and get the results.
1 2 3 4 5 6 7 8 |
extension String { var length: Int { return self.count } } print("webkul".count) output:- 6 |
so we can create a computed property from the above step.
We can create multiple methods in extensions according to our requirements with multiple types.
This will help us to use this method anywhere with the same datatype.
1 2 3 4 5 6 7 8 9 10 11 |
extension String { func getStringLength()-> int { return self.count } } print("webkul".getStringLength()) output :- 6 |
Initializer use to provide the new and custom initialization of the existing type.
So This enables you to extend other types to accept your own custom types as initializer parameters.
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 |
struct intializerStruct { var width = 100.0 var height = 200.0 } //we can define the structure above and now we initialize through the extension extension intializerStruct { init(widthValue: Double, heightValue: Double) { self.width = widthValue self.height = heightValue } } now access the structure and print the width and height value let obj = intializerStruct(widthValue: 400, heightValue: 500) print("width", obj.width) print("height", obj.height) Output width:- 400.0 height:- 500.0 |
Extension provides a feature that adds a new subscript for an existing type.
This example provides us the string character according to the given index.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
extension String { subscript(accessCharacterThroughIndex i: Int) -> Character { let index = self.index(startIndex, offsetBy: i) return self[index] } } let stringData = "webkul" print("character on 0 index is:- ", stringData[accessCharacterThroughIndex: 0]) output character on 0 index is:- w |
And thanks for reading this blog, for detail info please click here
For more blog please click here.
So pls follow the above step and And if you have any issue or suggestion you can leave your message in the comment section I will try to solve this.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.