【发布时间】:2010-04-18 04:15:43
【问题描述】:
我遇到NSDictionary 为NSString 返回null 的问题,即使该字符串在字典中。代码如下:
- (void)sourceDidChange:(NSNotification *)aNote {
NSDictionary *aDict = [aNote userInfo];
DLog(@"%@", aDict);
NSString *newSourceString = [aDict objectForKey:@"newSource"];
DLog(@"%@", newSourceString);
newSourceString = [newSourceString stringByReplacingOccurrencesOfString:@" " withString:@""];
DLog(@"%@", newSourceString);
NSString *inspectorString = [newSourceString stringByAppendingString:@"InspectorController"];
DLog(@"%@", inspectorString);
newSourceString = [newSourceString stringByAppendingString:@"ViewController"];
DLog(@"%@", newSourceString);
}
我得到以下日志语句:
2010-04-17 23:50:13.913 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] { newSource = "Second View"; }
2010-04-17 23:50:13.914 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] (null)
2010-04-17 23:50:13.916 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] (null)
2010-04-17 23:50:13.917 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] (null)
2010-04-17 23:50:13.917 CoreDataUISandbox[13417:a0f] -[RightViewController sourceDidChange:] (null)
如您所见,字符串在字典中的键 newSource 下,但是当我调用 objectForKey: 时,我得到了 null。我什至尝试过清理项目的后备选项。
有没有人遇到过这种情况,或者我只是忘记了一些非常基本的东西?
【问题讨论】:
-
出于好奇,在设置并记录
aDict之后,如果你这样做DLog(@"%@", [aDict allKeys]);会发生什么? -
这是实际代码吗?你有没有可能打错密钥?
DLog(@"%@", [aDict allKeys])给了什么? -
您可能还想尝试使用
%p而不是%@记录newSourceString。 -
我知道我做了一些昨晚找不到的蠢事。原来我已经修改了我在 iphoneincubator.com 上找到的 DLog 版本,没有列出行号,只是我只删除了 LINE 部分而不是 [Line %d] 部分。当然,后来我忘记了我已经这样做了。我不确定为什么它只会导致 NSStrings 有问题被记录,而不是 NSDictionary。感谢您的帮助。
标签: objective-c cocoa nsstring nsdictionary