如何在Swift中创建类方法/属性?

时间:2022-09-11 20:50:12

Class (or static) methods in Objective-C were accomplished using + in declarations.

Objective-C中的类(或静态)方法是在声明中使用+完成的。

@interface MyClass : NSObject

+ (void)aClassMethod;
- (void)anInstanceMethod;

@end

How can this be achieved in Swift?

如何才能在Swift中实现这一目标?

5 个解决方案

#1


132  

They are called type properties and type methods and you use the class or static keywords.

它们被称为类型属性和类型方法,您可以使用类或静态关键字。

class Foo {
    var name: String?           // instance property
    static var all = [Foo]()    // static type property
    class var comp: Int {       // computed type property
        return 42
    }

    class func alert() {        // type method
        print("There are \(all.count) foos")
    }
}

Foo.alert()       // There are 0 foos
let f = Foo()
Foo.all.append(f)
Foo.alert()       // There are 1 foos

#2


19  

They are called type properties and type methods in Swift and you use the class keyword.
Declaring a class method or Type method in swift :

它们在Swift中称为类型属性和类型方法,您可以使用class关键字。在swift中声明类方法或类型方法:

class SomeClass 
{
     class func someTypeMethod() 
     {
          // type method implementation goes here
     }
}

Accessing that method :

访问方法:

SomeClass.someTypeMethod()

or you can refer Methods in swift

或者你也可以用swift的方法。

#3


13  

Prepend the declaration with class if it's a class, or with static if it's a structure.

如果声明是一个类,请在声明前面加上class;如果声明是一个结构,请在声明前面加上static。

class MyClass : {

    class func aClassMethod() { ... }
    func anInstanceMethod()  { ... }
}

#4


4  

Swift 1.1 doesn't have stored class properties. You can implement it using a closure class property that fetches an associated object tied to the class object. (Only works in classes derived from NSObject.)

Swift 1.1没有存储类属性。您可以使用闭包类属性实现它,该属性获取与类对象绑定的关联对象。(只适用于从NSObject派生的类。)

private var fooPropertyKey: Int = 0  // value is unimportant; we use var's address

class YourClass: SomeSubclassOfNSObject {

    class var foo: FooType? {  // Swift 1.1 doesn't have stored class properties; change when supported
        get {
            return objc_getAssociatedObject(self, &fooPropertyKey) as FooType?
        }
        set {
            objc_setAssociatedObject(self, &fooPropertyKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
        }
    }

    ....
}

#5


4  

Prepend the declaration with class or static if it's a function, or with static if it's a property.

如果声明是函数,可以在声明前加上类或静态,如果是属性,可以在声明前加上静态。

class MyClass {

    class func aClassMethod() { ... }
    static func anInstanceMethod()  { ... }
    static var myArray : [String] = []
}

#1


132  

They are called type properties and type methods and you use the class or static keywords.

它们被称为类型属性和类型方法,您可以使用类或静态关键字。

class Foo {
    var name: String?           // instance property
    static var all = [Foo]()    // static type property
    class var comp: Int {       // computed type property
        return 42
    }

    class func alert() {        // type method
        print("There are \(all.count) foos")
    }
}

Foo.alert()       // There are 0 foos
let f = Foo()
Foo.all.append(f)
Foo.alert()       // There are 1 foos

#2


19  

They are called type properties and type methods in Swift and you use the class keyword.
Declaring a class method or Type method in swift :

它们在Swift中称为类型属性和类型方法,您可以使用class关键字。在swift中声明类方法或类型方法:

class SomeClass 
{
     class func someTypeMethod() 
     {
          // type method implementation goes here
     }
}

Accessing that method :

访问方法:

SomeClass.someTypeMethod()

or you can refer Methods in swift

或者你也可以用swift的方法。

#3


13  

Prepend the declaration with class if it's a class, or with static if it's a structure.

如果声明是一个类,请在声明前面加上class;如果声明是一个结构,请在声明前面加上static。

class MyClass : {

    class func aClassMethod() { ... }
    func anInstanceMethod()  { ... }
}

#4


4  

Swift 1.1 doesn't have stored class properties. You can implement it using a closure class property that fetches an associated object tied to the class object. (Only works in classes derived from NSObject.)

Swift 1.1没有存储类属性。您可以使用闭包类属性实现它,该属性获取与类对象绑定的关联对象。(只适用于从NSObject派生的类。)

private var fooPropertyKey: Int = 0  // value is unimportant; we use var's address

class YourClass: SomeSubclassOfNSObject {

    class var foo: FooType? {  // Swift 1.1 doesn't have stored class properties; change when supported
        get {
            return objc_getAssociatedObject(self, &fooPropertyKey) as FooType?
        }
        set {
            objc_setAssociatedObject(self, &fooPropertyKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
        }
    }

    ....
}

#5


4  

Prepend the declaration with class or static if it's a function, or with static if it's a property.

如果声明是函数,可以在声明前加上类或静态,如果是属性,可以在声明前加上静态。

class MyClass {

    class func aClassMethod() { ... }
    static func anInstanceMethod()  { ... }
    static var myArray : [String] = []
}