static variables in Objective-C - what do they do?

后端 未结 3 1134
旧巷少年郎
旧巷少年郎 2020-12-07 08:46

I\'ve seen a few posts discussing what a static variable is and I think I get it - but I\'d love to quickly write (or find) a program that utilizes both a regular and a stat

3条回答
  •  醉话见心
    2020-12-07 09:22

    "static" refers more to the attributes of the variable (who what where) rather than just the value. Unlike other languages where it refers exclusively to the value.

    • Sticky

    It's like pinning the var to a specific location, be it inside a function or in the implementation.

    • Private

    It has similar attributes to a "private" var in that it's not visible to sibling or parents, but children can access.

    • Classy

    It is a declaration with default value. Like in other languages where you define vars within a class and assign their "default" value:

    private int myNumber = 3;

    This gives us "class-like" variables within functions. Declare them once, then as the function manipulates the value, the value is retained. The next time the function is called, the value will be the same as it was after the previous "cycle", just like you would expect a class variable's value to remain after manipulation.

提交回复
热议问题