NSMutableDictionary使用objectAtIndex

时间:2014-02-25 22:14:19

标签: objective-c nsmutabledictionary

我是Xcode的新手,我创建了一个包含以下数据的NSMutableDictionary *dIc

密钥:ID,名称

数据:

  • ID = 1,姓名=约翰

  • ID = 2,姓名=史密斯

我想访问具有特定行的dIc。我希望这个结果例如:

姓名=史密斯。

我试过了:

-(void)initiateArrays
{
    dIc =[[NSMutableDictionary alloc] init];
}
- (void)viewDidLoad
{
    [self initiateArrays];
    [self callingWebSrvc];
    [super viewDidLoad];
}
-(void)callingWebSrvc
{

    NSInteger i=0;


    do {
        self.selectedRowId = [[mainMenuID objectAtIndex:i]integerValue];
        [self getItemListFromWebSrvc];
    i++;
    } while (i<= mainMenuData.count - 1);

}
-(void) getItemListFromWebSrvc
{
    //............ Request ....................................>>>
    .
    .
    .
    //............ Response ....................................>>>
    NSInteger i = 0;
    do {
        elm = [xmlDataTable objectAtIndex:i];
        //Set Object into Arrays

        [dIc setObject:[NSNumber numberWithInt:selectedRowId] forKey:@"Id"];
        [dIc setObject:[elm childNamed:@"Name"].value forKey:@"Name"];

        NSArray *keys = [rowArray allKeys];

        // values in foreach loop
        for (NSString *key in keys) {
            NSLog(@"%@ is %@",key, [dIc objectForKey:key ]);
        }

        i=i+1;
    } while (i <= xmlDataTable.count-1);
}
-(void)showResult
{
    NSString *anObject = [[dIc objectForKey:@"Name"  ] objectAtIndex:1];
    NSLog(@"The Value is = %@",anObject );
}

但它不起作用。

当我打电话给[self showResult];我收到以下错误:      - [__ NSCFString objectAtIndex:]:无法识别的选择器发送到实例0x8b9a7f0     2014-02-26 00:53:57.718 iWish [3311:70b] * 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFString objectAtIndex:]:无法识别的选择器发送到实例0x8b9a7f0 “

1 个答案:

答案 0 :(得分:1)

问题在于这一行:

NSString *anObject = [[dIc objectForKey:@"Name"  ] objectAtIndex:1];

本篇文章中的第一个对象[dIc objectForKey:@"Name" ]是一个NSString。然后你说objectAtIndex:1到一个NSString - 这是一个禁忌。