检查NSMutableArray中是否存在某些内容

时间:2022-03-11 22:37:47

I have an NSMutableArray which can hold several objects. I would like to check if an object exists, and if so, alter it. I was wondering about checking it. I thought this would work:

我有一个NSMutableArray可以容纳几个对象。我想检查对象是否存在,如果存在,请更改它。我想知道检查它。我认为这会奏效:

if ([[[self.myLibrary objectAtIndex:1] subObject] objectAtIndex:1]) // do something

However, this will crash if there aren't any subObjects at index 1. So I guess the problem is that the above does not return nil if there isn't anything at this Index.

但是,如果索引1处没有任何子对象,这将会崩溃。所以我猜问题是如果此索引中没有任何内容,则上述内容不会返回nil。

Is there another easy way to check or will I have to count through the array etc.? I know there are other posts on * on this, but I haven't found a simple answer yet.

还有另一种简单的检查方法,还是我必须通过数组等计算?我知道*上还有其他帖子,但我还没有找到一个简单的答案。

Any explanations / suggestions welcome. Thanks!

欢迎任何解释/建议。谢谢!

4 个解决方案

#1


46  

No check simply using :

没有检查只是使用:

[myArray indexOfObject:myObject];

or

要么

[myArray containsObject:myObject];

These methods check every object using isEqual.

这些方法使用isEqual检查每个对象。


For example:

例如:

NSUInteger objIdx = [myArray indexOfObject: myObject];
if(objIdx != NSNotFound) {
    id myObj = [myArray objectAtIndex: objIdx];
    // Do some alter stuff here
}

#2


13  

If this is a pattern you use a lot, you could add a category to NSArray called something like safeObjectAtIndex:, which takes an index, does the bounds checking internally:

如果这是一个你经常使用的模式,你可以在NSArray中添加一个名为safeObjectAtIndex:的类,它接受一个索引,在内部进行边界检查:

-(id)safeObjectAtIndex:(NSUInteger)index {
  if (index >= [self count])
    return nil;
  return [self objectAtIndex:index];
}

#3


3  

Assuming the object you are using to search with and the actual object in the array are the exact same instance, and not two different objects that are equal according to an overridden isEqual: method, you can do this:

假设您用于搜索的对象和数组中的实际对象是完全相同的实例,而不是根据重写的isEqual:方法相同的两个不同对象,您可以这样做:

if ([array containsObject:objectToSearchFor]) {
    // modify objectToSearchFor
}

If the two objects are different instances which are equal according to isEqual:, you will have to use code like this:

如果两个对象是根据isEqual:相同的不同实例,则必须使用如下代码:

NSUInteger index = [array indexOfObject:objectToSearchFor];
if (index != NSNotFound) {
    id objectInArray = [array objectAtIndex:index];
    // modify objectInArray
}

#4


2  

NSArray (which is the NSMUtableArray superclass) has lots of methods for finding objects. Have a look at the documentation.

NSArray(NSMUtableArray超类)有很多查找对象的方法。看看文档。

You can either rely on the equals method (e.g. indexOfObject:) or provide a block (e.g indexOfObjectPassingTest:) which is pretty funky.

您可以依赖equals方法(例如indexOfObject :)或提供一个非常时髦的块(例如indexOfObjectPassingTest :)。

It's fairly common in Objective C to be using the Mutable version of a class but rely on methods in the non mutable superclass so it's always a good idea when checking the online documentation to look at the superclass.

在Objective C中使用Mutable版本的类是相当常见的,但是依赖于非可变超类中的方法,因此在查看在线文档以查看超类时总是一个好主意。

#1


46  

No check simply using :

没有检查只是使用:

[myArray indexOfObject:myObject];

or

要么

[myArray containsObject:myObject];

These methods check every object using isEqual.

这些方法使用isEqual检查每个对象。


For example:

例如:

NSUInteger objIdx = [myArray indexOfObject: myObject];
if(objIdx != NSNotFound) {
    id myObj = [myArray objectAtIndex: objIdx];
    // Do some alter stuff here
}

#2


13  

If this is a pattern you use a lot, you could add a category to NSArray called something like safeObjectAtIndex:, which takes an index, does the bounds checking internally:

如果这是一个你经常使用的模式,你可以在NSArray中添加一个名为safeObjectAtIndex:的类,它接受一个索引,在内部进行边界检查:

-(id)safeObjectAtIndex:(NSUInteger)index {
  if (index >= [self count])
    return nil;
  return [self objectAtIndex:index];
}

#3


3  

Assuming the object you are using to search with and the actual object in the array are the exact same instance, and not two different objects that are equal according to an overridden isEqual: method, you can do this:

假设您用于搜索的对象和数组中的实际对象是完全相同的实例,而不是根据重写的isEqual:方法相同的两个不同对象,您可以这样做:

if ([array containsObject:objectToSearchFor]) {
    // modify objectToSearchFor
}

If the two objects are different instances which are equal according to isEqual:, you will have to use code like this:

如果两个对象是根据isEqual:相同的不同实例,则必须使用如下代码:

NSUInteger index = [array indexOfObject:objectToSearchFor];
if (index != NSNotFound) {
    id objectInArray = [array objectAtIndex:index];
    // modify objectInArray
}

#4


2  

NSArray (which is the NSMUtableArray superclass) has lots of methods for finding objects. Have a look at the documentation.

NSArray(NSMUtableArray超类)有很多查找对象的方法。看看文档。

You can either rely on the equals method (e.g. indexOfObject:) or provide a block (e.g indexOfObjectPassingTest:) which is pretty funky.

您可以依赖equals方法(例如indexOfObject :)或提供一个非常时髦的块(例如indexOfObjectPassingTest :)。

It's fairly common in Objective C to be using the Mutable version of a class but rely on methods in the non mutable superclass so it's always a good idea when checking the online documentation to look at the superclass.

在Objective C中使用Mutable版本的类是相当常见的,但是依赖于非可变超类中的方法,因此在查看在线文档以查看超类时总是一个好主意。