Squirrel
Squirrel

Reputation: 1523

count of objects (1116) differs from count of keys (1117)

How do I ignore the last 2 lines that is in result because that line shows stats like:

---
1113 entries, 25351 bytes used, 7417 bytes free.

also i am not sure I get this error below: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSDictionary initWithObjects:forKeys:]: count of objects (1116) differs from count of keys (1117)'

NSString *result ...
NSArray *strings = [result componentsSeparatedByCharactersInSet:
                   [NSCharacterSet characterSetWithCharactersInString:@"=\n"]];

NSMutableArray *keys = [NSMutableArray new];
NSMutableArray *values = [NSMutableArray new];

  for (int i = 0; i < strings.count; i+=1) {
            if (i % 2 == 0) { // if i is even
                [keys addObject:strings[i]];
            }
            else {
                [values addObject:strings[i-1]];

            }
        }

Sample data:
NC_AllowedWebHosts=
NC_BgeLAN=br1
NC_Doc=/tmp/dhcpd
NC_ExPts=1863
NC_Redirect=1
[...]
bt_binary_custom=/path/to/binaries/directory
bt_blocklist=0
bt_blocklist_url=http://list.g.com/?list=bt_level1
bt_check=1

Upvotes: 2

Views: 452

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

You will get this error every time that strings.count is odd, because the number of times the loop sees i % 2 == 0 is greater than the number of times it sees i % 2 == 1.

Note that your loop will try inserting identical keys and values, because strings[i-1] when i is odd refers to the preceding strings that has an even index.

Here is how you can fix it:

for (int i = 0; i+1 < strings.count; i += 2) {
    [keys addObject:strings[i]];
    [values addObject:strings[i+1]];
}

Upvotes: 2

Related Questions