Hi all,
I'm trying to search for a given integer inside an NSMutableArray (that contains random integers).
Can someone help?
Thanks
A fast, easy to use, free, and community supported 2D game engine
Hi all,
I'm trying to search for a given integer inside an NSMutableArray (that contains random integers).
Can someone help?
Thanks
Hi,
You can add object into NSMutableArray like [array addObject:[NSNumber numberWithInt:<#(int)value#>]];
put random function on (int)value.
Hope it helps.
@iphonemobdev:i think he is asking how to search for a value inside an array, not how to insert one...
I don't know if there is a direct way, but i think you can do that like you would with a normal array.
in c:
int positioninarray =-1;
for(i=0;i<sizeofthearray;i++)
{
if(array[i] == searchedvalue)
{
positioninarray = i;
}
}
if(positioninarray == -1)
//Didn't find the position of the searched value
else
{
//do whatever you want with that...
}Thanks, I'll try that.
@dnsolo, also:
indexOfObject:
Returns the lowest index whose corresponding array value is equal to a given object.
- (NSUInteger)indexOfObject:(id)anObject
Return Value
The lowest index whose corresponding array value is equal to anObject. If none of the objects in the receiver is equal to anObject, returns NSNotFound.
Discussion
Objects are considered equal if isEqual: returns YES.
Thanks Codemattic, that's exactly what I need.
Does that work on int's though?
If not, add break; after positioninarray = i; in pablo's code. You don't need to keep searching once you find it.
try this:
+(int)findArrayValue:(NSArray*)array number:(int)num{
for(i=0;i<[array count];i++){
if([[array objectAtIndex:i]intValue] == num){
return i
}
}
return -1;
}
+(int)findArrayValue:(NSArray*)array string:(NSString*)str{
for(i=0;i<[array count];i++){
if([[array objectAtIndex:i]stringValue] == str){
return i
}
}
return -1;
}
You must log in to post.