Self-learning Xcode / Objective-C: "static" doesn't seem to mean that I * think * it means

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.

.....

//
//  main.m
//  Using Constructors with Inheritance
//Quick Start Objective C page 137
//

#include <stdio.h>

#include <Foundation/Foundation.h>

@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 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;
}
+3
3

, , , , ..

, :

- (int)getNumber {
    static int number = 0;
    return ++number;
}

1, 2, 3, 4 .. . , ??

+4

"static" - , ++ "const". , (, ) . , :

int getIndex()
{
    static int index = 0;
    ++index;
    return index;
}

"static" . : 1,2,3,....

:

int getIndex()
{
    int index = 0;
    ++index;
    return index;
}

, : 1,1,1,1,1,....

+10

static C/++/ Objective-C.

Objective-C C. ++ static , C/Objective-C. , , static Obj-C , ( ++).

C Objective-C static:

  • / / (.. , ). .

  • , , , , .

++, , static , , . ; , , . virtual.

static , .

, , . . static PDF . static C .

+4
source

Source: https://habr.com/ru/post/1772578/


All Articles