Static vs class functions and variables in Swift classes: what's the difference?
Hi everyone! I started learning Swift and came across a topic I don't understand. In classes, you can declare functions and variables as both static and class. It seems both keywords work similarly: they allow you to call methods and access properties without creating an instance of the class. For example:
```swift
class MyClass {
static var staticProperty = "Static property"
class var classProperty: String {
return "Class property"
}
static func staticMethod() {
print("Static method")
}
class func classMethod() {
print("Class method")
}
}
MyClass.staticMethod() // works
MyClass.classMethod() // also works
```
But I noticed that if I try to override a `static` method in a subclass, Xcode gives an error, while `class
No answers yet.
Log in to leave an answer