-5

How can I change the value of a static variable in Objective-C?

Is this allowed?

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
Undertaker
  • 39
  • 1
  • 7

1 Answers1

0

You use the = operator to assign a value in the file where the static variable is visible.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • that doesn't work, for me it just sticks to its old value. – findusl Jan 21 '19 at 09:52
  • @findusl: It sounds like what you were trying to do is change a static variable from *another* file, where the variable is not visible. What "static" means in C is basically "this variable is only visible in this file." Static variables are pretty common in Objective-C — it's how, for example, singletons are commonly implemented. When other files need to set the variable, a setter function or method is provided. – Chuck Jan 22 '19 at 16:56
  • But the static variable is visible and usable in the other file. Just can't be changed. – findusl Jan 22 '19 at 17:03
  • @findusl: I am very sure that is not the case with any mainstream compiler — that would violate the C standard. It's hard to say without seeing your code, but I'd guess you're creating a second, unrelated variable, rather than seeing the file's static variable. For example, if you have `static int x = 5;` in a header file, every file that imports that header would have a static variable named `x` that is unrelated to any other file's static variable named `x`. – Chuck Jan 22 '19 at 17:26
  • Could be that that happens. Would explain the result since I wanted to change it for a different file. – findusl Jan 22 '19 at 17:29