What is the difference between static func and class func in Swift?
Hello! I started learning Swift and came across two similar ways to declare methods that belong to the type itself, not its instance. Using the `static` keyword before `func`, I can call a method without creating an object. But I also saw that you can use `class func` for the same purposes. For example:
```swift
struct MyStruct {
static func sayHello() {
print("Hello from static!")
}
}
class MyClass {
class func sayHello() {
print("Hello from class!")
}
}
```
Both calls work: `MyStruct.sayHello()` and `MyClass.sayHello()`. What is the fundamental difference between them? Why can't we just use `static` everywhere? And are there any restrictions on using `static` in classes? I would be grateful for a detailed explanation with examples.
No answers yet.
Log in to leave an answer
Blogs
Design Patterns in PHP with Examples: From A to Z
Creating CLI Utilities in C++: From Idea to Ready Tool
Go Community: A Guide to Conferences, Blogs, and Resources for Developers
Iterating Over an Array in Swift: Index and Element Under the Hood