Vins
Vins

Reputation: 1944

Value out of NSDictionary is valid, inside is null

I have a problem absurd.

I have the values ​​of different type, which insert in a dictionary. The problem is that the value out of the dictionary is valid, inside is null.

 NSString *name = [NSString stringWithFormat:@"%@",_nameTextField.text];
 NSString *icon = [NSString stringWithFormat:@"%i",itemIcon];

 NSDictionary *dictionary = [[NSDictionary alloc]initWithObjectsAndKeys:name,@"name",
icon,@"icon", nil];

NSLog(@"string %@ dictionary %@",icon,[dictionary objectForKey:@"icon"]);

LOG

string 18 dictionary (null)

This problem only occurs on the second value, the first "name" does not. Yet the process is the same.

What is more strange, this piece of code, I am using in another class, identical and it works well.

Now I wonder. Can be a problem in Xcode?

Thanks a lot

Upvotes: 1

Views: 471

Answers (1)

rmaddy
rmaddy

Reputation: 318924

When you create a dictionary using initWithObjectsAndKeys (or dictionaryWithObjectsAndKeys:), a nil value indicates the end of the arguments.

This is true whether you enter an explicit nil in the argument list (like everyone does at the end), or if any variable with a nil value is used.

NSSString *name = nil;
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:name, @"name", nil];

This gives you an empty dictionary because the first argument is nil. None of the other arguments are even looked at.

Upvotes: 7

Related Questions