【问题标题】:Unable to get an indexvalue of an object in nsmutablearray in iphone无法在 iphone 的 nsmutablearray 中获取对象的索引值
【发布时间】:2012-09-05 06:12:46
【问题描述】:

在我的应用程序中,我必须在某个地方获取特定对象的索引值。为此,我以如下所示的方式编码。

NSString *a=[NSString stringWithFormat:@"%d",[imagesdmobject.proofID intValue]];
NSLog(@"a value is %@",a);

这里的值为 56 现在我必须从 appdelegate.projectTitlesProofIDArray 获取 56 的索引。

int value=[appdelegate.projectTitlesProofIDArray indexOfObjectIdenticalTo:a];

appdelegate.projectTitlesProofIDArray 中的数据是:

41, 34, 35, 64, 45, 1、 67, 75, 76, 77, 78, 79, 80, 81, 82, 83, 85, 68, 86, 87, 88, 89, 90, 92, 69, 95, 97, 98, 100, 101, 70, 71, 72, 73, 74, 39, 37, 109, 108, 110, 38, 91, 93, 94, 36, 96, 99, 102, 103, 104, 105, 106, 107, 65, 66, 40, 46, 47, 48, 49, 50, 52, 53, 54, 60, 61, 62, 32, 55, 56, 57, 58, 59, 42, 43, 44, 63, 47, 48, 41, 34, 35, 64, 1、 91, 93, 94, 36, 96, 99, 102, 103, 104, 105, 106, 107, 65, 66, 40, 60, 61, 62, 32, 55, 56, 57, 58, 59, 54, 34, 35, 41, 34, 35, 64, 67, 75, 76, 77, 78, 79, 80, 81, 82, 83, 85, 68, 86, 87, 88, 89, 90, 92, 69, 95, 97, 98, 100, 101, 70, 71, 72, 73, 74, 39, 37, 109, 108, 110, 38, 91, 93, 94, 36, 96, 99, 102, 103, 104, 105, 106, 107, 65, 66, 40, 46, 47, 48, 49, 50, 52, 53, 54, 41, 34, 35, 64, 45, 1、 67, 75, 76, 77, 78, 79, 80, 81, 82, 83, 85, 68, 86, 87, 88, 89, 90, 92, 69, 95, 97, 98, 100, 101, 70, 71, 72, 73, 74, 39, 37, 109, 108, 110, 38, 91, 93, 94, 36, 96, 99, 102, 103, 104, 105, 106, 107, 65, 66, 40, 46, 47, 48, 49, 50, 52, 53, 54, 60, 61, 62, 32, 55, 56, 57, 58, 59, 42, 43, 44, 47, 34, 35, 39, 37, 109, 108, 110, 38, 46, 47, 48, 49, 50, 52, 53, 54, 42, 43, 44, 41, 34, 35, 64, 45, 1、 67, 75, 76, 77, 78, 79, 80, 81, 83, 85, 68, 86, 87, 88, 89, 90, 92, 69, 95, 97, 98, 100, 70, 71, 73, 74, 39, 37, 109, 108, 110, 38, 91, 94, 36, 96, 99, 102, 103, 104, 105, 106, 107, 65, 66, 40, 46, 47, 48, 49, 50, 52, 54, 60, 61, 62, 32, 55, 56, 57, 58, 59, 42, 43, 63, 41, 34, 35, 64, 45, 1、 67, 75, 76, 77, 78, 79, 80, 81, 82, 83, 85, 68, 86, 87, 88, 89, 90, 92, 69, 95, 97, 98, 100, 101, 70, 71, 72, 73, 74, 39, 37, 109, 108, 110, 38, 91, 93, 94, 36, 96, 99, 102, 103, 104, 105, 106, 107, 65, 66, 40, 46, 47, 48, 49, 50, 52, 53, 54, 60, 61, 62, 32, 55, 56, 57, 58, 59, 42, 43, 44, 63, 41, 34, 35, 64, 45, 1、 67, 75, 76, 77, 78, 79, 80, 81, 82, 83, 85, 68, 86, 87, 88, 89, 90, 92, 69, 95, 97, 98, 100, 101, 70, 71, 72, 73, 74, 39, 37, 109, 108, 110, 38, 91, 93, 94, 36, 96, 99, 102, 103, 104, 105, 106, 107, 65, 66, 40, 46, 47, 48, 49, 50, 52, 53, 54, 60, 61, 62, 32, 55, 56, 57, 58, 59, 42, 43, 44, 63, 46

56 在那里,但我得到的值不正确,为 2147483647。

请帮助我解决这个问题。我被困在这个地方了。

【问题讨论】:

  • 可能是您的数组在字符串中包含空格
  • 2147483647 是 NSNotFound - 不是垃圾值。
  • 使用 indexOfObject: 而不是 indexOfObjectIdenticalTo: (参见stackoverflow.com/q/3167849/1331515) 并确保将字符串与字符串或数字与数字进行比较。

标签: iphone nsarray


【解决方案1】:

你需要这样做:-

假设你的数组包含:-

[array addObject:@"1"];
[array addObject:@"2"];
[array addObject:@"3"];
[array addObject:@"4"];
[array addObject:@"5"];
[array addObject:@"6"];

现在你需要找到对象 5 的索引

if([array containsObject:@"5"])
{
int index=[array indexOfObject:@"5"];
}

【讨论】:

  • 嗨,吉普萨,非常感谢。即使我以这种方式写如下 int value=[appdelegate.projectTitlesProofIDArray indexOfObject:@"56"];那个时候我也得到了 garbade 的价值。
  • 您的 ppdelegate.projectTitlesProofIDArray 是否包含 sring 或其他一些数据类型,因为 @"56" 表示它是 nsstring,显示您如何在数组中添加数据。
  • 您需要发布如何在数组中添加数据的代码。
  • 您理解我的意思,提取索引的代码很好,数据类型不同或在数组中添加数据时存在某些问题。这就是为什么我要求您发布代码你在数组中添加数据吗?
  • hi 得到了输出。我所做的修改是 NSString *proofstr=[NSString stringWithFormat:@"%@",dmobject.proofID]; [appdelegate.projectTitlesProofIDArray addObject:proofstr];以这种方式,我将数据添加到数组中。这里 dmobject.proofid 是 nsstring 对象,但我从服务器获取的数据是 int,因为它发生了。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
相关资源
最近更新 更多