how to add dictionary data in to an array?

26,011

Solution 1

Your question contains so many other questions, I will try to solve few of them related to converting dictionary to array.

  1. Store all keys

    NSArray *dictKeys=[dict allKeys];
    
  2. Store all values

    NSArray *dictValues=[dict allValues];
    
  3. If you want to store both keys and values:

    NSArray *keysAndValues=[dictKeys arrayByAddingObjectsFromArray:dictValues];
    

EDIT:

As you require NSMutableArray, you can use :

NSMutableArray *dictAllKeys=[NSMutableArray arrayWithArray:[dict allKeys]];
NSMutableArray *dictAllValues=[NSMutableArray arrayWithArray:[dict allValues]];
NSMutableArray *keysAndValues=[NSMutableArray arrayWithArray:[dictAllKeys arrayByAddingObjectsFromArray:dictAllValues]];

Solution 2

for (NSDictionary *dictionary in arrayname)
{
    NSMutableArray *array = [dictionary objectForKey:@"writeKey"];
}

This might help you

Share:
26,011
Newbee
Author by

Newbee

working as Software Developer

Updated on July 29, 2022

Comments

  • Newbee
    Newbee almost 2 years

    i have made an array and a dictionary. now i want to put the values of dictionary in to array.. i am very new to objective C so kindly help. "viewcontroller .m"

    #import "ViewController.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    
    - (void)viewDidLoad
    {
    [super viewDidLoad];
    
    marray = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4", nil];
    marray = [[NSMutableArray alloc]initWithObjects:@"6",@"7",@"8",@"9",@"10", nil];
    [self displayarray];
    [marray release];
    mDictionary = [[NSMutableDictionary alloc]init];
    [mDictionary setValue:@"sdfsdhfg" forKey:@"firstname"];
    [mDictionary setValue:@"kvxv" forKey:@"lastname"];
    [self displaydict];
    [mDictionary release];
    }
    -(void)displaydict{
    CFShow(mDictionary);
    }
    -(void)displayarray{
    for (int i = 0; i<[marray count]; i++) {
        NSLog(@"the array data present at %d index is %@",i,[marray objectAtIndex:i]);
    }
    }
    
    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    }
    
    
    @end