Difference between ivars and (global?) Variables defined outside brackets

What's the difference between

@implementation aClass {
   aType *aVariable
}

- (void)aMethod: {
}

      

and

@implementation bClass

bType *bVariable

- (void)bMethod: {
}

      

Is it bVariable

global?

+3


source to share


1 answer


Is it bVariable

global?

Yes.

Objective-C is an extension of C and it is a standard C global variable declaration.

Probably, you should also look for value static

and extern

in relation to global variables in C.

NTN

Adding

So back to the first question, the difference is that I cannot define bVariable

again in my whole project, is the term aVariable

reusable?



Short answer: No, at least not in the way you expressed the question.

Long answer: you have two declarations, each declaring a variable and associating a name (or identifier) ​​- aVariable

, bVariable

- with that variable. Like a type that is part of a declaration, a variable has a lifetime — how long the variable exists — and a scope — the part of the program in which the variable is available. Scopes can be included, and inner scopes can contain declarations that use the same name as the outer scopes, resulting in hiding - a variable in the outer scope cannot be directly accessed via its name.

A global variable is one whose lifetime is the complete execution of the program, however, the scope in which the global variable is (directly) accessible does not have to be the whole program (cf static

qualifier in (Objective-) C), but different global variables with non-overlapping scopes can have the same name.

An instance variable is one whose lifetime is the same as that of an instance of its own instance, and whose scope is class members.

There are also local variables, whose lifetime and scope are method, function, block, etc., that declare them.

The above is just a summary, you should look for the meanings of all italicized terms.

+2


source







All Articles