Post

Replies

Boosts

Views

Activity

Reply to How do I properly use class variables?
Is this what you look for ? Not quite, no... I want to be able to set the value from outside, not from within one of the child classes. Here's one of the examples I tried out in a playground, which actually "crashed" with an EXC_BAD_ACCESS error. 😬 The basic idea is that it's an object that will be available to all subclasses, but that is not known at compile time and must be set at runtime. I'm trying to avoid the use of a global variable, as that's a poor solution, but I'm starting to wonder if Swift supports the concept of these class variables in the same way that other languages I've used do. class foo {     class var a: String {         set { self.a = newValue }         get { return self.a }     } } class bar: foo {     func printit() {         print(foo.a)     } } foo.a = "abc" let someBar = bar() someBar.printit()
Topic: Programming Languages SubTopic: Swift Tags:
May ’21
Reply to How do I properly use class variables?
Thanks, I ended up finding a solution myself... it appears I was misunderstanding the way the static keyword works in Swift. Here's what I ended up with (minus error checking that would go into the real code, of course): class foo {     static var a: String?     func printit() {         print(foo.a)     } } class bar: foo { } foo.a = "def" let someBar = bar() someBar.printit() This does what I want. Your point about being cautious with these things is taken; not to worry, this isn't something I plan to do much of, but it does provide a cleaner solution to tie this to the class rather than making it a true global. 🙂
Topic: Programming Languages SubTopic: Swift Tags:
May ’21
Reply to How do I properly use class variables?
Is this what you look for ? Not quite, no... I want to be able to set the value from outside, not from within one of the child classes. Here's one of the examples I tried out in a playground, which actually "crashed" with an EXC_BAD_ACCESS error. 😬 The basic idea is that it's an object that will be available to all subclasses, but that is not known at compile time and must be set at runtime. I'm trying to avoid the use of a global variable, as that's a poor solution, but I'm starting to wonder if Swift supports the concept of these class variables in the same way that other languages I've used do. class foo {     class var a: String {         set { self.a = newValue }         get { return self.a }     } } class bar: foo {     func printit() {         print(foo.a)     } } foo.a = "abc" let someBar = bar() someBar.printit()
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
May ’21
Reply to How do I properly use class variables?
Thanks, I ended up finding a solution myself... it appears I was misunderstanding the way the static keyword works in Swift. Here's what I ended up with (minus error checking that would go into the real code, of course): class foo {     static var a: String?     func printit() {         print(foo.a)     } } class bar: foo { } foo.a = "def" let someBar = bar() someBar.printit() This does what I want. Your point about being cautious with these things is taken; not to worry, this isn't something I plan to do much of, but it does provide a cleaner solution to tie this to the class rather than making it a true global. 🙂
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
May ’21