Are static classes supported by Swift?
No, Swift has no concept of a static class. But if you have no assignable property anyway, what difference will initialization make? I can think of 2 options:
Mark the class as
final
so it cannot be inherited:final class MyClass { .... }
Use a
struct
, which has no inheritance:struct MyUtilities { ... }
Static vs class functions/variables in Swift classes?
static
and class
both associate a method with a class, rather than an instance of a class. The difference is that subclasses can override class
methods; they cannot override static
methods.
class
properties will theoretically function in the same way (subclasses can override them), but they're not possible in Swift yet.
Does swift have class level static variables?
Swift supports static type properties, including on classes, as of Swift 1.2:
class MyClass {
static let pi = 3.1415926
}
If you need to have a class variable that is overridable in a subclass, you'll need to use a computed class property:
class MyClass {
class var pi: Double { return 3.1415926 }
}
class IndianaClass : MyClass {
override class var pi: Double { return 4 / (5 / 4) }
}
Static imports in swift
I don´t think you can import a static class like Java, it´s a traditional static in Swift where you call it by class name + variable/function.
class MyClass {
static let baseURL = "someURl"
static func myMethod() {
}
}
MyClass.baseURL
or MyClass. myMethod
.
What you can do is to add a typealias
to make an alias for your Static class.
private typealias M = MyClass
And then use the following: M.baseURL
or M.myMethod
.
Swift is not supported for static libraries' when creating a CocoaPod
Turns out I should have read the tutorial better, it clearly says this:
[!] Note: Due to a Development Pods implementation detail, when you
add new/existing files to Pod/Classes or Pod/Assets or update your
podspec, you should run pod install or pod update.
It helped with the very uninformative error.
What is the reason you can't use static methods/variables in a class
It is a terminological bêtise. What Apple did was decide that you use static
in an enum or struct and class
in a class. In my view this was a very foolish decision, as they are exactly parallel to one another. Many people have posted on the dev forums suggesting that they just call them all static
.
The outcome is a candidate for Occam's Razor: two phrases, e.g. static property and class property, or static method and class method, that mean exactly the same thing. And then they try to cover their tracks in the documentation by adding a third term as an umbrella, "type property" (meaning a static-or-class property) or "type method" (meaning a static-or-class method), which just makes the situation even worse.
The badness of the situation is also revealed by what you have to do in protocols: you declare a class
property in a protocol and then if a struct adopts it, it calls that a static
property. Same thing with a class
method in a protocol; an adopting struct must call that a static
method. You'd think that this alone would have told them they'd done a bad thing.
Edit: The more I think about it, the more I like the proposal put forth by newacct
in a comment below. If Apple had simply used the umbrella keyword type
here as part of the language, the whole problem would have been solved. We could have declarations type var
, type let
, type func
, and this would work equally in enums, structs, classes, and protocols across the board.
Class type properties should not have static keyword?
Effective Swift 1.2,
static
properties are now permitted in classes.Classes have always been permitted to have stored properties.
public static vs open static vs public class vs open class?
This question is over-complicated because you're comparing the members of the cartesian product of two variables (open
vs public
and static
vs class
), rather than asking about the two variables separately.
It's not a matter of open static
vs public static
vs open class
vs public class
, but rather of open
vs public
and static
vs class
. They're two orthogonal dimensions.
open
vs public
public
:
Within the module, the public
access specifier allows access and overriding.
From outside the module, the public
access specifier allows access, but does not permit overrides/subclasses.
open
:
Within the module, the open
access specifier allows access and overriding.
From outside the module, the open
access specifier allows access, and permits overrides/subclasses.
static
vs class
static
:
A static
member (method or property) is one who is bound to the specific scope (class
/struct
/enum
) it is defined in. It's named such because access to such members is always statically dispatched. This is equivalent to Java's static
. Objective C has no equivalent to this.
class
:
A class
member is one who is bound to a class or its subclasses. class
members can be overridden by subclasses. Because of this, they're dynamically dispatched in the general case, although accesses to class
members can be devirtualized by the optimizer in some cases. Java has no equivalent to this. This is equivalent to Objective C's class (+
) methods.
Static properties in Swift
With this code:
class var items: [AnyObject] {
return [AnyObject]()
}
you are not creating a stored property - instead it's a computed property, and the worst part is that every time you access to it, a new instance of [AnyObject]
is created, so whatever you add to it, it's lost as soon as its reference goes out of scope.
As for the error, the static computed property returns an immutable copy of the array that you create in its body, so you cannot use any of the array method declared as mutating
- and removeAll
is one of them. The reason why it is immutable is because you have defined a getter, but not a setter.
Currently Swift classes don't support static properties, but structs do - the workaround I often use is to define an inner struct:
class SomeClass {
struct Static {
static var items = [AnyObject]()
}
}
SomeClass.Static.items.append("test")
If you want to get rid of the Static
struct every time you refer to the items
property, just define a wrapper computed property:
class var items: [AnyObject] {
get { return Static.items }
set { Static.items = newValue }
}
so that the property can be accessed more simply as:
SomeClass.items.append("test")
Related Topics
Context Menu Not Updating in Swiftui
Using Animoji/Memoji as Profile Photo
How to Star a Repo with Github API
Is Dispatchsemaphore a Good Replacement for Nslock
Swiftui Tabbar: Action for Tapping Tabitem of Currently Selected Tab to Reset View
How to Integrate Uisearchcontroller with Swiftui
Deleting a Camera Roll Asset Using Photos Framework
iOS Swift Error: 'T' Is Not Convertible to 'Mirrordisposition'
Nstimer Not Stopping When Invalidated in This Function
Building an Nsoutline View with Check Marks
Testing Protocol Conformance with Associated Types
Swift Progress View with Nstimer
"Use Default Container" Doesn't Show in Icloud Capabilities
Swiftlint Overriding Project Settings Related to Spm
Convert JSON Anyobject to Int64