How to find a particular object from an NSMutableArray iPhone app?
17,997
Solution 1
This will do the trick most economically [tested]:
[myArray filteredArrayUsingPredicate:[NSPredicate
predicateWithFormat:@"self == %@", @"New"]];
Solution 2
Gopinath if you have added constant string like @"New"
then
int index = [array indexOfObject:@"New"];
will do the trick as
NSString *str1 = @"New";
NSString *str2 = @"New";
Then str1
and str2
points to same object.
And if you know the string that you are searching then is there any need of finding it?
Mundi's answer is also good.
Solution 3
Why not just do a fast enumeration of the objects?
NSMutableArray *matchingObjects = [NSMutableArray array];
NSArray *objects = [NSArray arrayWithObjects:@"New", @"Old", @"Future", nil];
NSInteger count = 0;
for (NSString *string in objects) {
if ([string isEqualToString:@"New"]) {
[matchingObjects addObject:string];
}
count++;
}
This way matchingObjects
contains all of the objects that matched the test, and you can use count
to get the index of all the matches.
Author by
Gopinath
Updated on June 05, 2022Comments
-
Gopinath almost 2 years
I have store many values in
NSMutableArray
like@"New"
,@"Old"
,@"Future"
. TheNSMutablArray
having 100+ objects. I need to find the@"New"
objects from the array. Can anyone please help me to solve this issue? Thanks in advance. -
Gajendra K Chauhan over 8 yearsgood ans, u can filter object also by [string containsString: ] method