An error: [Events objectForKey:]: unrecognized selector sent to instance 0x

225 views Asked by At

Im quiet new to Xcode and objective-C and Im trying to build this app about events, I have two buttons in my mainViewController one to show today's events and the other will show any future events, my data are coming from a json file [which will include several keys one of them is the date of the event] and the way I thought about how the app will show this by matching the date of the event with the date of the calendar within the app and and I used this code:

 @implementation MV_HomeViewController {
 NSArray *_events;
 }



.....
.......
 - (IBAction)upcomingEvents:(id)sender {

 //  Events *event = [_events object:.row];

//Events *event = [_events objectForKey:@"Events"];

NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-YYYY"];
NSString *dateString = [dateFormatter stringFromDate:currDate];

if([_events objectForKey:@"date"] != dateString){

    [[self myTableView] reloadData];
 }

}

but it is giving me an error @ [_events objectForKey:@"date"] which is totally make sense because I have to call my json data again within this action and that is I don't know how to do ?? can anyone plz tell me how to do that or how to make the above code works for my app? I would be very appreciated.

PS, for more details about how my json file looks like plz see the previous question that I have posted.

Thanks,

I tried the implement the answer below but this with this code:

  for (NSDictionary *event in _events) {
    // Now you can use [event objectForKey:@"date"] to retrieve the event date
    if ([[event objectForKey:@"date"]  isEqualToString:dateString]){
    [[self myTableView] reloadData];
    }

  }

but this time Im getting an error saying --> -[Events objectForKey:]: unrecognized selector sent to instance 0x74303f0

Could anyone plzzz tell me what is the problem now!! Im sooo confused ...

1

There are 1 answers

2
neilco On

objectForKey: is a NSDictionary method, not a NSArray method. To retrieve an event dictionary from the array, you would use objectAtIndex:. If you want to loop over all the event dictionaries in the array, you can use fast enumeration like so:

for (NSDictionary *event in _events) {
    // Now you can use [event objectForKey:@"date"] to retrieve the event date 
}

When comparing strings, use isEqualToString: rather than the equality operator (==) or inequality operator (!=).