我有一个荒谬的问题。
我有不同类型的值,它们插入字典中。问题在于字典中的值是有效的,内部的值为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"]);
日志
string 18 dictionary (null)
仅在第二个值上会出现此问题,第一个“名称”则不会。但是过程是一样的。
更奇怪的是,我在另一个类中使用的这段代码是相同的,并且运行良好。
现在我想知道。在Xcode中有问题吗?
非常感谢
最佳答案
使用initWithObjectsAndKeys
(或dictionaryWithObjectsAndKeys:
)创建字典时,nil
值指示参数的结尾。
无论您在参数列表中输入显式的nil
(就像每个人都在末尾一样),或者是否使用了带有nil
值的任何变量,都是如此。
NSSString *name = nil;
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:name, @"name", nil];
这给您一个空的字典,因为第一个参数是
nil
。甚至没有其他的论点被看过。
关于ios - NSDictionary中的值是有效的,内部为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15035605/