Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何查找数组是否包含对象(不使用for循环)?_Iphone_Objective C_Ios_Nsarray - Fatal编程技术网

Iphone 如何查找数组是否包含对象(不使用for循环)?

Iphone 如何查找数组是否包含对象(不使用for循环)?,iphone,objective-c,ios,nsarray,Iphone,Objective C,Ios,Nsarray,我用下面的代码检查ary_navigationControllerViews数组是否包含myclass对象,但我需要使用out for循环。我知道我们有像containsObject这样的方法,但在这种情况下如何使用。是否有任何方法可以在不使用for循环的情况下检查此条件 NSArray *ary_navigationControllerViews = [[NSArray alloc] initWithArray:[self.navigationController viewController

我用下面的代码检查ary_navigationControllerViews数组是否包含myclass对象,但我需要使用out for循环。我知道我们有像containsObject这样的方法,但在这种情况下如何使用。是否有任何方法可以在不使用for循环的情况下检查此条件

NSArray *ary_navigationControllerViews = [[NSArray alloc] initWithArray:[self.navigationController viewControllers]];
    for(id obj_viewController in ary_navigationControllerViews) {
        if([obj_viewController isKindOfClass:[myClass  class]]) {
            //some my code
            return;
        }
    }
可以使用块:

NSArray *arr = [NSArray arrayWithObjects:[NSString stringWithFormat:@"test"], [NSNumber numberWithInt:10], [NSNull null], nil];
__block int index = NSNotFound;
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[NSNumber class]])
    {
        index = idx;
        *stop = YES;
    }
}];
if (index != NSNotFound)
{
    NSLog(@"contains at %d", index);
}
例如,在您的情况下:

[ary_navigationControllerViews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[myClass class]])
    {
        //some my code
        *stop = YES;
    }
}];
但我不认为这会降低yout代码的复杂性。循环是最简单的循环

您可以使用块:

NSArray *arr = [NSArray arrayWithObjects:[NSString stringWithFormat:@"test"], [NSNumber numberWithInt:10], [NSNull null], nil];
__block int index = NSNotFound;
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[NSNumber class]])
    {
        index = idx;
        *stop = YES;
    }
}];
if (index != NSNotFound)
{
    NSLog(@"contains at %d", index);
}
例如,在您的情况下:

[ary_navigationControllerViews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[myClass class]])
    {
        //some my code
        *stop = YES;
    }
}];

但我不认为这会降低yout代码的复杂性。循环是最简单的一个

或者,根据Nekto的回答,您也可以直接返回索引,如下所示:

NSArray *arr = [NSArray arrayWithObjects:[NSString stringWithFormat:@"test"], [NSNumber numberWithInt:10], [NSNull null], nil];

int index = [arr indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[NSNumber class]])
    {
        *stop = YES;
       return YES;
    }

    return NO;
}];
if (index != NSNotFound) 
{
    NSLog(@"contains at %d", index);
}

享受。

或者,根据Nekto的回答,您也可以直接返回索引,如下所示:

NSArray *arr = [NSArray arrayWithObjects:[NSString stringWithFormat:@"test"], [NSNumber numberWithInt:10], [NSNull null], nil];

int index = [arr indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[NSNumber class]])
    {
        *stop = YES;
       return YES;
    }

    return NO;
}];
if (index != NSNotFound) 
{
    NSLog(@"contains at %d", index);
}

享受。

如果您真的不能使用for循环,那么您必须使用类似
indexesOfObjectsPassingTest:
的方法,该方法将块应用于数组中的每个对象,并返回数组中所有项的
NSIndexSet
。然后,您可以使用它返回一个新数组,其中只包含通过测试的对象:

check = ^ (id obj, NSUInteger idx, BOOL *stop) 
{
    return [obj isKindOfClass:[myClass class]];
};

NSIndexSet *objectsInMyClass = [[self.navigationController viewControllers] indexesOfObjectsPassingTest:check];

NSArray *filteredArray = [[self.navigationController viewControllers] objectsAtIndexes:objectsInMyClass];

然后,您可以使用
makeObjectsPerformSelector
或类似工具来实际执行特定代码

如果确实无法使用for循环,则必须使用类似
indexesOfObjectsPassingTest:
的方法,该方法将块应用于数组中的每个对象,并返回数组中所有项的
NSIndexSet
。然后,您可以使用它返回一个新数组,其中只包含通过测试的对象:

