NSMutableDictionary using objectAtIndex



我是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 对象索引:]:发送到实例0x8b9a7f0的无法识别的选择器 2014-02-26 00:53:57.718 iWish[3311:70b] * 由于未捕获的异常"NSInvalidArgumentException"而终止应用程序,原因:"-[__NSCFString对象索引:]:无法识别的选择器发送到实例0x8b9a7f0"

问题是这一行:

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

这个故事中的第一个对象,[dIc objectForKey:@"Name" ],是一个NSString。然后你对NSString说objectAtIndex:1 - 这是一个禁忌。

最新更新