繁体   English   中英

如何从mutableArray访问数组字典值

[英]How to access the array dictionary values from mutableArray

如何从键值数组的字典数组中获取值。

-(void)viewDidLoad{
listMutableArray =[[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObject:[NSArray 
arrayWithObjects:@"kiran",@"1234512345",@"IN", nil] forKey:[NSArray 
arrayWithObjects:@"name",@"phone",@"location", nil]],nil];
}

//将值设置为tableview单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

// Custom cell declaration completed.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}
       NSLog(@"Value of name %@", [[listMutableArray objectAtIndex:indexPath.row] objectForKey:@"name"]);
}

当从返回nil对象的可变数组的字典数组中打印名称密钥对时。

如何从可变数组获取名称电话位置值。

您的数组格式错误这样的Form数组

NSMutableArray *listMutableArray =[[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"kiran",@"name",@"1234512345",@"phone",@"IN",@"location", nil], nil];

NSLog(@"%@",listMutableArray);

并在cellForRowAtIndexPath尝试打印

NSLog(@"Value of name %@", [[listMutableArray objectAtIndex:indexPath.row] objectForKey:@"name"]);

您想在数组流中添加多个对象,如下代码

NSDictionary *dict = @{
                       @"name": @"kiran",
                       @"phone": @"1234512345",
                       @"location": @"IN"
                       };

NSDictionary *dict1 = @{
                       @"name": @"Bala",
                       @"phone": @"12345123",
                       @"location": @"OUT"
                       };

NSDictionary *dict2 = @{
                       @"name": @"Sri",
                       @"phone": @"898989898",
                       @"location": @"IN"
                       };

NSMutableArray *listMutableArray =[[NSMutableArray alloc] initWithObjects:dict,dict1,dict2, nil];

您的代码正在创建一个包含单个字典的数组,该字典具有作为键和值的数组。 我认为您想要一系列词典; 例如:

listMutableArray = [NSMutableArray new];
[listMutableArray addObject:@{
    @"name": @"kiran",
    @"phone": @"1234512345",
    @"location": @"IN"
}];
We get all NSDictionary response add in jsonResult(NSDictionary) and create globel (arrData)NSMutableArray.

arrData = [NSMutableArray new];
for (int i =0; i < [jsonResult count]; i++)
    {
           NSString *string = [NSString stringWithFormat:@"%d",i];
[arrData addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[[jsonResult valueForKey:string] valueForKey:@"name"],@"name",[[jsonResult valueForKey:string] valueForKey:@"phone"],@"phone",[[jsonResult valueForKey:string] valueForKey:@"location"],@"location",nil]];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

// Custom cell declaration completed.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}
    NSLog(@"Value of name %@", [[arrData objectAtIndex:indexPath.row] valueForKey:@"name"]);
return cell;
}
NSArray *dictionary = @[
                              @{
                                @"Source_lat": @"28.6287",
                                @"Source_lon": @"77.3208",
                                @"Detail":@{
                                            @"Address": @"NOIDA",
                                            @"Region": @"NCR",
                                           },
                                },
                              @{
                                  @"Source_lat": @"28.628454",
                                  @"Source_lon": @"77.376945",

                                  }

                             ];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM