William Hu William Hu - 7 months ago 43
Swift Question

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

I want to add a class function into extension:

extension String {
class func test () {
}
}


I get the error:
Class methods are only allowed within classes; use 'static' to declare a static method


Or how should i call "
String.test()
"

But for NSString

extension NSString {
class func aaa () {
}
}


no errors.

If i add static keyword:

extension String {
static func aaa () {
self.stringByAppendingString("Hello")
}
}


Got:
Expression resolves to an unused function
,

So how should i add a class function also want to use
self.
method.

EDIT: This works!

extension String {
static func aaa (path:String) -> String {
return path.stringByAppendingString("Hello")
}
}


but about @lan's answer:

mutating func bbb(path: String) {
self += "world"
}


When i type it appears like this:

String.bbb(&<#String#>)
String.bbb(&"nihao")

Cannot invoke 'bbb' with an argument list of type '(String)'

Ian Ian
Answer

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() // 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
Comments