加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 19:00:50 所属栏目:百科 来源:网络整理
导读:非常基本的编程问题,我知道 PHP有它的功能,但iPhone OS有吗? 我想检查当前indexPath是否是数组中的值. PHP示例: ?php$indexPath = 3;$array = array("0","1","2","3","4");if (in_array($indexPath,$array)) { // Do something}? 有谁知道如何在iOS中做同
非常基本的编程问题,我知道 PHP有它的功能,但iPhone OS有吗?

我想检查当前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");

或检查indexPath:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读