我正在处理一个非常基本的问题,我需要比较两个字典键,但是当我这样做时,它并没有给我正确的答案,不知何故我无法创建逻辑。
所以我在一个 plist 字典数组和来自服务器的 json 数组中有这个我想比较它们,如果 plist 中存在事件,如果不做另一件事,则做某事;
问题:
根据我的逻辑,如果 plist 中的两个事件等于来自服务器的其他两个事件,如果应该为来自服务器的每个字典打印逻辑,那么在这种情况下总共 2 次,但在我的代码中它被打印 4 次. 基本上每次都满足条件。
NSLOG:
evreka find the id
evreka find the id
evreka find the id
evreka find the id
来源: 在一个列表中;
createList (
{
"end_date" = "2013-07-24";
ends = "13:07:00";
"event_id" = 173;
"event_name" = Static;
location = Asdad;
"root_folder" = "dadapof@gmail.com";
"start_date" = "2013-07-24";
starts = "13:07:00";
"user_id" = 13;
},
{
"end_date" = "2013-07-25";
ends = "13:08:00";
"event_id" = 174;
"event_name" = "Event Delete";
location = Asdsad;
"root_folder" = "dadapof@gmail.com";
"start_date" = "2013-07-25";
starts = "13:08:00";
"user_id" = 13;
}
)
这来自服务器
responseobj (
{
"end_date" = "2013-07-24";
ends = "13:07:00";
"event_id" = 173;
"event_name" = Static;
location = Asdad;
"root_folder" = "dadapof@gmail.com";
"start_date" = "2013-07-24";
starts = "13:07:00";
"user_id" = 13;
},
{
"end_date" = "2013-07-25";
ends = "13:08:00";
"event_id" = 174;
"event_name" = "Event Delete";
location = Asdsad;
"root_folder" = "dadapof@gmail.com";
"start_date" = "2013-07-25";
starts = "13:08:00";
"user_id" = 13;
}
)
代码:
比较代码;
if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
createList= [NSMutableArray arrayWithContentsOfFile:dataPath];
NSLog(@"createList %@",createList);
NSLog(@"responseobj %@",responseObj);
//compare plist and response object
for (int i=0; i<[responseObj count]; i++) {
NSDictionary *dataDict = [responseObj objectAtIndex:i];
NSString *event_name =[dataDict objectForKey:@"event_name"];
//NSString *event_id =[dataDict objectForKey:@"event_id"];
BOOL eventExistInPlist=NO;
for (int j=0; j<[createList count]; j++) {
NSDictionary *createDict = [createList objectAtIndex:i];
//NSString *create_id =[createDict objectForKey:@"event_id"];
NSNumber *create_id =[createDict objectForKey:@"event_id"];
if ([[dataDict objectForKey:@"event_id"] isEqual:[createDict objectForKey:@"event_id"]]) {
// if (create_id==event_id) {
NSLog(@"evreka find the id");
我尝试过的: 我尝试将它们作为字符串(isEqualtoString)、数字(==)和 isEqual: 方法进行比较,它们都失败了。
我究竟做错了什么?