Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add dictionary data in to an array?

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
like image 787
Newbee Avatar asked Dec 15 '22 14:12

Newbee


2 Answers

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]];
like image 173
Anoop Vaidya Avatar answered Dec 29 '22 04:12

Anoop Vaidya


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

This might help you

like image 22
iAppDeveloper Avatar answered Dec 29 '22 04:12

iAppDeveloper