Junior — Middle
What elements can be extended using extension in programming language?
sobes.tech AI
Answer from AI
In Swift language, extensions can be used to extend the following elements:
- Classes
- Structures
- Enumerations (enum)
- Protocols
Extensions allow adding new methods, computed properties, initializers, subscripts, and protocol conformance without modifying the original source code of the type.
Example of an extension to add a method to the String type:
extension String {
func reversedString() -> String {
return String(self.reversed())
}
}
let text = "hello"
print(text.reversedString()) // output: "olleh"