Swift:如何在'String'扩展中添加类方法

时间:2022-04-18 23:59:31

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

我得到错误:类方法只允许在类中;使用'static'来声明静态方法

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

或者我该如何调用“String.test()”

But for NSString

但是对于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.

那么我应该如何添加一个类函数也想使用self。方法。

EDIT: This works!

编辑:这有效!

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

but about @lan's answer:

但关于@ lan的回答:

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)'

2 个解决方案

#1


7  

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.

在类型方法的主体内,隐式self属性指的是类型本身,而不是该类型的实例。

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

但是,您可以使用mutating关键字将字符串附加到String的变量实例:

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:

如果您尝试使用指向字符串的指针作为参数,则可以使用inout关键字来更改输入的字符串:

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

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

#2


1  

"Within the body of a type method, the implicit self property refers to the type itself, rather than 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 a method on that.

因此,当您通过添加类型方法扩展类型时,您只能通过self调用其他类型的方法。如果要调用实例方法,则需要创建实例并在其上调用方法。

#1


7  

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.

在类型方法的主体内,隐式self属性指的是类型本身,而不是该类型的实例。

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

但是,您可以使用mutating关键字将字符串附加到String的变量实例:

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:

如果您尝试使用指向字符串的指针作为参数,则可以使用inout关键字来更改输入的字符串:

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

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

#2


1  

"Within the body of a type method, the implicit self property refers to the type itself, rather than 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 a method on that.

因此,当您通过添加类型方法扩展类型时,您只能通过self调用其他类型的方法。如果要调用实例方法,则需要创建实例并在其上调用方法。