0

当我创建自定义类的实例时,我试图自动初始化一个数组:

Sections.h

#import <Foundation/Foundation.h>

@interface Sections : NSObject {
    NSMutableArray* allSections;
}
@property(nonatomic, strong) NSMutableArray* allSections;
@end

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

4

2 回答 2

1

不,您使用的是设置而不是吸气剂。此外,如果您将数组填充到init.

Sections.h

#import <Foundation/Foundation.h>

@interface Sections : NSObject {
    NSMutableArray* allSections;
}

@property(nonatomic, strong) NSMutableArray* allSections;

@end

截面.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

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

于 2012-10-10T11:15:37.267 回答
1

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];
}
于 2012-10-10T11:16:37.063 回答