我想,當我創建一個自定義的類的實例時自動初始化數組:在NSObject的創建陣列自動
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];
}
所以現在當我創建和實例這個我想有一個預填充的allSections數組
- (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];
}
但是,這些對象似乎是空的?這是在objective-c中自動創建數組的正確方法嗎?
你永遠不會調用setAllSections,你呢? –
沒有我的文章的重點是,我想我不會必須調用secallsections方法。我可以在章節類本身中設置數組屬性嗎? – user987723
你爲什麼使用'[Section alloc]'而不是'[[Section alloc] init]'?你不能確定超類(無論它是否是NSObject)已經正確初始化。 – dreamlax