无法从预期将NSNumber作为输出的NSDictionary获得价值

时间:2011-10-24 15:51:43

标签: objective-c nsdictionary

我创建了一个类似以下的NSDictionary:

NSArray * alphabets = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",nil];

    alphaToNum = [NSMutableDictionary dictionary];
    numToAlpha = [NSMutableDictionary dictionary];

for(NSString* character in alphabets)
    {

        [alphaToNum setObject:[NSNumber numberWithInteger:index] forKey:character];
        [numToAlpha setObject:character forKey:[NSNumber numberWithInteger:index]];
        index++;
    }

现在我想访问“numToAlpha”,如下所示:

NSInteger code1;
NSNumber * nn = [numToAlpha objectForKey:code1];

我会得到错误,而在objectforkey的手册中它清楚地说(id)objectforkey(id)这意味着什么!

1 个答案:

答案 0 :(得分:2)

NSInteger不是对象。 id指的是任何对象类型。您需要在代码示例中使用[NSNumber numberWithInteger:code1]