自动在nsobject中创建数组

时间:2012-10-10 11:07:47

标签: objective-c ios xcode

我正在尝试在创建自定义类的实例时自动初始化数组:

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中自动创建数组的正确方法吗?

2 个答案:

答案 0 :(得分: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

如您所见,实现部分不包含allSections的任何setter或getter。这些是使用@synthesize指令为您创建的。

答案 1 :(得分: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];
}