addObjects仅向NSMutableArray添加最后一个对象

时间:2016-02-19 19:29:32

标签: ios objective-c arrays nsmutablearray

我正在创建一个Schedule类,并尝试在ViewController的循环中输出所有事件。我创建了一个Event类和Schedule类。我循环使用NSMutableArray来获取所有事件的文本。我的问题是当我在Schedule.m文件中的 addNewEvent 方法时,我用我通过 addObject 添加的最后一个对象覆盖我的NSMutable数组。我的目标是将所有事件对象逐个添加到数组中并显示其文本。如果我可以为事件对象设置单独的ID也会很好。我知道当我向可变数组添加对象时,我可能需要在 addNewEvent 方法中循环一些东西,但我不知道通过什么。我也想过,也许我在错误的地方初始化NSMutableArray(这是Schedule类的一个属性),但是如果我把初始化器移到其他地方就会出错。

你可以帮我解决这个问题。任何帮助将不胜感激!

我的代码如下。我有两个类(Event& Schedule)和一个ViewController文件。

Event.h:

@interface Event : NSObject

@property int eventId;

@property NSString * eventTitle;

@property NSString * eventDescription;

@property NSDate * eventDate;

-(void) logEvent;

-(NSString*) getEventText;

@end

Event.m:

#import "Event.h"

@implementation Event

-(NSString*) getEventText {

    NSString * eventText1 = [NSString stringWithFormat: @"\n\nEvent ID: %d.\n", _eventId];

    NSString * eventText2 = [eventText1 stringByAppendingFormat: @"Title: %@.\n", _eventTitle];

    NSString * eventText3 = [eventText2 stringByAppendingFormat:@"Description: %@.\n", _eventDescription];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterFullStyle;
    dateFormatter.timeStyle = NSDateFormatterShortStyle;

    NSString * eventText = [eventText3 stringByAppendingFormat:@"Date: %@.\n\n", [dateFormatter stringFromDate:_eventDate]];

    return eventText;
}

@end

Schedule.h:

@interface Schedule : NSObject

@property int idIndex;

@property NSMutableArray * scheduledEvents;

-(NSString*) getAllEventText;

-(void)addNewEventWithTitle:(NSString *)Title Description:(NSString *)Description andDate:(NSDate *)Date;

@end

Schedule.m:

#import "Schedule.h"
#import "Event.h"

@implementation Schedule

-(void)addNewEventWithTitle:(NSString *)Title Description:(NSString *)Description andDate:(NSDate *)Date {

    Event * event = [[Event alloc] init];

    _idIndex = 1;

    [event setEventTitle:Title];
    [event setEventDescription:Description];
    [event setEventDate:Date];
    [event setEventId:_idIndex];

    NSLog(@"%@", [event getEventText]);

    _scheduledEvents = [[NSMutableArray alloc] init];

    [_scheduledEvents addObject: event];

    NSLog(@"%lu", [_scheduledEvents count]);

}

-(NSString*) getAllEventText {

    NSString * allEventText;

    // loop through NSMutableArray calling each Event's getEventText method using a for loop

    for (int i=0; i<[_scheduledEvents count]; i++) {

        allEventText = [NSString stringWithFormat:@"Event %d: %@\n", i+1, [_scheduledEvents[i] getEventText] ];

        NSLog(@"%@", [_scheduledEvents[i] getEventText]);

    }

    // return concatenated string

    return allEventText;

}

@end

ViewController.m:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    Schedule * mySchedule = [[Schedule alloc] init];

    [mySchedule addNewEventWithTitle:@"My Birthday" Description:@"My most cherished birthday" andDate:[NSDate dateWithTimeIntervalSinceReferenceDate:484830000]];

    [mySchedule addNewEventWithTitle:@"Meeting with the Client" Description:@"My most important meeting" andDate:[NSDate dateWithTimeIntervalSinceReferenceDate:481302000]];

    [mySchedule addNewEventWithTitle:@"Appointment with Family Doctor" Description:@"My most urgent appointment" andDate:[NSDate dateWithTimeIntervalSinceReferenceDate:480270000]];

    [_outputTextView setText:[mySchedule getAllEventText]];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

3 个答案:

答案 0 :(得分:0)

每次调用addNewEventWithTitle:Description:andDate时,您都在重新初始化scheduledEvents。

您必须在viewDidLoad方法中移动_scheduledEvents = [[NSMutableArray alloc] init];

答案 1 :(得分:0)

问题在于这一行:

-(void)addNewEventWithTitle:(NSString *)Title Description:(NSString *)Description andDate:(NSDate *)Date {
    // ...
    _scheduledEvents = [[NSMutableArray alloc] init]; // <--
    // ...
}

根据定义,如果_scheduledEvents已经具有任何值(现有数组),则该行会销毁该值并将其替换为新的空数组。因此,运行addNewEvent...方法的结果总是_scheduledEvents最终只包含这一个事件。

答案 2 :(得分:0)

解决:问题出在 getAllEventText 方法中。

allEventText 字符串在以前的方法中不断替换for循环中的内容。

我必须将 NSString 更改为 NSMutableString 并使用追加方法添加到现有字符串,而不是替换其内容。

所以我按如下方式更改了方法,现在可以使用了:

-(NSString*) getAllEventText {

NSMutableString* allEventText = [NSMutableString stringWithCapacity:150];

// loop through NSMutableArray calling each Event's getEventText method using a for loop

for (int i=0; i<[_scheduledEvents count]; i++) {

    [allEventText appendFormat:@"Event %d: %@\n", i+1, [_scheduledEvents[i] getEventText]];

    NSLog(@"%@", [_scheduledEvents[i] getEventText]);

}

// return concatenated string

return allEventText;

}

同样非常感谢metronic和matt提示我需要将MSMutableArray初始化程序从 addNewEvent 方法移开!!