0

我正在处理一个非常基本的问题,我需要比较两个字典键,但是当我这样做时,它并没有给我正确的答案,不知何故我无法创建逻辑。

所以我在一个 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: 方法进行比较,它们都失败了。

我究竟做错了什么?

4

2 回答 2

3

 NSDictionary *createDict = [createList objectAtIndex:i];
                                                      ^----- HERE

您使用了错误的索引,它应该j代替i内部循环。

于 2013-07-24T18:00:18.787 回答
1

Martin R 的答案是您的问题所在。我会更改您的逻辑以包含 Objective-C 快速枚举而不是标准的 ++ 循环。我还将第二个 for 循环分离为 BOOL 方法,以便于阅读

for (NSDictionary *dataDict in responseObj) {
    NSString *event_name =[dataDict objectForKey:@"event_name"];
    if ([self matchFoundForEventId:event_name]) {
        NSLog(@"Match found");
    }
}

-(BOOL)matchFoundForEventId:(NSString *)eventId {
    for (NSDictionary *dataDict in createList) {
        NSString *createEvent = [dataDict objectForKey:@"event_id"];
        if ([eventId isEqualToString:createEvent]) {
            return YES;
        }
    }
    return NO;
}
于 2013-07-24T18:11:43.200 回答