0

I am trying to sort a uitableview alphabetically and also with sections. Therefore I use this function

-(void)updateName{
    sectionsNaam = [[NSMutableDictionary alloc] init]; ///Global Object

        BOOL found;

        for (Relation *tempRelation in resultsRelations)
        {
            NSString *c;
            if(tempRelation.rel_name.length == 0){
                continue;
            }else{
                c =  [NSString stringWithFormat:@"%@",[[tempRelation.rel_name substringToIndex:1] uppercaseString]];

            }

            found = NO;

            for (NSString *str in [sectionsNaam allKeys])
            {
                if ([str isEqualToString:c])
                {
                    found = YES;
                }
            }
            NSLog(@"STRIG IS %@",c);

            if (!found)
            {
                [sectionsNaam setValue:[[NSMutableArray alloc] init] forKey:c];
            }
        }
        for (Relation *tempRelation in resultsRelations)
        {
            if(tempRelation.rel_name.length == 0){
                continue;
            }else{
                [[sectionsNaam objectForKey:[[tempRelation.rel_name substringToIndex:1] uppercaseString]] addObject:tempRelation];
            }
        }
    NSLog(@"SECTIONS NAAM IS %@",[sectionsNaam allKeys]);


    _typeView = 100;
    [tableRelations reloadData];
}

When I log all the first letters I noticed these ones:

2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS '
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS  
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS  
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS "
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS &
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS (
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS (
2014-12-04 10:06:49.674 Adsolut[43298:8578408] STRIG IS (
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS (
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS @
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS @
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS @
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS \
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS ´
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS “
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS “
2014-12-04 10:06:49.675 Adsolut[43298:8578408] STRIG IS £

And then when I log the keys of the NSDictionary I see this:

SECTIONS NAAM IS (
    3,
    K,
    4,
    L,
    5,
    M,
    N,
    "\U00dc",
    "\U201c",
    7,
    " ",
    8,
    O,
    P,
    "\U00c7",
    Q,
    "\"",
    R,
    "\U00c9",
    S,
    T,
    U,
    "&",
    "\U00b4",
    V,
    "'",
    W,
    "(",
    "@",
    X,
    A,
    Y,
    B,
    Z,
    C,
    D,
    "\\",
    "\U00a3",
    E,
    F,
    "\U00d4",
    G,
    H,
    "\U00d6",
    1,
    I,
    2,
    J
)

Now when I reload my table the app crashes on this line

 rows = [[sectionsNaam valueForKey:[[[sectionsNaam allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];

With this error:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryM 0x7fa7a34c8f40> valueForUndefinedKey:]: this class is not key value coding-compliant for the key .'

Can anybody help me with this? Thanks in advance !

Steaphann
  • 2,797
  • 6
  • 50
  • 109

1 Answers1

0

There is a difference between valueForKey and objectForKey.

valueForKey is a KVC method for any object to find a property with the name you have passed in.

objectForKey is a dictionary method to find an object inside the dictionary with the string as its key pair.

more info: Difference between objectForKey and valueForKey?

You should be using objectForKey where your code is crashing. objectForKey can return nil if its not found. valueForKey requires that the key be present in the class ... or else crash.

Community
  • 1
  • 1
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56