making a class variable global in throught the application - objective-c

In my application there are lot of view controller in some view controller some variables are there which i want to use in other classes .my variable is not present in application delegate file so i can i make it global to use every where in my application?

In my opinion, how about using singleton pattern? So when you want to use the variables of that class, just get instance and then use the variables.
#interface MySingletonViewController : UIViewController
{
//here your variables
int globalVariables;
}
#property (nonatomic, assign) int globalVariables;
+ (MySingletonViewController *)sharedSingleton;
#end
#implementation MySingletonViewController
#synthesize globalVariables;
static MySingletonViewController *sharedSingleton = nil;
+ (MySingletonViewController *)sharedSingleton
{
#synchronized(self)
{
if (sharedSingleton == nil)
sharedSingleton = [[MySingleton alloc] init];
return sharedSingleton;
}
}
#end
UIViewController is class actually, so we can do this way : ) Hope this helpful.

Sure you can, but using global variables through entire app is definitely broken architecture design.
As Objective-C based on C, you can define variable (in you case - pointer to class) in any *.m file outside implementation part as:
MyVeryOwnClass *g_MyVeryOwnClassPointer = nil;
And access it as:
extern MyVeryOwnClass *g_MyVeryOwnClassPointer;
/* do some operations with your pointer here*/
Or move extern declaration to header file.
PS: You can use singletons. They are not the best solution, but better then using raw variable.

Related

Structure (struct) in Xcode as Array, and global

I'm trying to create a structure, which should be used as an array.
Then at the same time I need to access it from a different view, so I'm guessing I need to make it global for now, until I learn a better way (have tried many suggestions from google searches)
But how do I declare a variable twice?
I mean both as my structure, and as an array?
Sometimes "global variables" can be appropriate, but not simply as "global variables". The proper way to do it in Objective C is with singletons.
Here is part of the .h implementation below:
#interface MySingleton : NSObject
#property (nonatomic, strong) NSMutableArray *myArray;
+ (MySingleton *)sharedSingleton
#end
And here is what the .m implementation would look like:
#implementation MySingletion
#synthesize myArray;
static MySingleton *shared = nil;
- (id)init
{
self = [super init];
if(self)
{
myArray = [[NSMutableArray alloc] init];
}
return self;
}
+ (MySingleton *)sharedSingleton
{
if(shared == nil)
{
shared = [[MySingleton alloc] init];
}
return shared;
}
Then, whenever you wanted to access this from a given class, you would #import "MySingleton.h" and access your array like so:
[MySingleton sharedSingleton].myArray
Edit: However, this does not mean that every time you need to pass data it should be done with a singleton. Singletons are correct in certain situations, but as others have mentioned, you should pass the data to the next class yourself in most situations.
Well i just created a new h and m fil, where i put a lot of varibales
using extend in the.h file
and alloc init in the .m file
Then i just include this file where i need the variables. Probably there is a much better way, but this was the only one i could figure out.

how to save a variable and use it from two different classes Objective C

