Note that there are some explanatory texts on larger screens.

plurals
  1. POSelf-Learning XCode/Objective-C: 'static' doesn't seem to mean what I *think* it means
    primarykey
    data
    text
    <p>I'm working through examples in the book 'Visual Quick Start, Objective-C' by Holzner. I spend a lot of time with each example, getting the code debugged is the faster part, and then stepping through saying to myself why each line of code works, what each word in each line does and deciding why the author used one way of doing things versus another. Then I repeat the example with some story of my own. This seems to be a good way to move from being a structured programmer and to an oop-like one. It works with these examples because he just does one concept at a time. (I've worked part way through 2 other books and this idea did not work for me in those. Once I got confused by something, I just stayed confused. There were too many variables in the lengthier, more complex examples.)</p> <p>In the current example (page 137), Holzner uses the word 'static'. I looked through examples in this book to decide what this word means. I also read the description in Bjarne Stroustrups' C++ Programming Language book (I understand that C++ and Objective-C are not exactly the same)</p> <p>(Bjarne Stroustup p 145) use a static variable as a memory, instead of a global variable that 'might be accessed and corrupted by other functions'</p> <p>Here is what I understand 'static' means as a result. I thought that meant that the value of a static variable would never change. I thought that meant it was like a constant value, that once you set it to 1 or 5 it couldn't be changed during that run.</p> <p>But in this example piece of code, the value of the static variable <em>does</em> change. So I am really unclear on what 'static' means.</p> <p>(Please ignore the 'followup question' I left commented in. I didn't want to change anything from my run, and risk creating a reading error</p> <p>Thank you for any clues you can give me. I hope I didn't put too much detail into this question.</p> <p>Laurel</p> <p>.....</p> <pre><code>Program loaded. run [Switching to process 2769] Running… The class count is 1 The class count is 2 Debugger stopped. Program exited with status value:0. </code></pre> <p>.....</p> <pre><code>// // main.m // Using Constructors with Inheritance //Quick Start Objective C page 137 // #include &lt;stdio.h&gt; #include &lt;Foundation/Foundation.h&gt; @interface TheClass : NSObject // FOLLOWUP QUESTION - IN last version of contructors we did ivars like this //{ // int number; //} // Here no curly braces. I THINK because here we are using 'static' and/or maybe cuz keyword? // or because in original we had methods and here we are just using inheirted methods // And static is more long-lasting retention 'wise than the other method // * * * // Reminder on use of 'static' (Bjarne Stroustup p 145) // use a static variable as a memory, // instead of a global variable that 'might be accessed and corrupted by other functions' static int count; // remember that the + is a 'class method' that I can execute // using just the class name, no object required (p 84. Quick Start, Holzner) // So defining a class method 'getCount' +(int) getCount; @end @implementation TheClass -(TheClass*) init { self = [super init]; count++; return self; } +(int) getCount { return count; } @end // but since 'count' is static, how can it change it's value? It doeeessss.... int main (void) { TheClass *tc1 = [TheClass new] ; printf("The class count is %i\n", [TheClass getCount]); TheClass *tc2 = [TheClass new] ; printf("The class count is %i\n", [TheClass getCount]); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload