检查数组是否包含某个对象(iOS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14365991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:52:26  来源:igfitidea点击:

Checking if an array contains a certain object (iOS)

iosobjective-carraysnsmutablearray

提问by iosdevrocks

I need to check if a certain array contains a certain object, and if it does, delete that object. If it hasn't got that object, the funciton is suposed to add it into the array. The problem is that the object is always added because the checking statement always return false.

我需要检查某个数组是否包含某个对象,如果包含,则删除该对象。如果它没有得到那个对象,函数应该将它添加到数组中。问题是总是添加对象,因为检查语句总是返回false。

Here's my current function:

这是我目前的功能:

- (void) myFunction:(NSString *)parameter {

    if (![myMutableArray containsObject:parameter]) {

        [myMutableArray addObject:parameter];
        NSLog(@"%@ added", parameter);

    } else {

        [myMutableArray removeObject:parameter];
        NSLog(@"%@ deleted", parameter);

    }
}

回答by Rohan Agarwal

containsObject is calling isEqual on each of the object in the arrays. What type of object are you checking for? If it's a custom object, override and implement the method isEqual.

containsObject 正在对数组中的每个对象调用 isEqual。您要检查什么类型的对象?如果是自定义对象,则覆盖并实现方法 isEqual。

I'm guessing you're trying to check the value of the object, but containsObject is actually calling isEqual which is comparing the reference to the object, and not its actual value.

我猜您正在尝试检查对象的值,但 containsObject 实际上正在调用 isEqual ,它正在比较对对象的引用,而不是它的实际值。

回答by Darshit Shah

if (![arrList containsObject:arrObj]) {
    // do something
}

containsObject:

包含对象:

回答by Nirav

First you need to check which type data or object you are adding in this myMutableArray. According to your method you are checking in mutable array for string type that you have passed argument parameter. It may be possible that you are containing int or float array.

首先,您需要检查要在此 .xml 文件中添加的类型数据或对象myMutableArray。根据您的方法,您正在检查已传递参数的字符串类型的可变数组parameter。您可能包含 int 或 float 数组。

There may be issue of type casting in your array.If your is STRING type of data then you can use another method like this.

您的数组中可能存在类型转换问题。如果您是 STRING 类型的数据,那么您可以使用另一种类似的方法。

- (void) myFunction:(NSString *)parameter {

for (int i = 0 ; i < [myMutableArray count ] ; i++) {


    if (![[myMutableArray objectAtIndex:i] isEqualToString:parameter]) {
        [myMutableArray addObject:parameter];
        NSLog(@"%@ added", parameter);
    }
    else{
        [myMutableArray removeObject:parameter];
        NSLog(@"%@ deleted", parameter);
    }
}

}

}

Hope this will help you. If your object is not type of NSString then you need to convert.

希望这会帮助你。如果您的对象不是 NSString 类型,那么您需要进行转换。

回答by Attila H

You should implement isEqual:in your custom class. By default two objects are only identical if they share the same reference.

您应该isEqual:在自定义类中实现。默认情况下,两个对象仅在共享相同引用时才相同。

Also make sure to initialize your mutable array before using it.

还要确保在使用可变数组之前初始化它。

EDIT:

编辑

It seems that your array's variable name are most probably mistyped.

似乎您的数组的变量名称很可能输入错误。

  • myMutableArray
  • myMutbaleArray
  • myMutableArray
  • myMutbaleArray

回答by mprivat

You probably forgot to initialize your NSMutableArray. If not initialized, you are sending addObjectmessages to a nilobject, which has no effect, and the array never contains what you previously added...

您可能忘记初始化您的NSMutableArray. 如果未初始化,则您正在向addObject一个nil对象发送消息,该对象无效,并且该数组永远不会包含您之前添加的内容...

Of course, if the array is nil, then the containscheck will always return false. According to the Objective-C docs:

当然,如果数组是nil,则contains检查将始终返回 false。根据 Objective-C 文档:

If the method returns an object, any pointer type, any integer scalar of size less than or equal to sizeof(void*), a float, a double, a long double, or a long long, then a message sent to nil returns 0.

如果该方法返回对象、任何指针类型、大小小于或等于 sizeof(void*) 的任何整数标量、浮点数、双精度数、长双精度数或长长整数,则发送到 nil 的消息返回 0 .

And 0 is false

0 是假的