0

I know this could be a noob question but I am a bit stucked here. I usualy makes the following to access app data in different ViewControllers: First I declare a global.h module like this

global.h

typedef struct {

    NSString *appName
    NSString *appVersion;

    bool mode;

} structApp;

extern structApp app;

After that I declare in MainViewController.h the struct so that I can access data

@implementation ViewController

structApp app;

- (void)viewDidLoad
{
    app.appVersion = @"v1.02";
}

@end

And then I include "global.h" in every ViewController.h

This way I can access globally. As far I can see this is a good implementation and I have used it in more than 20 apps. Problem starts when this struct grows in size. In those cases I see corrupted memory, nil variables that were previously loaded with data, etc.

There is a better way of making data available in all ViewController? Please give me some examples if you can.

Alejandro Luengo
  • 1,256
  • 1
  • 17
  • 27

5 Answers5

1

As you were using extern in your structure, any object updating the same value.

In OOPS, global variables are never said Good, so you need to use a singleton pattern.

Create a singleton/shared class having all those stuffs in your structure and use it.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
1

You have two options

  1. Use a singleton class - Refer Objective C Singleton
  2. Declare properties in App delegate - Refer SO

You can access the app delegate from any class using:

AppDelegate *appDel = [[UIApplication sharedApplication] delegate];
Community
  • 1
  • 1
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

You should deal with struct only if you deal with primitive data (if you are in a OOP way).

  app.appVersion = @"v1.02";

Make your struct pointing on dangling pointer, since you are pointing a data in a function scope (app.appVersion is only holding the pointer, not the data). So you must retain all those object values in order to make it content safe, but i must admit it is still a Q&D approach.

If you need global access to data, you can use a singleton, only if you really need strong encapsulation and control to data.

How to make a singleton What should my Objective-C singleton look like?

You can use macro too, that way you'll can use constants string without worrying data persistency, since they will always be available into the scope you are dealing with.

Community
  • 1
  • 1
Mr Bonjour
  • 3,330
  • 2
  • 23
  • 46
0

In General using struct should work fine. There is nothing wrong with using them. If you observe weird values caused by overlapping memory or illegal re-use of it or so then your problem is somewhere else but not in using structs in principle. The extern statement could lead to such an issue.

A class is not much more than a struct too, from a memory usage perspective. If I were you I would design a class with properties where ever you have members when using a struct. And make use of them in pretty the same way.

For "global variables" I apply a singleton pattern. That is basically a class with a class method (the leading + instead of -) that makes the one and only instance of the class available. Within that method I check if the class (a class internal static reference to the same class) is already available (!= nil) and instantiate it. Sometimes I use the initialize method for that. Initialize is an objective-c typical thing. It is called only once for each class, even subclassed ones, when or before the class is used for the first time. A very good place for instantiating class variables as singletons but not portable to other programming languages.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
0

If you only want to read the data and you dont need any complex data structure you can also use a settings file like

Settings.h

#define appName @"blabla"
#define appVersion @"1.01"
#define mode 1
pdrcabrod
  • 1,467
  • 1
  • 14
  • 22