Let’s say I want to add the capability to trim white space from the ends of a String. I can write a function:
func trimmed(_ item: String) -> String
{ item.trimmingCharacters(in: .whitespacesAndNewlines) }
let trimmedStr = trimmed(str)
or I can use an extension:
extension String {
func trimmed() -> String {
self.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
let trimmedStr = str.trimmed()
Is there any advantage to using one method over the other?