如何在Swift中声明一个类级别的函数?

时间:2021-04-06 20:53:58

I can't seem to find it in the docs, and I'm wondering if it exists in native Swift. For example, I can call a class level function on an NSTimer like so:

我似乎在文档中找不到它,我想知道它是否存在于本地的Swift中。例如,我可以像这样在NSTimer上调用类级函数:

NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "someSelector:", userInfo: "someData", repeats: true)

But I can't seem to find a way to do it with my custom objects so that I could call it like:

但我似乎找不到一种方法来处理我的自定义对象,这样我就可以这样称呼它:

MyCustomObject.someClassLevelFunction("someArg")

Now, I know we can mix Objective-C w/ Swift and it's possible that the NSTimer class method is a remnant from that interoperability.

现在,我知道我们可以混合Objective-C w/ Swift, NSTimer类方法可能是互操作性的残余。

Question

  1. Do class level functions exist in Swift?

    类级函数在Swift中存在吗?

  2. If yes, how do I define a class level function in Swift?

    如果是,我如何在Swift中定义一个类级别的函数?

5 个解决方案

#1


110  

Yes, you can create class functions like this:

是的,你可以创建这样的类函数:

class func someTypeMethod() {
    //body
}

Although in Swift, they are called Type methods.

虽然在Swift中,它们被称为类型方法。

#2


38  

You can define Type methods inside your class with:

您可以在类中定义类型方法:

class Foo {
    class func Bar() -> String {
        return "Bar"
    }
}

Then access them from the class Name, i.e:

然后从类名中访问它们,即:

Foo.Bar()

In Swift 2.0 you can use the static keyword which will prevent subclasses from overriding the method. class will allow subclasses to override.

在Swift 2.0中,您可以使用静态关键字来防止子类覆盖方法。类将允许子类覆盖。

#3


14  

UPDATED: Thanks to @Logan

更新:由于@Logan

With Xcode 6 beta 5 you should use static keyword for structs and class keyword for classes:

使用Xcode 6 beta 5,你应该对结构使用静态关键字,对类使用类关键字:

class Foo {
    class func Bar() -> String {
        return "Bar"
    }
}

struct Foo2 {
    static func Bar2() -> String {
        return "Bar2"
    }
}

#4


4  

From the official Swift 2.1 Doc:

来自官方的Swift 2.1 Doc:

You indicate type methods by writing the static keyword before the method’s func keyword. Classes may also use the class keyword to allow subclasses to override the superclass’s implementation of that method.

您通过在方法的func关键字之前编写静态关键字来指示类型方法。类也可以使用class关键字来允许子类重写超类的方法实现。

In a struct, you must use static to define a Type method. For classes, you can use either static or class keyword, depending on if you want to allow your method to be overridden by a subclass or not.

在结构中,必须使用静态来定义类型方法。对于类,您可以使用static或class关键字,这取决于您是否希望让子类覆盖方法。

#5


4  

you need to define the method in your class

您需要在类中定义方法

 class MyClass 
 {
       class func myString() -> String
       {
           return "Welcome"
       }
}

Now you can access it by using Class Name eg:

现在您可以使用类名来访问它。

   MyClass.myString()

this will result as "Welcome".

这将是“欢迎”的结果。

#1


110  

Yes, you can create class functions like this:

是的,你可以创建这样的类函数:

class func someTypeMethod() {
    //body
}

Although in Swift, they are called Type methods.

虽然在Swift中,它们被称为类型方法。

#2


38  

You can define Type methods inside your class with:

您可以在类中定义类型方法:

class Foo {
    class func Bar() -> String {
        return "Bar"
    }
}

Then access them from the class Name, i.e:

然后从类名中访问它们,即:

Foo.Bar()

In Swift 2.0 you can use the static keyword which will prevent subclasses from overriding the method. class will allow subclasses to override.

在Swift 2.0中,您可以使用静态关键字来防止子类覆盖方法。类将允许子类覆盖。

#3


14  

UPDATED: Thanks to @Logan

更新:由于@Logan

With Xcode 6 beta 5 you should use static keyword for structs and class keyword for classes:

使用Xcode 6 beta 5,你应该对结构使用静态关键字,对类使用类关键字:

class Foo {
    class func Bar() -> String {
        return "Bar"
    }
}

struct Foo2 {
    static func Bar2() -> String {
        return "Bar2"
    }
}

#4


4  

From the official Swift 2.1 Doc:

来自官方的Swift 2.1 Doc:

You indicate type methods by writing the static keyword before the method’s func keyword. Classes may also use the class keyword to allow subclasses to override the superclass’s implementation of that method.

您通过在方法的func关键字之前编写静态关键字来指示类型方法。类也可以使用class关键字来允许子类重写超类的方法实现。

In a struct, you must use static to define a Type method. For classes, you can use either static or class keyword, depending on if you want to allow your method to be overridden by a subclass or not.

在结构中,必须使用静态来定义类型方法。对于类,您可以使用static或class关键字,这取决于您是否希望让子类覆盖方法。

#5


4  

you need to define the method in your class

您需要在类中定义方法

 class MyClass 
 {
       class func myString() -> String
       {
           return "Welcome"
       }
}

Now you can access it by using Class Name eg:

现在您可以使用类名来访问它。

   MyClass.myString()

this will result as "Welcome".

这将是“欢迎”的结果。