NSDictionary中的奇怪键

时间:2014-12-04 09:33:54

标签: ios objective-c uitableview nsdictionary

我正在尝试按字母顺序对uitableview进行排序。因此我使用这个功能

-(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];
}

当我记下所有首字母时,我注意到了这些字母:

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 £

然后当我记录NSDictionary的键时,我看到了:

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
)

现在当我重新加载我的桌子时,应用程序崩溃了

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

出现此错误:

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

有人可以帮我这个吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

valueForKeyobjectForKey之间存在差异。

valueForKey是一个KVC方法,任何对象都可以找到您传入的名称的属性。

objectForKey是一种字典方法,用于在字典中查找字符串作为密钥对的字典方法。

更多信息:Difference between objectForKey and valueForKey?

您应该使用代码崩溃的objectForKey。如果找不到,objectForKey可以返回nilvalueForKey要求密钥出现在班级中......否则会崩溃。