nardo
nardo

Reputation: 149

Global variables objective-C not working

I need to create global variables in a class1, which will hold some coordinates, which I then want to use in a different class2. These variables are used and changed in a method in class1, I then need to call them in class2 after being used by the method in class1. My research has taken me to the “extern” definition but I’m not quite sure of how to get the value of these global variables in class 2. Any example code would be greatly appreciated.

Upvotes: 0

Views: 165

Answers (1)

Sergio
Sergio

Reputation: 1732

Create header and source file GlobalVariable.h and GlobalVariable.m

In GlobalVariable.h declare global variable like this: extern NSString * globalString;

In GlobalVariable.m put default value like this: NSString* globalString = @"global_string";

Put #import "GlobalVariable.h" in files where you want to use global variables, and you can access them like they are local variables.

Upvotes: 1

Related Questions