[英]An error: [Events objectForKey:]: unrecognized selector sent to instance 0x
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: 我是Xcode和Objective-C的新手,我正在尝试构建有关事件的应用程序,我在mainViewController中有两个按钮,一个用于显示今天的事件,另一个用于显示将来的事件,我的数据来自json文件[将包含几个键,其中之一是事件的日期],以及我通过将事件的日期与应用程序中日历的日期进行匹配来思考应用程序如何显示此键的方式,并且我使用了以下代码:
@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 ?? 但这给了我一个错误@ [_events objectForKey:@“ date”],这是完全有道理的,因为我必须在此操作中再次调用json数据,这就是我不知道该怎么做? 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. PS,有关我的json文件看起来如何像plz的更多详细信息,请参阅我之前发布的问题。
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 但这一次我收到一条错误消息->-[Events ObjectForKey:]:无法识别的选择器发送到实例0x74303f0
Could anyone plzzz tell me what is the problem now!! 谁能告诉我现在是什么问题! Im sooo confused ... 我太困惑了...
objectForKey:
is a NSDictionary
method, not a NSArray
method. objectForKey:
是NSDictionary
方法,而不是NSArray
方法。 To retrieve an event dictionary from the array, you would use objectAtIndex:
. 要从数组中检索事件字典,可以使用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 ( !=
). 比较字符串时,请使用isEqualToString:
而不要使用等于运算符( ==
)或不等于运算符( !=
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.