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?
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
Sections.m
As you see the implementation fo sections does not contain any setters or getters for the allSections. These are create for you with the
@synthesizedirective.