Swift: how to add a class method to the extension "String"

I want to add a class function to the extension:

extension String { class func test () { } } 

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

Or how can I call " String.test() "

But for NSString

 extension NSString { class func aaa () { } } 

no mistakes.

If I add a static keyword:

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

Got: Expression resolves to an unused function ,

So, as I have to add a class function, I also want to use the self. method self. .

EDIT: It works!

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

but about @lan answer:

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

When I type, it looks like this:

 String.bbb(&<#String#>) String.bbb(&"nihao") Cannot invoke 'bbb' with an argument list of type '(String)' 
+6
source share
3 answers

Class and static functions are called not for an instance of the class / structure, but for the class / structure itself, so you cannot just add a line to the class.

Apple Documentation :

In the body of a type method, the implicit property self refers to the type itself, and not to an instance of this type.

However, you can add a string to an instance of the String variable 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 modify the entered string:

 extension String { static func aaa(inout path: String) { path += "Hello" } } var foo = "someText" String.aaa(&foo) foo //someTextHello 
+9
source

"Inside the body of a type method, the implicit property of self refers to the type itself, and not to an instance of that type."

Thus, when you extend a type by adding a type method, you can only call other type methods through self . If you want to call an instance method, you need to create an instance and call the method.

0
source

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

Thus, the struct extension for returning new instances is much more common. So this is typical:

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

and then on the call site:

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

You will notice, of course, that I do not use static at all. This is because for appending(_:) it is necessary to use the current value of the String instance we add, and class / static do not refer to the instances and therefore have no values.

0
source

Source: https://habr.com/ru/post/987708/


All Articles