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 8650721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:53:32+00:00 2026-06-12T13:53:32+00:00

I am trying to initialize an array automatically when i create an instance of

  • 0

I am trying to initialize an array automatically when i create an instance of a custom class:

Sections.h

#import <Foundation/Foundation.h>

@interface Sections : NSObject {
    NSMutableArray* allSections;
}
@property(nonatomic, strong) NSMutableArray* allSections;
@end

Sections.m

-(void)setAllSections:(NSMutableArray *)allSections {
    //--This method sets the array of all the sections available on the app.
    NSMutableArray *array = [[NSMutableArray alloc] init];
    Section* sectionA = [Section alloc];
    sectionA.htmlForExplanation = @"hello";
    sectionA.description = @"section a description";
    sectionA.name = @"section A";
    sectionA.SectionId = 1;

    [array addObject:sectionA];
    Section* sectionB = [Section alloc];
    sectionB.htmlForExplanation = @"hello";
    sectionB.description = @"section B description";
    sectionB.name = @"section B";
    sectionB.SectionId = 2;

    [array addObject:sectionB];
    [allSections setArray:array.mutableCopy];
}

so now when i create and instance of this i want to have a the pre-populated allSections array

- (void)viewDidLoad
{
    //GET DATA FOR SECTIONS POPULATE WEB VIEWS
    Sections *sections = [[Sections alloc] init];
    //ections setAllSections:<#(NSMutableArray *)#>]
    Section* sectionA = [sections.allSections objectAtIndex:0];
    Section* sectionB = [sections.allSections objectAtIndex:1];
    [webViewA loadHTMLString:sectionA.name baseURL:nil];
    [webViewB loadHTMLString:sectionB.name baseURL:nil];
    [super viewDidLoad];

}

however these objects appear to be empty? Is this the correct way to create an array automatically in objective-c?

  • 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-12T13:53:33+00:00Added an answer on June 12, 2026 at 1:53 pm

    No, you are using the setting and not the getter.
    Also it would be better if you fill the array in the init.

    Sections.h

    #import <Foundation/Foundation.h>
    
    @interface Sections : NSObject {
        NSMutableArray* allSections;
    }
    
    @property(nonatomic, strong) NSMutableArray* allSections;
    
    @end
    

    Sections.m

    #import "Sections.h"
    
    @implementation
    
    @synthesize allSections
    
    - (id) init {
       self = [super init];
    
       if (self) {
          allSections= [[NSMutableArray alloc] init];
    
          Section* sectionA = [Section alloc];
          sectionA.htmlForExplanation = @"hello";
          sectionA.description = @"section a description";
          sectionA.name = @"section A";
          sectionA.SectionId = 1;
          [allSections addObject:sectionA];
    
          Section* sectionB = [Section alloc];
          sectionB.htmlForExplanation = @"hello";
          sectionB.description = @"section B description";
          sectionB.name = @"section B";
          sectionB.SectionId = 2;
          [allSections addObject:sectionB];
       }
    
       return self;
    }
    
    @end
    

    As you see the implementation fo sections does not contain any setters or getters for the allSections. These are create for you with the @synthesize directive.

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

Sidebar

Related Questions

I'm trying to initialize and instance variable as an array as follows: class Arch
I am trying to initialize a node array in the node class, they are
How do I initialize a 2D array in Perl? I am trying the following
Can I declare an int array, then initialize it with chars? I'm trying to
Trying to initialize and set things up for a class as follows: class Accel
This is probably the easiest/lamest question. So I am trying to initialize an array
I am trying to initialize the following array of the following struct, but my
I am trying to do something really simple - initialize an array in Javascript.
I got this problem when trying to initialize global c++ matrix(2D array) inside a
I'm trying to initialize an array in matlab of ninety zero's. However, I don't

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.