I am working on examples in the book "Visual Quick Start, Objective-C" by Holzner. I spend a lot of time on each example, debugging code debugging - this is the faster part, and then step by step, telling myself why each line of code works, what each word in each line does and decides why the author used one of the ways to do things against another. Then I repeat the example with my own story. This is apparently a good way to go from a structured programmer to oop-like. He works with these examples because he just does one concept at a time. (I worked partly through two other books, and this idea did not work for me in them. Once I was embarrassed by something, I just remained confused. There were too many variables for longer and more complex examples.)
In the current example (p. 137), Holzner uses the word "static". I looked through the examples in this book to decide what the word means. I also read the description in Bjarne Stroustrups' C ++ Programming Language book (I understand that C ++ and Objective-C are not quite the same)
(Bjarne Stroustup p 145) use a static variable as memory, instead of a global variable that "may be accessible and corrupted by other functions"
This is what I understand as "static." I thought that meant that the value of the static variable would never change. I thought this means that it looks like a constant value, that after you set it to 1 or 5, it cannot be changed during this run.
. , "" .
(, " ", .
, . , .
.....
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.
.....
#include <stdio.h>
#include <Foundation/Foundation.h>
@interface TheClass : NSObject
static int count;
+(int) getCount;
@end
@implementation TheClass
-(TheClass*) init
{
self = [super init];
count++;
return self;
}
+(int) getCount
{
return count;
}
@end
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;
}