4

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
0

3 Answers 3

9

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]];
Sign up to request clarification or add additional context in comments.

2 Comments

but if i am taking NSMutableArray instead of NSArray its not working.. can u elaborate a little bit. thank you.
No a big issue... whatever method NSArray contains are inherited by NSMutableArray.... in few cases you need to adjust a bit... I am updating my answer in few secs
3
for (NSDictionary *dictionary in arrayname)
{
    NSMutableArray *array = [dictionary objectForKey:@"writeKey"];
}

This might help you

Comments

0

If you want to access the values of Dictionary, use particular key for values. For that use the following format.

NSArray *yourarray=[YourDictionary ObjectForKey:@"KEY"];

I hope this will help you.

2 Comments

O of objectForKey should be small, and there will be only one object for a given key, it must be an array to store in yourArray.
the "ObjectForKey" will return any Object that may be array or one object, that's not a problem. If you want another solution, why don't you gor for "ObjectAtIndex" in Dictionary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.