How to store { "Abc" :"Xyz" } in NSDictionary in Objective C

68 views Asked by At

How to store the given below into NSDictionary

{

"Test":"Example"

}

2

There are 2 answers

0
Ajharudeen khan On

You can add value for key by using this

NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:@"Test",@"Example", nil];
0
yigitture26 On

You can add value for key by using 'initWithObjects:(objects) forKeys:'

NSDictionary* dictionary = [[NSDictionary alloc] initWithObjects:@[@"Example",@"Example2"] forKeys:@[@"Test",@"Test2"]];
NSLog(@"%@",dictionary);
//{
//    Test = Example;
//    Test2 = Example2;
//}

Or you can use NSMutableDictionary :

 NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
[mutableDictionary setObject:@"Example" forKey:@"Test"];
[mutableDictionary setObject:@"Example2" forKey:@"Test2"];
NSLog(@"%@",mutableDictionary);
//{
//    Test = Example;
//    Test2 = Example2;
//}

Same output for these. Also you should take a look