简体   繁体   中英

How to access the array dictionary values from mutableArray

how to get value from array of dictionary of key value array.

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

// set value to tableview cell.

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

when print the name key pair from the dictionary array from the mutable array which return nil object.

how to get name phone location values from the mutable array.

Your array formation is wrong Form array like this

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

NSLog(@"%@",listMutableArray);

And In cellForRowAtIndexPath try to print

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

You want to add multiple Object in array flow the below code

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

Your code is creating an array containing a single dictionary with an array as a key and value. I think you want an array of dictionaries; for example:

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",

                                  }

                             ];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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