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?

4
  • you never call setAllSections, do you? Commented Oct 10, 2012 at 11:14
  • no the point of my post was that i thought i wouldnt have to call the secallsections method. Can i set the array property in the sections class itself? Commented Oct 10, 2012 at 11:17
  • Why are you using [Section alloc] and not [[Section alloc] init]? You can't be sure that the superclass (whether or not it's NSObject) has properly initialised. Commented Oct 10, 2012 at 11:21
  • Also, I really hope you are using ARC. Commented Oct 10, 2012 at 11:21

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Do it in your class's init method : (And you do not need to override the setter)

-(id)init {
    if (self = [super init]) {
       [self initSections];
    }

    return self;
}

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

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

    [self.allSections addObject:sectionB];
}

2 Comments

great, thanks doesnt seem to be populating my allsections array. Do i need to change the init Sections *sections = [[Sections alloc] init]; ??
You should always call init on allocated objects to make sure they are initialized properly. Alloc by itself is not enough, it just allocates enough space for the object's data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.