【问题标题】:IOS how to compare dictionary array item in the index with another NSarray index itemIOS如何将索引中的字典数组项与另一个NSarray索引项进行比较
【发布时间】:2015-11-23 03:47:20
【问题描述】:

我有一个名为“feeds”的字典项。在提要中,我有一个名为“SignatureNumber”的键。这个键里面包含一个数组项的列表。

当我注销它时,它看起来像这样。

Final signature : (
        {
        SignatureNumber = 7075274256;
    }
)

现在,我有另一个 NSarray 调用“信息”,其中包含项目列表。我想比较这两个数组的索引内的项目。

目前,这是我的代码。

   for (int i=0; i <information.count; i++) {
   for (NSDictionary *feed in feeds) {

    if ([feed[@"SignatureNumber"] containsObject:[information objectAtIndex:i]]) {
        badIPCount++;
    }else{
        goodIPCount++;
    }
   }
}

错误是我拥有的此代码未将“提要”中的项目与“信息”进行比较。为什么?请帮我调整我的代码。谢谢。

【问题讨论】:

  • 能否请您显示完整的feedsinformation 日志?
  • 提要日志在上面。关于信息数据是:NSarray deepsightSig = [NSArray arrayWithObjects:@"2802504",@"752286",@"752288",@"752287",@"3763718",@"696585",@"752285",@" 7075274256",无];
  • 我的提要是一个 NSmutable 数组,其中包含带有键调用 SignatureNumber 的字典
  • 代码运行顺利,但当两个数组包含相同的项目时没有命中。
  • 你应该使用2个循环,1个用于feeds,1个用于information

标签: ios arrays for-loop nsdictionary


【解决方案1】:
for (NSDictionary * dict in feeds) {

  for (NSNumber * number in deepsightSig) {

    long idFeed = [dict[@"SignatureNumber"] longValue];  /// if your id is too large, using NSString instead.
    long idSig = [number longValue];   /// like NSString = [number stringValue];

    if (idFeed == idSig) { /// if using NSString, it should be: "if ([idFeed isEqualToString:idSig])"
      badIPCount++;
    }
    else {
      goodIPCount++;
    }
  }
}

【讨论】:

  • 它给出了一个错误提示:array subscript is not an integer on line"long idFeed = [dict[@"SignatureNumber"] longValue]; "
  • 请问for (NSDictionary * dict in feeds)之后的NSLog(@"%@",dict),给我看看结果
  • 对不起。上面的代码是比较数组里面的数据还是索引号?
  • 我的代码?我的代码比较了SignatureNumber = 7075274256; 中的数字和deepsightSig 中的数字。如果我误解了,请详细说明您的问题。
猜你喜欢
  • 2021-04-17
  • 2017-07-03
  • 2014-01-03
  • 2016-07-06
  • 2019-06-23
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多