Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:04:27+00:00 2026-06-11T00:04:27+00:00

The goal, to create a class which contains an array of data to be

  • 0

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;


-(id)init;
@end

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;


-(id)init;
@end


#import "GlobalObjects.h"

@implementation GlobalObjects

@synthesize animals;

-(id)init{
    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;
}
@end

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:04:28+00:00Added an answer on June 11, 2026 at 12:04 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The goal is to create a mock class which behaves like a db resultset.
So assume I have one class named Program which contains my whole program of
I have a class LINE which contains two properties of type POINT. I would
I have an application that contains a business entity called a 'Task'. This entity
My goal is to create a trait that a case class can extend, which
My goal is to create a list from menu.bin. This is the func: pitem
I have a multi module maven web application, which uses hibernate. I use the
The preamble: I have designed a strongly interfaced and fully mockable data layer class
I have an ASP.NET web site which contains some ImageButton controls that cause postbacks
My goal is to load an external class in a running application environment (like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.