Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective c - static members and constants

Whats the difference between:

@interface SomeClass : NSObject {
    NSObject *something;
}

and

@interface SomeClass : NSObject {

}
NSObject *something;

? Also, what's the difference between Java's final and Objective C (C)'s const? And where should I declare static class members for the following situations: 1. When only the class needs it **2.**Where it would be a property that other classes could read ? I already know about #define, but that isn't good for objects because it creates new ones each time. Thanks!

like image 572
mk12 Avatar asked Sep 05 '09 16:09

mk12


People also ask

What is static in Objective-C?

"In both C and Objective-C, a static variable is a variable that is allocated for the entire lifetime of a program.

How do you declare a global variable in Objective-C?

int gMoveNumber = 0; at the beginning of your program—outside any method, class definition, or function—its value can be referenced from anywhere in that module. In such a case, we say that gMoveNumber is defined as a global variable.

How do you declare a class variable in Objective-C?

Variable Declaration in Objective-C You will use extern keyword to declare a variable at any place. Though you can declare a variable multiple times in your Objective-C program but it can be defined only once in a file, a function or a block of code.

What is static class?

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new operator to create a variable of the class type.


2 Answers

The former is an instance variable and creates a something for each instance of SomeClass. It's similar to C's

typedef struct {
    NSObject *something;
} SomeClass;

The latter declares a global variable that has no real association with SomeClass. In C, it's equivalent to

NSObject *something;

defined in global scope. Objective-C doesn't really have class variables, so global variables are used (rather, are sometimes used; variables with compilation unit scope and static storage class are what should be used).

The cleanest way of defining a "class" variable is to define a static variable in the implementation file. That way, only the class methods can access it and you avoid polluting the global namespace. If you want it publicly available, define accessors.

Destroying class variables properly can be tricky. Memory will be reclaimed and open files will be closed automatically when the application exits, but other resources may not be handled so well.

like image 84
outis Avatar answered Sep 27 '22 21:09

outis


As to the "final vs const" question, both are similar. They state that the value cannot change. note that in Java, as all values (except primitives) are pointers, the object it is pointing to could change underneath, but the memory location (the pointer) will never change. I believe you would expect similar behavior in Objective C, and it is always a good idea to not allow elements that are mutable be "final" or "const" as the values inside the object can still be modified then.

like image 30
aperkins Avatar answered Sep 27 '22 21:09

aperkins