gpt4 book ai didi

iphone - 如何检查*number*是否在数组中

转载 作者:行者123 更新时间:2023-12-03 19:02:37 25 4
gpt4 key购买 nike

非常基本的编程问题,我知道 PHP 有一个函数,但是 iPhone 操作系统有这个函数吗?

我想检查当前的indexPath是否是数组中的值。

PHP 示例:

<?php
$indexPath = 3;
$array = array("0", "1", "2", "3", "4");
if (in_array($indexPath, $array)) {
// Do something
}
?>

有人知道如何在 iOS 中做同样的事情吗?

最佳答案

containsObject:
Returns a Boolean value that indicates whether a given object is present in the receiver.

- (BOOL)containsObject:(id)anObject

例如:

if ([arrayofNumbers containsObject:[NSNumber numberWithInt:516]])
NSLog(@"WIN");

或者检查索引路径:

if ([arrayofIndexPaths containsObject:indexPath])
NSLog(@"Yup, we have it");

我应该澄清一下,NSIndexPath 不是一个数字,而是一系列数字,“表示嵌套数组集合树中特定节点的路径”,如 the developer documentation 中更详细地解释的那样。

关于iphone - 如何检查*number*是否在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2677243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com