2

normally I hate going to people much more seasoned than I am at a programming language and try to gut it out, but I've been scratching my head over this one for quite some time. It's the ever popular

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* - [__NSArrayMutable .. insertObject:atIndex:]: object cannot be nil

I'm quite new to Xcode/Obj-C, so my understanding is limited. Essentially, I've been trying to modify Apple's 'Your Second iOS App' to better understand how everything is working together. The app is basically a modified version of their "BirdSighting" app. I checked this awesome site and found no concise answers for my answer in particular.

Anyways, I have added the exception breakpoints and it is throwing at the line:

[self.masterNewEventList addObject:event];

towards the bottom.

NewEventDataController.m

#import "NewEventDataController.h"
#import "NewEvent.h"

@interface NewEventDataController ()

- (void)initializeDefaultDataList;

@end

@implementation NewEventDataController

- (void)initializeDefaultDataList
{
    NSMutableArray *eventList = [[NSMutableArray alloc] init];

    self.masterNewEventList = eventList;

    NewEvent *event;

    NSDate *today = [NSDate date];

    event = [[NewEvent alloc] initWithName:@"Event" date:today];

    [self addNewEventWithEvent:event];
}

- (void)setMasterNewEventList:(NSMutableArray *)newList
{
    if (_masterNewEventList != newList)
        _masterNewEventList = [newList mutableCopy];
}

- (id)init
{
    if (self = [super init])
    {
        [self initializeDefaultDataList];

        return self;
    }

    return nil;
}

- (NSUInteger)countOfList
{
    return [self.masterNewEventList count];
}

- (NewEvent *)objectInListAtIndex:(NSUInteger)theIndex
{
    return [self.masterNewEventList objectAtIndex:theIndex];
}

- (void)addNewEventWithEvent:(NewEvent *)event
{
    [self.masterNewEventList addObject:event];
}

@end

My NewEvent.m is as follows:

#import "NewEvent.h"

@implementation NewEvent

-(id)initWithName:(NSString *)event date:(NSDate *)date
{
    self = [super init];

    if (self)
    {
        _event = event;

        _date = date;

        return self;

    }

    return nil;
}

@end

My line of thinking is that when it is trying to add the initial event (right before throwing), the event itself is not initialized properly. The debug reads:

event = (NewEvent *) 0x00000000

Thanks in advance for taking a look.

main.m 

#import <UIKit/UIKit.h>

#import "ParentAppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([ParentAppDelegate class]));
    }
}
4

2 回答 2

0

在 NewEvent 类的 initWithName:date 方法中,您需要在 if 块中返回 self 。在您的情况下,在此方法运行后,您总是返回 nil。

像这样:

-(id)initWithName:(NSString *)event date:(NSDate *)date
{
    self = [super init];

    if (self)
    {
        _event = event;

        _date = date;        
        //don't forget this line
        return self;
     }
     return nil;
}
于 2013-08-06T05:58:28.040 回答
0

问题出在您的 init 方法中。您应该在其中返回 self 。

 - (id)initWithName:(NSString *)event date:(NSDate *)date
    {
    self = [super init];

    if (self)
    {
        _event = event;
        _date = date; 
        return self;
    }

    return nil;
}
于 2013-08-06T06:03:52.990 回答