How to check if * a number * in an array

Pretty basic programming question, I know that PHP has a function for it, but does it have an iPhone OS?

I want to check if the current indexPath is a value in an array.

PHP example:

<?php $indexPath = 3; $array = array("0", "1", "2", "3", "4"); if (in_array($indexPath, $array)) { // Do something } ?> 

Does anyone know how to do the same in iOS?

+4
source share
2 answers

containsObject :
Gets a boolean value indicating whether this object is present in the receiver.

- (BOOL)containsObject:(id)anObject

For instance:

 if ([arrayofNumbers containsObject:[NSNumber numberWithInt:516]]) NSLog(@"WIN"); 

or to check indexPath:

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

I must clarify that NSIndexPath is not a number, but a series of numbers that "represents the path to a specific node in a tree of nested array collections", as described in more detail in the developer documentation .

+20
source

Do you want containsObject or indexOfObject :

 unsigned int myIndex = [myArray indexOfObject: [NSNumber numberWithInt: 3]]; if(myIndex != NSNotFound) { // Do something with myIndex } 

Or:

 if([myArray containsObject: [NSNumber numberWithInt: 3]]) { // Just need to know if it there... } 
+9
source

Source: https://habr.com/ru/post/1307472/


All Articles