在尝试添加对象时,对象不能为零

时间:2017-10-14 10:24:08

标签: ios objective-c

[__ NSArrayM insertObject:atIndex:]:对象不能为零

问题是当尝试执行[_eventsArray addObject:eventModel];错误时发生错误。

eventModel为nil。

我该如何解决这个问题?

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *eventsArray;
@end

@interface EventsModel : NSObject
@property(nonatomic, assign) NSString  *title;
@property(nonatomic, strong) NSString  *details;

@end


@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
EventsModel *eventModel ;
eventModel.title = @"Meeting";
eventModel.details = @"some description";
[_eventsArray addObject:eventModel];
}

@end

#pragma mark - Model Class
@implementation EventsModel
-(instancetype)init{
self = [super init];
if (self) {
    self.title = nil;
    self.details =nil;
}
return self;
}

@end

1 个答案:

答案 0 :(得分:0)

EventsModel *model = [EventsModel new];

model.title = @"test";
model.details = @"Detail test";

EventsModel *model2 = [EventsModel new];

model2.title = @"test2";
model2.details = @"Detail test2";


[_eventsArray addObject:model];
[_eventsArray addObject:model2];


NSLog(@"%lu elements in array",(unsigned long)_eventsArray.count);

你使用EventsModel * model = [EvenntsModel new];并创建对象

试试这段代码