[__ 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
答案 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];并创建对象
试试这段代码