PDA

View Full Version : getting the value of key in dictionary incorrectly




tranvutuan
Feb 28, 2012, 08:31 AM
What I am having so far right now is

NSArray *keys = [NSArray arrayWithObjects:@"firstName",@"lastName",@"phoneNumber",@"email",@"password",nil];

NSArray *objects = [NSArray arrayWithObjects:@"nil",@"nil",@"nil",@"nil",@"abc",nil];

dictionary = [NSDictionary dictionaryWithObject:objects forKey:keys];

NSLog(@"pass is %@",[keys objectAtIndex:4]);

NSLog(@"value of pass is%@",[dictionary objectForKey:@"password"]);

However, What I got from the debugger is

pass is password

value of pass is (null)

Can anyone explain why the value is null.It should be abc,shouldn't it.



OverByThere
Feb 28, 2012, 09:02 AM
I think you want dictionaryWithObjects rather dictionaryWithObject, here is the code that (for me) works:
NSArray *keys = [NSArray arrayWithObjects:@"firstName",@"lastName",@"phoneNumber",@"email",@"password",nil];

NSArray *objects = [NSArray arrayWithObjects:@"nil",@"nil",@"nil",@"nil",@"abc",nil];

NSDictionary *dictionary = [[NSDictionary alloc] init];

dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSLog(@"pass is %@",[keys objectAtIndex:4]);

NSLog(@"value of pass is%@",[dictionary objectForKey:@"password"]);

Note I have initialised the Array in this version just so I can test it, you can remove that line.

tranvutuan
Feb 28, 2012, 09:04 AM
I think you want dictionaryWithObjects rather dictionaryWithObject, here is the code that (for me) works:
NSArray *keys = [NSArray arrayWithObjects:@"firstName",@"lastName",@"phoneNumber",@"email",@"password",nil];

NSArray *objects = [NSArray arrayWithObjects:@"nil",@"nil",@"nil",@"nil",@"abc",nil];

NSDictionary *dictionary = [[NSDictionary alloc] init];

dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSLog(@"pass is %@",[keys objectAtIndex:4]);

NSLog(@"value of pass is%@",[dictionary objectForKey:@"password"]);

Note I have initialised the Array in this version just so I can test it, you can remove that line.
thanks for your help, i just tried and it worked