2012-10-10 21 views
0

我想,當我創建一個自定義的類的實例時自動初始化數組:在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中自動創建數組的正確方法嗎?

+0

你永遠不會調用setAllSections,你呢? –

+0

沒有我的文章的重點是,我想我不會必須調用secallsections方法。我可以在章節類本身中設置數組屬性嗎? – user987723

+0

你爲什麼使用'[Section alloc]'而不是'[[Section alloc] init]'?你不能確定超類(無論它是否是NSObject)已經正確初始化。 – dreamlax

回答

1

不,你正在使用的設置,而不是吸氣劑。 另外它會更好,如果你填充陣列在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 

正如你看到的FO部分實現不包含任何setter方法或者getter方法的allSections。這些是用@synthesize指令爲您創建的。

1

做在你的類的init方法:(而且你也不需要覆蓋的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]; 
} 
+0

太棒了,謝謝似乎沒有填充我的allsections陣列。我是否需要更改init Sections * sections = [[Sections alloc] init]; ?? – user987723

+0

您應該始終在分配的對象上調用init以確保它們已正確初始化。 Alloc本身是不夠的,它只是爲對象的數據分配足夠的空間 – giorashc