How to store data in class in iOS programming?

The goal, to create a class which contains an array of data to be used throughout the application by other classes.

I have this GlobalObject.h It declares the array to be used to store the data.

#import <Foundation/Foundation.h>

@interface GlobalObjects : NSObject
@property (retain) NSMutableArray *animals;


I have this GlobalObject.m. It contains the NSDictionary data and stores in to the array.

#import <Foundation/Foundation.h>

@interface GlobalObjects : NSObject
@property (retain) NSMutableArray *animals;


#import "GlobalObjects.h"

@implementation GlobalObjects

@synthesize animals;

    self = [super init];
    if (self) {
        // Define the data
        NSArray *imagesValue = [[[NSArray alloc] initWithObjects:@"dog.wav",@"cat.png",@"bird.png",nil] autorelease];
        NSArray *audioValue =[[[NSArray alloc] initWithObjects:@"dog.wav",@"cat.wav",@"bird.wav",nil] autorelease];
        NSArray *descriptionValue = [[[NSArray alloc] initWithObjects:@"Dog",@"Cat",@"Bird",nil] autorelease];

        // Store to array
        for (int i=0; i<8; i++) {
            NSDictionary *tempArr = [NSDictionary dictionaryWithObjectsAndKeys:[imagesValue objectAtIndex:i],@"image", [audioValue objectAtIndex:i],@"audio", [descriptionValue objectAtIndex:i], @"description", nil];
            [self.animals addObject:tempArr];
    return self;

Here's how I call it.

// someOtherClass.h
#import "GlobalObjects.h"
@property (nonatomic, retain) GlobalObjects *animalsData;

// someOtherClass.m
@synthesize animalsData;
self.animalsData = [[[GlobalObjects alloc] init] autorelease];
NSLog(@"Global Object %@ ",self.animalsData.animals);

Now the problem is, when I call this array in another class, it always returns null.

I'm new to iOS programming. So probably my method is wrong?


You forgot to allocate the animals array in the init method of "GlobalObjects":

self.animals = [[NSMutableArray alloc] init];

If you don't do this, self.animals is nil and addObject has no effect.

Since you do not use ARC, remember to release the array in dealloc.

EDIT: As @H2CO3 and @Bastian have noticed, I forgot my pre-ARC lessons. So the correct way to allocate self.animals in your init method is

self.animals = [[[NSMutableArray alloc] init] autorelease];

and in dealloc you have to add

self.animals = nil;

before calling [super dealloc]. I hope that I got it right now!

Yes, it's wrong - an instance variable isn't tied to a class itself, but to a particular instance of the class. The Cocoa-standard solution to this problem is creating a shared instance of the class - instead of

elf.animalsData = [[[GlobalObjects alloc] init] autorelease];


elf.animalsData = [GlobalObjects sharedInstance];

and implement the + sharedInstance method like this:

+ (id)sharedInstance
    static shared = nil;
    if (shared == nil)
        shared = [[self alloc] init];

    return shared;

As @MartinR pointed out, you make another mistake: you don't create the array you're adding objects to - then it remains nil, cancelling out the effect of all method calls on itself. You have to alloc-init a mutable array for it in the - init method.

Need Your Help

What does the number in parenthesis really mean?

sql mysql

I always thought that the number in the parenthesis represented the field lenght? However, I understand that is not always the case. Maybe it's a mysql issue? Someone told me if I set a field to 9

Git: how to check out the currently pushed branch in the pre-push hook?

git githooks

In my project I have a pre-push git hook that runs unit tests on every push.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.