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

  • Home
  • SEARCH
  • 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 4616864
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:02:35+00:00 2026-05-22T02:02:35+00:00

How do a populate an NSArray const? Or more generically how can I fix

  • 0

How do a populate an NSArray const? Or more generically how can I fix my code below to have an array constant (created in Constants.h & Constants.m) to be available to other parts of my code.

Was hoping to be able to access the constant as a static type object (i.e. as opposed to having to create an instance of constants.m and then access it) is this is possible.

I note the approach works OK for a string, but for NSArray the issue is populating the array.

Code:

constants.h

@interface Constants : NSObject {
}
extern NSArray  * const ArrayTest;
@end

#import “Constants.h”

    @implementation Constants

    NSArray  * const ArrayTest = [[[NSArray alloc] initWithObjects:@"SUN", @"MON", @"TUES", @"WED", @"THUR", @"FRI", @"SAT", nil] autorelease];   
    // ERROR - Initializer element is not a compile time constant

    @end
  • 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-05-22T02:02:36+00:00Added an answer on May 22, 2026 at 2:02 am

    The standard approach is to supply a class method that creates the array the first time it is requested and thereafter returns the same array. The array is never released.

    A simple, example solution is this:

    /* Interface */
    + (NSArray *)someValues;
    
    /* Implementation */
    + (NSArray *)someValues
    {
        static NSArray *sSomeValues;
        if (!sSomeValues) {
            sSomeValues = [[NSArray alloc]
                           initWithObjects:/*objects*/, (void *)nil];
        }
        return sSomeValues;
    }
    

    You can of course fancy this up with GCD instead of using an if:

    /* Implementation */
    + (NSArray *)someValues
    {
        static NSArray *sSomeValues;
        static dispatch_once_t sInitSomeValues;
        dispatch_once(&sInitSomeValues, ^{
            sSomeValues = [[NSArray alloc]
                           initWithObjects:/*objects*/, (void *)nil];
        });
        return sSomeValues;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the code below to populate an array from my ManagedObjectContext, but what
So in my view controller, I run code to populate an NSArray of Customer
I have a for loop as below which populates this data into an array.
I have an NSArray populated with 0's and 1's. How can I return the
I have a problem with using an NSArray to populate a UITableView. I'm sure
I am using cocos2d to populate a NSMutable Array, then creating an NSArray from
After writing code to populate textboxes from an object, such as: txtFirstName.Text = customer.FirstName;
I am trying to populate a UITableView with data from json result. I can
I am trying to use CoreData to populate a UITableView. I have been using
This is kinda confusing so please bear with me. I have an array (listOfStates)

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.