Pedro.Alonso
Pedro.Alonso

Reputation: 1005

My code is skipping the if end the else in Xcode obj-c

My issue is here, I have a code with several ifs and else, but the code does not enter anything and I have no idea why. Here:

 id msgWithFormat;

NSArray *allkeys = [dict allKeys];
NSMutableString *msg = [[NSMutableString alloc]init];
for (int i=0; i<allkeys.count; i++) {

    NSMutableString *listOfErrors = [[NSMutableString alloc]init];
    //NSMutableString *lbl = [[NSMutableString alloc]init];

    NSMutableDictionary *currentError = [dict objectForKey:allkeys[i]];
    NSMutableDictionary *isGroup;
    if ([currentError isKindOfClass:[NSMutableDictionary class]]) {
        isGroup = [currentError objectForKey:@"group"];
    }
    if ([isGroup count] == 0) {
        NSArray *lbls;
        if ([currentError isKindOfClass:[NSMutableDictionary class]]) {
            lbls = [currentError objectForKey:@"msg"];

            [listOfErrors appendString:[currentError objectForKey:@"label"]];
            [listOfErrors appendString:@":"];

            for (int j=0; j < lbls.count; j++) {
                [listOfErrors appendString:lbls[j]];
                [listOfErrors appendString:@","];

            }
            [msg appendString:listOfErrors];
            [msg appendString:@"\n"];
        } else if([[dict objectForKey:@"groupError"] isEqualToString:@"group 1"]) {

            lbls = [currentError objectForKey:@"msg"];

            [listOfErrors appendString:[currentError objectForKey:@"label"]];
            [listOfErrors appendString:@":"];

            for (int j=0; j < lbls.count; j++) {
                [listOfErrors appendString:lbls[j]];
                [listOfErrors appendString:@","];

            }
            [msg appendString:listOfErrors];
            [msg appendString:@"\n"];

        }

dict is a NSMutableDictionary I have checked, but it does not go into the isGroup if and neither to the value check if of group I have no clue as to why.

Here is dict:

dict    __NSDictionaryM *   3 key/value pairs   0x799f5390

Any help?

EDIT1:

I have placed a breakpoint in this line,

     if ([currentError isKindOfClass:[NSMutableDictionary class]]) {

it goes thru it and don't go in the else just jumps out of all this code:

     if ([currentError isKindOfClass:[NSMutableDictionary class]]) {
                lbls = [currentError objectForKey:@"msg"];

                [listOfErrors appendString:[currentError objectForKey:@"label"]];
                [listOfErrors appendString:@":"];

                for (int j=0; j < lbls.count; j++) {
                    [listOfErrors appendString:lbls[j]];
                    [listOfErrors appendString:@","];

                }
                [msg appendString:listOfErrors];
                [msg appendString:@"\n"];
            } else if([[dict objectForKey:@"groupError"] isEqualToString:@"group 1"]) {

//                lbls = [currentError objectFo rKey:@"msg"];
//                
//                [listOfErrors appendString:[currentError objectForKey:@"label"]];
//                [listOfErrors appendString:@":"];
//                
//                for (int j=0; j < lbls.count; j++) {
//                    [listOfErrors appendString:lbls[j]];
//                    [listOfErrors appendString:@","];
//                    
//                }
//                [msg appendString:listOfErrors];
//                [msg appendString:@"\n"];
//                
            }

Upvotes: 0

Views: 75

Answers (1)

Daniel Hall
Daniel Hall

Reputation: 13679

The most simple explanation I would suggest checking is if whether the condition in the "if" and the "else if" both evaluate to false at runtime.

It seems quite plausible that both could be false for a given input dictionary.

I would suggest breakpointing each condition, or logging the values under inspection in each condition to the console. If neither the "if" expression is true, nor the "else if" expression for those given values, then you have your answer!

Upvotes: 1

Related Questions