Nicholas Hemstreet
Nicholas Hemstreet

Reputation: 33

I can't add my data object to my NSArray

So I have this method:

-(void)addLaneToRacingLanes:(UITapGestureRecognizer*)sender{


    laneDataObject *data=[self.laneDataObjects objectAtIndex:sender.view.tag];
    [self.racingLanes addObject:data];


    NSLog(@"%i",self.racingLanes.count);
    [sender.view setBackgroundColor:[UIColor yellowColor]];

}

It uses the tag from the senders view to find out which data object corresponds to that view.I'm using this to add to my racingLanes which is how I update these views, but my problem is that for some reason I cant add my laneDataObjects to my array racingLanes. Any ideas?

This is how the properties are set up:

@property (strong,nonatomic)NSArray *laneDataObjects;
@property (strong,nonatomic) NSMutableArray *racingLanes;

I have already run through the tags and they all work. The tags work such that lane 1 is tag 0 with its data object at 0, then lane 2 is tag 1 and its data is 1, so on and so forth. I already pre-tested this. And I have checked that both the laneDataObject array has been properly set up. Is it because my racingLanes isn't using a custom getter or setter? How would I go about changing that? Incase it matters I used

NSLog(@" %i",self.racingLanes.count);

to find out if the array was empty.

Upvotes: 3

Views: 203

Answers (2)

MJN
MJN

Reputation: 10808

Did you make sure to initialize your NSMutableArray in your class's -init or -viewDidLoad function?

// WITH ARC
self.racingLanes = [NSMutableArray array];

// WITHOUT ARC
self.racingLanes = [[NSMutableArray alloc] init];

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

It is a near certainty that the racingLanes has not been initialized: since the objects that you are adding are non-nil (you'd see an exception thrown otherwise) the racingLanes must be nil then.

You need to set racingLanes to NSMutableArray in the designated initializer:

_racingLanes = [NSMutableArray array];

Upvotes: 1

Related Questions