Earlier, we wrote an extension for our DataManager; extensions are useful for adding functionality onto standard libraries, structs, or classes—such as arrays, ints, and strings—or onto your data types.
Here is an example. Let's say that you wanted to know the length of a string:
let name = "Craig"
name.characters .count
For us to access the count of the string, we would need to access the characters and then get a count.
Let's simplify this by creating an extension:
extension String {
var length: Int {
return self.characters.count
}
}
With this newly created String extension, we can now access the count by writing the following:
let name = "Craig"
name.length
As you can see, extensions are very powerful by enabling us to add extra functionality without having to change the main class or struct. The...