Sas*_*sha 0 objective-c nsdictionary
我有一个NSDictionary,我试图从中拉出一个字符串.出于某种原因,最后一个字符串似乎无法恢复(!?!).在下面的代码中,我检索labelString的NSString对象,完全没有问题.但是当我尝试为foo检索NSString时,我总是没有.但我没有看到差异 - 你能看出我做错了吗?
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:CellStyleLabelledStringCell], @"cellStyle",
@"name", @"fieldName",
@"Name", @"labelString",
foodItem.name, @"contentString",
@"foo", @"fookey",
nil];
NSString *string1 = (NSString *)[dict objectForKey:@"fookey"];
NSString *string2 = (NSString *)[dict objectForKey:@"labelString"];
NSLog(@"[%@][%@]", string1, string2);
Run Code Online (Sandbox Code Playgroud)
日志消息看起来像这样,并备份我在调试器中看到的内容(即string1为null):
2012-03-17 21:35:03.302 QuickList7 [8244:fb03] [(null)] [名称]
真的很困惑.提前致谢.
foodItem.name是nil,因此-[NSDictionary dictionaryWithObjectsAndKeys:]停在那里,并且不会将后续对象添加到字典中.
换句话说,就像你这样做:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:CellStyleLabelledStringCell], @"cellStyle",
@"name", @"fieldName",
@"Name", @"labelString",
nil];
Run Code Online (Sandbox Code Playgroud)
这就是为什么你必须小心任何nil作为"参数结束"哨兵的方法.
| 归档时间: |
|
| 查看次数: |
107 次 |
| 最近记录: |