Pretty basic programming question, I know PHP have a function for it, but does the iPhone OS have one?
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 anybody know how to do the same thing in iOS?
containsObject
:
Returns a Boolean value that indicates whether a given object is present in the receiver.
- (BOOL)containsObject:(id)anObject
For example:
if ([arrayofNumbers containsObject:[NSNumber numberWithInt:516]])
NSLog(@"WIN");
or to check an indexPath:
if ([arrayofIndexPaths containsObject:indexPath])
NSLog(@"Yup, we have it");
I should clarify that an 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 explained in more detail in the developer documentation.
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's there...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With