getting the value of key in dictionary incorrectly

Discussion in 'iOS Programming' started by tranvutuan, Feb 28, 2012.

  1. macrumors member

    Joined:
    Dec 19, 2011
    #1
    What I am having so far right now is

    Code:
        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

    Code:
        pass is password
    
        value of pass is (null)
    Can anyone explain why the value is null.It should be abc,shouldn't it.
     
  2. macrumors newbie

    OverByThere

    Joined:
    Nov 24, 2011
    Location:
    Rugby, UK
    #2
    I think you want dictionaryWithObjects rather dictionaryWithObject, here is the code that (for me) works:
    Code:
    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.
     
  3. thread starter macrumors member

    Joined:
    Dec 19, 2011
    #3
    thanks for your help, i just tried and it worked
     

Share This Page