In objective-c, I can declare an int or bool etc in the .m file, outside any function. That allows me to use such variable everywhere in the class.
I can also declare such variables in the .h file, inside the interface block, achieving the same result.
Well, my question is: what is the difference? Is there? Or is it all a matter of organization?
In second case, it is a global variable that has external linkage. Meaning, it can be accessed other translation units / source files using extern keyword. But in first case, it is a part of interface, so it can be only used by it's member functions and any other interfaces derived from this interface depending on access specifier.
In first case they become gloabal variables in .m file and are are shared between all the instances of the interface. In second case the will be separate for multiple instances. The common way is to declare interface variables with in the interface
Related
I have an ABAP include containing only constants. (Not my code)
I want to use these constants in an ABAP OO method. (My code)
How can I use these constants in an object oriented ABAP environment without copying them?
The idea is to define these constants once and only once. And they are already defined in this include.
Additional question: is it possible to create a class that contains the constants of the above include in a public section, so that I do the include only once and use these constants in an object oriented way from other classes?
Assuming that the include really only contains constant definitions: From the Class Builder, select Goto --> Class-relevant Local Definitions and place an INCLUDE statement there. This should make the constants available throughout your implementation.
Will you need these constants just for this class? Create private atributes in the class.
Will you use it in other classes (and programs as well)? Create and class with static components and use it everywhere...
I do not have a background on C/C++. I just started learning Objective-C after a past using other languages.
In which situations should I use static declaration of a variable over regular ivars or properties? What do I gain doing this?
thanks
Global variables and functions
By default, all symbols (global variables and functions) are exported (made visible to code in other source files). If a global variable is declared static, it is not exported. That means it is only accessible to code in the current source file.
This is useful when you have a global variable that you want to restrict access to, and don't want to worry about a name collision. For example, if you wanted to maintain a counter to track how many instances of a class have been created, you could create a static int gInstanceCount. Since it is static, you would know that (1) no other code can modify the variable and (2) if some other file uses a global with the same name, there won't be any collisions.
Static declarations in header files
Note that when you put something in a header file, it is as if you copied-and-pasted that code into every other file that includes it. That means if you declare something as static in a header file, every file that includes it gets its own copy of that thing.
That means if you declare static int foo in Foo.h and then write execute foo = 4 in Bar.m, when you try to access that value in Other.m you won't necessarily get 4 back.
Local static variables
You can also define a local variable (inside a function or method body) as static. Normally, local variables are allocated on the "stack" which means they are created when your function is executed and deallocated when the function exits. If two threads enter the same function at the same time (or one thread makes a recursive call back into a function) each thread gets a fresh chunk of memory to work with, and anything it does won't affect any other thread.
However, a local static variable is stored on the "heap" instead. All executions of the function share that same memory location. Also, when the function ends, the value stays where it is. That's why a local static variable is often used in sharedInstance methods on Objective-C singleton objects.
In most ways, a local static variable basically acts like a global variable that can only be seen inside the function which declared it.
the static keyword is used to provide scope to global variables. Normally, global variables defined outside of a function have a public scope, and are visible to all .m or .c files in a project. making a variable static allows you to have a "global" variable which is scoped (visible) to only the .m or .c file containing the variable definition. This allows you to have a variable which can be shared by multiple functions in a single source file, while avoiding potential naming conflicts. Also of note, the extern keyword, which allows you to indicate that a specific global variable is initialized in a different source file but used in this source file.
The keyword static is overused in C. It means several different things. In some contexts, it just means that field is only understood in the remainder of that file (compilation unit). In other words, it can't be linked to from some other file.
In Java and C++, static class members are defined at the class level, not the individual object level, so the one value is shared by all objects of that class (or one of its subclasses). Unfortunately, IMHO, Objective C does not support this. Instead you use statics at the file level.
If you don't know if you need a static variable, you probably don't need one.
One reason you might use a static variable is to provide a class variable (that is, one variable for an entire class, rather than an instance variable, which each instance of a class would have it's own copy of).
This question (and its accepted answer) explain how one would use static variables to simulate a class variable in Objective-C.
First: "object" in this context refer to "c objects", in very easy words, something, that lives at runtime. It has nothing to do with objects in the sense of OOP or Objective-C. You can think of an object as a var.
static has nothing to do with scope. static on an extern identifier (= declared outside a block) has internal linkage. The results of linkage are defined in ISO/IEC 9899:TC3, section 6.2.2. Scope is defined in section 6.2.1.
Internal linkage means, in short words, that two identifier in different translation units (".m files") do not denote the same object, but different objects. Every translation unit has its "own" object.
If an identifier is not extern (= inside a block) declared with static storage class it has a lifetime from begin of the program execution until the program stops running.
Static variable "Declaring a variable static limits its scope to just the class—and to just the part of the class that’s implemented in the file" (Apple doc).
And I think a variable was defined in class extension has a limit scope in just only the class define it.
That is similar !
What is different between static variable and variable in class extension ?
The static variable is tied to the file it's defined in. It's not accessible from outside of that file, and there is only one place for storage created for it in your entire program.
The distinction about storage also applies to non-static global variables -- there will only be one in your program.
A variable in a class extension is likewise limited in visibility to the file in which it's declared, but it's an instance variable. There's a new piece of storage attached to each instance of the class you create.
If you create a static variable and change its value from several instances of the class, every instance will see the same value. That is not the case with an ivar -- each object can change and retain its own value for that variable.
(This is why static variables are sometimes used in ObjC to simulate class variables, which are present in other languages.)
I read somewhere that interfaces can have member variables.
Static final constants only, can use
them without qualification in classes
that implement the interface. On the
other paw, these unqualified names
pollute the namespace. You can use
them and it is not obvious where they
are coming from since the
qualification is optional.
I am not quite understood by what they meant? Any help?
What you read is incorrect. Interfaces cannot have member variables.
In VB.Net the only allowable definitions inside an interface are
Properties
Methods
Events
Type Definitions (not legal in C#)
I'm not entirely sure what the above paragraph is referring to. Based on the text though it sounds like it's refering to Java. They phrase static and final is most often associated with Java code and not .Net (static and readonly).
Can you give us some more context on it?
If you define a constant like this inside a class MyClass:
public static final int MY_CONSTANT = 1;
you can refer to it from other classes as MyClass.MY_CONSTANT, using the MyClass qualifier. This hints the location of the constant definition.
If you define such a constant in an interface MyInterface, you still can refer to it using MyInterface.MY_CONSTANT. However in the classes implementing MyInsterface you can simply use MY_CONSTANT without "MyInterface" prefix.
It may look convenient (less key strokes), but may lead to confusion because without qualifier (prefix) it is not clear where the constant was originally defined.
Adding member variables to interfaces would be bringing in MI through the back door.
Not available in .NET, sorry.
I wish it were there though.
What is the standard way of incorporating helper/utility functions in Obj-C classes?
I.e. General purpose functions which are used throughout the application and called by more than 1 class.
Can an Obj-C method exist outside of a class, or does it need to be a C function for it to have this kind of behaviour?
I would group similar functions as static methods in a helper class. These can then be called using the classname rather the instance name. Static methods are defined with a + instead of the usual -.
like so:
#interface HelperClass: superclassname {
// instance variables - none if all methods are static.
}
+ (void) helperMethod: (int) parameter_varName;
#end
This would be called like so.
[HelperClass helperMethod: 10 ];
As this is static you do not init/alloc the class. This has the advantage of clearly grouping like Helper functions. You could use standalone C functions but as your Application gets larger it can become a right mess! Hope this helps.
Tony
I don't see why people are avoiding creating functions. Objective-C is a superset of C, which means that C is part of it. Moreover, it's completely integrated—there's no wall between them.
Create functions! It's fine! Foundation does it. Application Kit does it. Core Animation does it. Core Media does it.
I see no reason not to.
There are a number of options for this in Objective-C. First, since Obj-C is a strict superset of C, you can define all your library functions in a separate module (source file) and happily call them from any Obj-C object/code you already have. If you create an obj-c source file (.m file) you can then call back into/use objects.
If your generic functions are logically manipulating other, established objects (for instances, operates on an NSString), you can use categories to graph your functions on already existing classes (where that makes sense).
Finally, as Tony points out, you can create classes with static methods (although I like this option the least, personally). I tend to use a mix of one an two... adding categories where appropriate and using standard functions for others. I generally only make a new class where it makes sense to design a class.