Swift: How to add a class method in 'String" extension

Class and static functions are not called on an instance of a class/struct, but on the class/struct itself, so you can't just append a string to a class.

Apple Documentation:

Within the body of a type method, the implicit self property refers to the type itself, rather than an instance of that type.

You can, however, append a string to a variable instance of a String using the mutating keyword:

extension String {
    mutating func aaa() {
        self += "hello"
    }
}

let foo = "a"
foo.aaa() // ERROR: Immutable value of type 'String' only has mutating members named 'aaa'

var bar = "b"
bar.aaa() // "bhello"

If you are trying to use a pointer to a string as a parameter, you can use the inout keyword to alter the inputed string:

extension String {
    static func aaa(inout path: String) {
        path += "Hello"
    }
}

var foo = "someText"
String.aaa(&foo)
foo //someTextHello

While correct, it's somewhat atypical to see a mutating member added to a String extension as shown in Ian's answer. Strings (and value types in general) are meant to be immutable so the only way to use a mutating method is to declare instances var at the call site. Most of the time in your code you should be using let constants.

As such, it is much more common to extend structs to return new instances. So this is typical:

extension String {
    func appending(_ string: String) -> String {
        return self + string
    }
}

and then at the call site:

let hello = "Hello, "
let helloWorld = hello.appending("World!")

You'll note of course that I'm not using static at all. That's because appending(_:) needs to use the current instance value of the String we're appending to, and class/static do not refer to instances and therefore do not have values.

Tags:

Swift