Post

Replies

Boosts

Views

Activity

How do I properly use class variables?
Suppose I have classes foo and bar: class foo { } class bar: foo { } I will never instantiate foo, but I will instantiate bar, and other foo subclasses. (foo will provide some functions for its subclasses to use and potentially override, so it's not something that could be replaced with a protocol, as I understand it.) Now, suppose that I wanted to define a class variable in foo, that would be available to all subclasses, and I wanted to set the value of that variable at runtime, from outside foo and any of its subclasses. How do I do this? I've been wrestling with all kinds of different syntax and can't figure it out. Or is there a better way to achieve the same goal in Swift that my brain hasn't grokked yet?
6
0
2.1k
May ’21
How do I properly use class variables?
Suppose I have classes foo and bar: class foo { } class bar: foo { } I will never instantiate foo, but I will instantiate bar, and other foo subclasses. (foo will provide some functions for its subclasses to use and potentially override, so it's not something that could be replaced with a protocol, as I understand it.) Now, suppose that I wanted to define a class variable in foo, that would be available to all subclasses, and I wanted to set the value of that variable at runtime, from outside foo and any of its subclasses. How do I do this? I've been wrestling with all kinds of different syntax and can't figure it out. Or is there a better way to achieve the same goal in Swift that my brain hasn't grokked yet?
Replies
6
Boosts
0
Views
2.1k
Activity
May ’21