NSDictionary使用NSString键的奇怪行为

时间:2014-04-16 04:00:27

标签: ios objective-c nsstring nsdictionary

NSString *downloadedItemIDString = [dict objectForKey:@"id"];
NSLog(@"downloadedItemIDString = %@", downloadedItemIDString);

if([storedRecordsDict objectForKey:downloadedItemIDString] != nil)
      NSLog(@"1) Item WITH ID %@ EXISTS!", downloadedItemIDString);
if([storedRecordsDict objectForKey:@"6226691114401792"] != nil)
      NSLog(@"2) Item WITH ID 6226691114401792 EXISTS!");

记录:

  

2014-04-15 20:51:37.336 AppDemo [2201:60b] downloadedItemIDString = 6226691114401792
  2014-04-15 20:51:37.337 AppDemo [2201:60b] 2)项目ID为6226691114401792 EXISTS!

我是Obj C的新手,我一直试图解决这个问题几个小时..请帮助我理解为什么在提供NSString作为密钥时它找不到对象?

1 个答案:

答案 0 :(得分:0)

NSString *downloadedItemIDString = [dict objectForKey:@"id"];
NSLog(@"downloadedItemIDString = %@", downloadedItemIDString);
if([downloadedItemIDString isEqualToString:@"6226691114401792"])
{
    if([storedRecordsDict objectForKey:downloadedItemIDString] != nil)
        NSLog(@"1) Item WITH ID %@ EXISTS!", downloadedItemIDString);
if([storedRecordsDict objectForKey:@"6226691114401792"] != nil)
    NSLog(@"2) Item WITH ID 6226691114401792 EXISTS!");
}

首先检查你的downloadedItemIDString是否等于@“6226691114401792”,然后检查条件是否都会执行。