check = ^ (id obj, NSUInteger idx, BOOL *stop) 
{
    return [obj isKindOfClass:[myClass class]];
};

NSIndexSet *objectsInMyClass = [[self.navigationController viewControllers] indexesOfObjectsPassingTest:check];

NSArray *filteredArray = [[self.navigationController viewControllers] objectsAtIndexes:objectsInMyClass];
然后,您可以使用
makeObjectsPerformSelector
或类似工具来实际执行特定代码

NSPredicate做这类工作要简单得多

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF.class.description == %@", [[myClass class] description]];
BOOL exists =  [predicate evaluateWithObject:ary_navigationControllerViews];
if (exists) {
    //some my code
    return;
}
要获取视图控制器实例,可以使用以下代码

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.class.description == %@", [[self class] description]];
NSArray *vcs = [ary_navigationControllerViews filteredArrayUsingPredicate:predicate];
if ([vcs count] > 0) {
    id vc = [vcs objectAtIndex:0];
    // Now vc is the view controller you are looking for
}
NSPredicate做这类工作要简单得多

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF.class.description == %@", [[myClass class] description]];
BOOL exists =  [predicate evaluateWithObject:ary_navigationControllerViews];
if (exists) {
    //some my code
    return;
}
要获取视图控制器实例,可以使用以下代码

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.class.description == %@", [[self class] description]];
NSArray *vcs = [ary_navigationControllerViews filteredArrayUsingPredicate:predicate];
if ([vcs count] > 0) {
    id vc = [vcs objectAtIndex:0];
    // Now vc is the view controller you are looking for
}


为什么不能使用for循环?为了降低代码的复杂性,for循环和其他东西一样好/复杂。@Akshay是对的-看看答案,然后看看问题中的代码-哪一个看起来更简单?我觉得NSPredicate比for循环更简单!为什么不能使用for循环?为了降低代码的复杂性,for循环和其他东西一样好/复杂。@Akshay是对的-看看答案,然后看看问题中的代码-哪一个看起来更简单?我觉得NSPredicate比for循环更简单!你为什么复制并粘贴我的答案?您没有添加任何重大更改Shey Nekto,我将使用的方法更改为indexOfObjectPassingTest:它直接返回索引,而不需要_块变量。我不想让它看起来像是我剽窃了您的答案。我已经添加了一个属性,我希望你可以。好的,我没有注意到所有的变化。为什么你要复制并粘贴我的答案?您没有添加任何重大更改Shey Nekto,我将使用的方法更改为indexOfObjectPassingTest:它直接返回索引,而不需要_块变量。我不想让它看起来像是我剽窃了您的答案。我已经添加了一个属性,我希望你可以。好的,我没有注意到所有的变化。TYI假设Narayana希望对通过测试的数组中的每个特定对象执行某些操作,但是如果没有,那么这就简单多了@emptyStack这是一个非常棒的回答干净优雅!但这取决于类返回唯一描述的前提(不一定是这样),我有一个搜索对象的项目数组。相等性取决于对象的地址,我必须将该谓词提供给NSFetchedResultsController。所以,我不能使用块。我正在尝试这样的操作:NSPredicate*taskComponentsPredicate=[NSPredicate predicateWithFormat:@“ANY%@==self”,[[self-taskRegisterObject]componentsCollection]allObjects]];我已经转储了这些值并手动检查,这些对象存在于数组中,但它不是由谓词返回的。不知何故,我觉得我试图得到的东西有点不对劲,我不知道是什么。救命啊!我假设Narayana想要对通过测试的数组中的每个特定对象做一些事情,但是如果没有,是的,这要简单得多@emptyStack这是一个非常棒的回答干净优雅!但这取决于类返回唯一描述的前提(不一定是这样),我有一个搜索对象的项目数组。相等性取决于对象的地址,我必须将该谓词提供给NSFetchedResultsController。所以,我不能使用块。我正在尝试这样的操作:NSPredicate*taskComponentsPredicate=[NSPredicate predicateWithFormat:@“ANY%@==self”,[[self-taskRegisterObject]componentsCollection]allObjects]];我已经转储了这些值并手动检查,这些对象存在于数组中,但它不是由谓词返回的。不知何故,我觉得我试图得到的东西有点不对劲,我不知道是什么。救命啊!