I have one Class called "System" it contains some variables and two arrays and I need to access this from two other classes which should be able to read and write that variables
Im a total Beginner so its pretty possible that i already did some mistakes.
System.h
#interface System : UIViewController{
float length_of_one_hour;
float length_of_first_break;
float length_of_second_break;
float length_of_lunch_break;
float length_of_shortned_hour;
float school_begin;
int school_end[5];
float school_length[5];
}
About_now.m
- (void)read_school_end_monday{
school_end_label.text=[NSString stringWithFormat:#"%i", school_end[0]];
}
Settings.m
- (IBAction)set_school_end_monday{
school_end[0]= [school_end_on_mondays_textfield.text intValue];
}
But i don't know what to write in System.h and About_now.m that the variables are saved in System class and can be accessed from anywhere. And Yes I already tried #public and extern.
BTW I need to have an array for the school_end because I'll calculate it (in use of length of an hour and when school actually starts etc.) with a function which already works but i need to access the variables from the About_now class afterwards.
Hope there is someone who can help me. Thanks
A common way to share data across classes in iOS apps is by following the singleton pattern.
SystemModel.h
#import <Foundation/Foundation.h>
#interface SystemModel : NSObject {
#public
float length_of_one_hour;
float length_of_first_break;
float length_of_second_break;
float length_of_lunch_break;
float length_of_shortned_hour;
float school_begin;
int school_end[5];
float school_length[5];
}
+(SystemModel*)instance;
#end
SystemModel.m
#implementation SystemModel
static SystemModel* _instance= nil;
+(SystemModel*)instance {
#synchronized([SystemModel class]) {
if (!_instance)
[[self alloc] init];
return _instance;
}
return nil;
}
+(id)alloc {
#synchronized([SystemModel class]) {
return (_instance = [super alloc]);
}
return nil;
}
-(id)init {
self = [super init];
if (self != nil) {
// your init code
}
return self;
}
#end
Now you can use your instance like this:
float tmp = [SystemModel instance]->length_of_one_hour;
You could also convert instance variables to properties, and use the dot syntax. It does not matter for floats and arrays, but for id-based objects using properties is preferred.
You need a pointer to an instance of System. So to access the member variable:
System* system = GetSystem();
system->school_end[0] = whatever();
This is exactly the same as accessing a member via a struct pointer.
But saying that, it is good practice to hide access via methods:
- (int)getSchoolEndAtIndex:(int)index
{
return school_end[index];
}
You have many design options for doing that:
create a global System object;
make the System class a Singleton;
define the System class interface so that it offers accessors methods for its "properties" at the class level (vs. object level).
instantiate System as it is now and pass it as a initialization parameter to the other two classes.
Now, in more detail:
(1) declare:
extern System* globalSystemObject;
in System.h; then define:
static System* globalSystemObject = nil;
in System.m. Don't forget to initialize globalSystemObject somewhere in your code:
globalSystemObject = [[System alloc] init];
Doing like that, you can use globalSystemObject from any file which imports System.h.
(2) have a look here for details about implementing a singleton class. Keep in mind that many people suggest against using Singleton (if it is only a way to "hide" a global variable). In this specific case, considering the semantics of a class called System, I would say a Singleton is the best option for you.
(3) your class might look like this:
#interface System : UIViewController{
}
+(float)length_of_one_hour;
+(void)set_length_of_one_hour:(float)length;
+(float)length_of_first_break;
+(void)length_of_first_break:(float)length;
...
#end
You would implement those variable in the .m file by means of static variables like in 1.
(4) this is pretty straightforward. The only thing is that you have to create the System instance and those of the other two classes in the same scope:
System* system = [[System alloc] init];
about_now* anow = [[about_now alloc] initWithSystemInstance:system];
...
The system parameter would be stored inside an ivar (or property) of about_now.
Doing like that you avoid altogether the use of global variables (even those hidden behind the Singleton mask).
You could expose the system instance variables via getters/setters (#property / #synthesize) and pass a reference to other classes instances.
#interface System : UIViewController{
float length_of_one_hour;
//...
#property (nonatomic,assign);
..
#end
OtherClass other = [[OtherClass alloc] initWithSystem:sys];
or with a setter
[other setSystem:sys];

Global variable in iOS TabBar Application

I am creating ios app in xcode 4.2.
I have outside file with database. I dont wanna download data in every view. How should i create one global variable for tabbar application? And when should i upload this database before closing of application?
In iOS applications the model data is often kept in a singleton, rather than in a global variable. Here is an article briefly describing singletons in Objective-C.
You can load your data in the class method that initializes your shared singleton. Uploading the data back is a bit trickier, because the singleton itself does not know when to do it. Therefore you should make an instance method -(void)uploadData in your singleton class, and call that method when your application is about to close. applicationWillResignActive: method of your application delegate is a good place to initiate the upload.
I use singletones like this: in class DataBase with some arrays of data i implement share method:
+(id)share
{
static id share = nil;
if (share == nil) {
share = [[self alloc] init];
}
return share;
}
and then in some classes: self.dataBase = [DataBase share];
You can create global variables by doing this
extern NSString *someString;
#interface ......
#property (strong, nonatomic) NSString *someString;
#end
#implementation ......
#systhesize someString;
NSString *someString;
#end

Class variable defined at #implementation rather than #interface?

I'm new to Objective-C, but I am curious about something that I haven't really seen addressed anywhere else.
Could anyone tell me what is the difference between a private variable that is declared at the #interface block versus a variable that is declared within the #implementation block outside of the class methods, i.e:
#interface Someclass : NSObject {
NSString *forExample;
}
#end
vs.
#implementation Someclass
NSString *anotherExample;
-(void)methodsAndSuch {}
#end
It seems both variables ( forExample, anotherExample ) are equally accessible throughout the class and I can't really find a difference in their behaviour. Is the second form also called an instance variable?
The latter is not defining an instance variable. Rather, it is defining a global variable in the .m file. Such a variable is not unique to or part of any object instance.
Such globals have their uses (roughly equivalent C++ static members; e.g. storing a singleton instance), but normally you would define them at the top of the file before the #implementation directive.
They're very different! The one in #implementation is a global variable not unique to each instance. Imagine there were accessors for both variables, written in the obvious way. Then the difference in behavior is shown here:
Someclass* firstObject = [[Someclass alloc] init];
Someclass* secondObject = [[Someclass alloc] init];
//forExample is an instance variable, and is unique to each instance.
[firstObject setForExample:#"One"];
[secondObject setForExample:#"Two"];
NSLog(#"%#",[firstObject forExample]); //Result: "One"
NSLog(#"%#",[secondObject forExample]); //Result: "Two"
//anotherExample is a global variable, and is NOT unique to each instance.
[firstObject setAnotherExample:#"One"];
[secondObject setAnotherExample:#"Two"];
NSLog(#"%#",[firstObject anotherExample]); //Result: "Two" (!)
NSLog(#"%#",[secondObject anotherExample]); //Result: "Two"
//Both instances return "Two" because there is only ONE variable this time.
//When secondObject set it, it replaced the value that firstObject set.
If you are looking for this sort of behavior, you might be better off using a class variable, like this:
static NSString* yetAnotherExample = nil;
Then you can use class methods to interact with the variable, and it's clearly class-specific (as opposed to instance-specific or global).
If you declare a variable inside the #implementation section, you're actually creating a global variable, visible everywhere (in every method in your application).
Member variables can only be declared in the #interface section. They are only accessible in the class itself.
The private block declared inside the #implementation block is kind of dangerous, seems to me, comparing with other OOP concept e.g. Java. Its look like member variable but kinda static.
Novice programmer can easily fooled with it. I write a test program and surprised with the behaviour.
#interface SomeClass : NSObject
{
NSString *forExample;
}
- (void) set:(NSString *)one another:(NSString *)another;
- (void)print;
#end
Implementation:
#import "SomeClass.h"
#implementation SomeClass
NSString *anotherExample;
- (void) set:(NSString *)one another:(NSString *)another
{
forExample = one;
anotherExample = another;
}
- (void)print{
NSLog(#"One = %#, another = %#", forExample, anotherExample);
}
#end
Test:
- (void)testClass {
SomeClass * s1 = [SomeClass new];
[s1 set:#"one one" another:#"one another"];
SomeClass *s2 = [SomeClass new];
[s2 set:#"two one" another:#"two another"];
[s1 print];
[s2 print];
}
And the output is,
One = one one, another = two another
One = two one, another = two another
Use a code snippet to tell the difference between a member variable and a global variable:
#implementation MyClass {
// It is an ivar, or called member variable
// Can NOT be initialized when defined.
// Can be accessed with `self->_i`
int _i;
}
- (instancetype)init {
if (self = [super init]) {
_i = 2; // should be initialized before being used.
}
return self;
}
int i = 9; // Global variable, and can be initialized when defined.
- (void)myFun {
NSLog(#"%i, %i", self->_i, i);
}
#end
// Another file
extern int i;
NSLog(#"%i", i);
Just to be clear, never ever ever declare an IBOutlet as a global var (in the implementation) if you are using it for localized nibs/xibs.
I spent a few hours figuring why the outlet is connectable only in one of the localized nibs at any given time.
Thanks for this question and the answers!

Objective-C Static Class Level variables

I have a class Film, each of which stores a unique ID. In C#, Java etc I can define a static int currentID and each time i set the ID i can increase the currentID and the change occurs at the class level not object level. Can this be done in Objective-C? I've found it very hard to find an answer for this.
Issue Description:
You want your ClassA to have a ClassB class variable.
You are using Objective-C as programming language.
Objective-C does not support class variables as C++ does.
One Alternative:
Simulate a class variable behavior using Objective-C features
Declare/Define an static variable within the classA.m so it will be only accessible for the classA methods (and everything you put inside classA.m).
Overwrite the NSObject initialize class method to initialize just once the static variable with an instance of ClassB.
You will be wondering, why should I overwrite the NSObject initialize method. Apple documentation about this method has the answer: "The runtime sends initialize to each class in a program exactly one time just before the class, or any class that inherits from it, is sent its first message from within the program. (Thus the method may never be invoked if the class is not used.)".
Feel free to use the static variable within any ClassA class/instance method.
Code sample:
file: classA.m
static ClassB *classVariableName = nil;
#implementation ClassA
...
+(void) initialize
{
if (! classVariableName)
classVariableName = [[ClassB alloc] init];
}
+(void) classMethodName
{
[classVariableName doSomething];
}
-(void) instanceMethodName
{
[classVariableName doSomething];
}
...
#end
References:
Class variables explained comparing Objective-C and C++ approaches
As of Xcode 8, you can define class properties in Obj-C. This has been added to interoperate with Swift's static properties.
Objective-C now supports class properties, which interoperate with Swift type properties. They are declared as: #property (class) NSString *someStringProperty;. They are never synthesized. (23891898)
Here is an example
#interface YourClass : NSObject
#property (class, nonatomic, assign) NSInteger currentId;
#end
#implementation YourClass
static NSInteger _currentId = 0;
+ (NSInteger)currentId {
return _currentId;
}
+ (void)setCurrentId:(NSInteger)newValue {
_currentId = newValue;
}
#end
Then you can access it like this:
YourClass.currentId = 1;
val = YourClass.currentId;
Here is a very interesting explanatory post I used as a reference to edit this old answer.
2011 Answer: (don't use this, it's terrible)
If you really really don't want to declare a global variable, there another option, maybe not very orthodox :-), but works... You can declare a "get&set" method like this, with an static variable inside:
+ (NSString*)testHolder:(NSString*)_test {
static NSString *test;
if(_test != nil) {
if(test != nil)
[test release];
test = [_test retain];
}
// if(test == nil)
// test = #"Initialize the var here if you need to";
return test;
}
So, if you need to get the value, just call:
NSString *testVal = [MyClass testHolder:nil]
And then, when you want to set it:
[MyClass testHolder:testVal]
In the case you want to be able to set this pseudo-static-var to nil, you can declare testHolder as this:
+ (NSString*)testHolderSet:(BOOL)shouldSet newValue:(NSString*)_test {
static NSString *test;
if(shouldSet) {
if(test != nil)
[test release];
test = [_test retain];
}
return test;
}
And two handy methods:
+ (NSString*)test {
return [MyClass testHolderSet:NO newValue:nil];
}
+ (void)setTest:(NSString*)_test {
[MyClass testHolderSet:YES newValue:_test];
}
Hope it helps! Good luck.
On your .m file, you can declare a variable as static:
static ClassName *variableName = nil;
Then you can initialize it on your +(void)initialize method.
Please note that this is a plain C static variable and is not static in the sense Java or C# consider it, but will yield similar results.
In your .m file, declare a file global variable:
static int currentID = 1;
then in your init routine, refernce that:
- (id) init
{
self = [super init];
if (self != nil) {
_myID = currentID++; // not thread safe
}
return self;
}
or if it needs to change at some other time (eg in your openConnection method), then increment it there. Remember it is not thread safe as is, you'll need to do syncronization (or better yet, use an atomic add) if there may be any threading issues.
As pgb said, there are no "class variables," only "instance variables." The objective-c way of doing class variables is a static global variable inside the .m file of the class. The "static" ensures that the variable can not be used outside of that file (i.e. it can't be extern).
Here would be an option:
+(int)getId{
static int id;
//Do anything you need to update the ID here
return id;
}
Note that this method will be the only method to access id, so you will have to update it somehow in this code.
(Strictly speaking not an answer to the question, but in my experience likely to be useful when looking for class variables)
A class method can often play many of the roles a class variable would in other languages (e.g. changed configuration during tests):
#interface MyCls: NSObject
+ (NSString*)theNameThing;
- (void)doTheThing;
#end
#implementation
+ (NSString*)theNameThing { return #"Something general"; }
- (void)doTheThing {
[SomeResource changeSomething:[self.class theNameThing]];
}
#end
#interface MySpecialCase: MyCls
#end
#implementation
+ (NSString*)theNameThing { return #"Something specific"; }
#end
Now, an object of class MyCls calls Resource:changeSomething: with the string #"Something general" upon a call to doTheThing:, but an object derived from MySpecialCase with the string #"Something specific".
u can rename the class as classA.mm and add C++ features in it.
Another possibility would be to have a little NSNumber subclass singleton.