user987723
user987723

Reputation: 965

create array in nsobject automatically

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?

Upvotes: 0

Views: 1468

Answers (2)

rckoenes
rckoenes

Reputation: 69469

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.

Upvotes: 1

giorashc
giorashc

Reputation: 13713

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];
}

Upvotes: 1

Related Questions