containsObject - 为什么这不起作用?

发布于 2024-08-29 13:21:46 字数 270 浏览 4 评论 0原文

我有一个数组,我试图检查是否存在 indexPath(.row)。

我使用以下代码:

if ([array containsObject:[NSNumber numberWithInt:indexPath.row]]){
    NSLog(@"Yep, it exists in there.");
}

该数组由数字 3、8 和 2 组成。索引路径在 a 中加载从 0 到 8 的数字。环形。

有人能明白为什么这不起作用吗?

I have an array that I am trying to check wether or not an indexPath(.row) exists in.

I use this code:

if ([array containsObject:[NSNumber numberWithInt:indexPath.row]]){
    NSLog(@"Yep, it exists in there.");
}

the array consist of the numbers 3, 8 and 2. The index path loads numbers fromm 0 to 8 in a loop.

Can anybody see why this doesen't work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心↖ 2024-09-05 13:21:46

由于数组包含字符串,因此您应该与字符串进行比较。要创建数字字符串,请使用-stringWithFormat:。所以:

if ([array containsObject:[NSString stringWithFormat:@"%d", indexPath.row]]){
    NSLog(@"Yep, it exists in there.");
}

更好的解决方案是将 NSNumber 存储在数组中。

Since the array contains strings, you should compare against strings. To create a numeric string, use -stringWithFormat:. So:

if ([array containsObject:[NSString stringWithFormat:@"%d", indexPath.row]]){
    NSLog(@"Yep, it exists in there.");
}

A better solution is to store NSNumber's in the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文