简体   繁体   English

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

[英]Checking if an array contains a certain object (iOS)

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);

    }
}

containsObject is calling isEqual on each of the object in the arrays. containsObject在数组中的每个对象上调用isEqual。 What type of object are you checking for? 你正在检查什么类型的对象? If it's a custom object, override and implement the method 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,它正在比较对象的引用,而不是它的实际值。

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

containsObject: containsObject:

First you need to check which type data or object you are adding in this myMutableArray . 首先,您需要检查要在此myMutableArray中添加的类型数据或对象。 According to your method you are checking in mutable array for string type that you have passed argument parameter . 根据您的方法,您正在检查已传递参数parameter字符串类型的可变数组。 It may be possible that you are containing int or float array. 您可能包含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的类型,那么您需要转换。

You should implement isEqual: in your custom class. 您应该在自定义类中实现isEqual: . By default two objects are only identical if they share the same reference. 默认情况下,如果两个对象共享相同的引用,则它们只相同。

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

You probably forgot to initialize your NSMutableArray . 您可能忘记初始化NSMutableArray If not initialized, you are sending addObject messages to a nil object, which has no effect, and the array never contains what you previously added... 如果未初始化, addObject消息发送到nil对象,该对象无效,并且该数组永远不会包含您之前添加的内容...

Of course, if the array is nil , then the contains check will always return false. 当然,如果数组为nil ,则contains check将始终返回false。 According to the Objective-C docs: 根据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 *),float,double,long double或long long的整数标量,则发送给nil的消息返回0 。

And 0 is false 0是假的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM