【问题标题】:NSMutableDictionary Key Value Checking ProblemsNSMutableDictionary 键值检查问题
【发布时间】:2011-11-17 05:30:19
【问题描述】:

我在 NSMutableDictionary 检索键和值检查时有点困惑,因为我做了这段代码。

    NSMutableDictionary *testDictionary=[NSMutableDictionary dictionary];
        //  NSMutableDictionary *testDictionary=[[NSMutableDictionary alloc]init];

        [testDictionary setObject:@"Muhammed Musthafa" forKey:@"10005"];
        [testDictionary setObject:@"Lubaba p" forKey:@"10006"];
        [testDictionary setObject:@"Sahala Banu" forKey:@"10007"];
        [testDictionary setObject:@"Vishnu k" forKey:@"10008"];
        [testDictionary setObject:@"Shabeer PP" forKey:@"10009"];
        [testDictionary setObject:@"Sabeehath " forKey:@"10010"];
        [testDictionary setObject:@"Anusha GS" forKey:@"10011"];
        [testDictionary setObject:@"JINI KS" forKey:@"10012"];
        [testDictionary setObject:@"Athulya T" forKey:@"10013"];
        [testDictionary setObject:@"Midhun KS" forKey:@"10014"];

        for(NSString *kk in testDictionary)
        {
            NSString *fName;
            if(kk==@"10006")
            {
                fName=[testDictionary objectForKey:kk];

            }
            if(kk==@"10007")
            {
                fName=[testDictionary objectForKey:kk];

            }
            if(kk==@"10013")
            {
                fName=[testDictionary objectForKey:kk];
            }
            NSLog(@"KEY=====%@ and  Value====%@",kk,fName);

        }
The out put value :

    2011-11-17 10:49:37.054 ArchiveData[2539:207] KEY=====10013 and  Value====
    2011-11-17 10:49:37.055 ArchiveData[2539:207] KEY=====10006 and  Value====
    2011-11-17 10:49:37.056 ArchiveData[2539:207] KEY=====10008 and  Value====
    2011-11-17 10:49:37.056 ArchiveData[2539:207] KEY=====10010 and  Value====
    2011-11-17 10:49:37.057 ArchiveData[2539:207] KEY=====10012 and  Value====
    2011-11-17 10:49:37.057 ArchiveData[2539:207] KEY=====10014 and  Value====
    2011-11-17 10:49:37.058 ArchiveData[2539:207] KEY=====10005 and  Value====
    2011-11-17 10:49:37.059 ArchiveData[2539:207] KEY=====10007 and  Value====
    2011-11-17 10:49:37.060 ArchiveData[2539:207] KEY=====10009 and  Value====
    2011-11-17 10:49:37.061 ArchiveData[2539:207] KEY=====10011 and  Value====
  1. 为什么这三个 {(kk==@"10006") if(kk==@"10007") if(kk==@"10013")} if 语句返回 false?

  2. 还有其他方法可以像这样检查id key吗?

  3. 我看到相同类型的编码在其他一些编码中工作正常,所以这取决于任何其他配置?

  4. 如何检查特定的 key==@"some Key value"?

【问题讨论】:

  • 我无法重现您的问题。您的代码中可能还有其他内容没有在问题中显示。

标签: objective-c xcode4 if-statement osx-snow-leopard nsmutabledictionary


【解决方案1】:

您最好使用[kk isEqualToString:@"10006"] 比较字符串,以便进行正确的字符串比较而不是指针比较。

【讨论】:

  • +1 - 另外,请参阅此链接以更好地解释字符串文字:stackoverflow.com/questions/8032375/…
  • 当我使用 [kk isEqualToString:@"10006"] 时出现相同的输出?所以问题是别的东西......
【解决方案2】:

代码:

NSMutableDictionary *testDictionary=[NSMutableDictionary dictionary];
    //  NSMutableDictionary *testDictionary=[[NSMutableDictionary alloc]init];

    [testDictionary setObject:@"Muhammed Musthafa" forKey:@"10005"];
    [testDictionary setObject:@"Lubaba p" forKey:@"10006"];
    [testDictionary setObject:@"Sahala Banu" forKey:@"10007"];
    [testDictionary setObject:@"Vishnu k" forKey:@"10008"];
    [testDictionary setObject:@"Shabeer PP" forKey:@"10009"];
    [testDictionary setObject:@"Sabeehath " forKey:@"10010"];
    [testDictionary setObject:@"Anusha GS" forKey:@"10011"];
    [testDictionary setObject:@"JINI KS" forKey:@"10012"];
    [testDictionary setObject:@"Athulya T" forKey:@"10013"];
    [testDictionary setObject:@"Midhun KS" forKey:@"10014"];

    for(NSString *kk in testDictionary)
    {
        NSString *fName;
        if([kk isEqualToString:@"10006"])
        {
            fName=[testDictionary objectForKey:kk];

        }
        if([kk isEqualToString: @"10007"])
        {
            fName=[testDictionary objectForKey:kk];

        }
        if([kk isEqualToString:@"10013"])
        {
            fName=[testDictionary objectForKey:kk];
        }
        NSLog(@"KEY=====%@ and  Value====%@",kk,fName);

    }

您的代码的输出。

2011-11-17 12:47:59.910 test[12512:a0f] KEY=====10008 and  Value====(null)
2011-11-17 12:47:59.910 test[12512:a0f] KEY=====10010 and  Value====(null)
2011-11-17 12:47:59.911 test[12512:a0f] KEY=====10012 and  Value====(null)
2011-11-17 12:47:59.911 test[12512:a0f] KEY=====10014 and  Value====(null)
2011-11-17 12:47:59.912 test[12512:a0f] KEY=====10005 and  Value====(null)
2011-11-17 12:47:59.912 test[12512:a0f] KEY=====10007 and  Value====Sahala Banu
2011-11-17 12:47:59.912 test[12512:a0f] KEY=====10009 and  Value====Sahala Banu
2011-11-17 12:47:59.913 test[12512:a0f] KEY=====10011 and  Value====Sahala Banu
2011-11-17 12:47:59.913 test[12512:a0f] KEY=====10013 and  Value====Athulya T
2011-11-17 12:47:59.913 test[12512:a0f] KEY=====10006 and  Value====Lubaba p  

它工作正常。 在 10007 之后,您没有 10009 和 10011 的条件。因此 fName 值不会因此而更改。
当您使用 == 运算符时,您是在比较指针值。这仅在您要比较的对象是完全相同的对象、位于相同的内存地址时才有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-03
    • 2015-01-23
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    相关资源
    最近更新 更多