Easy way to search array of dictionary [duplicate] - ios

This question already has answers here:
How to implement searching of a string in an array of dictionaries?
(2 answers)
Searching NSArray of NSDictionary objects
(3 answers)
Closed 8 years ago.
I have a NSArray looking like:
[{#"firstName":#"abc", #"lastName":#"ABC"},
...
{#"firstName":#"xyz", #"lastName":#"XYZ"}]
I want to get the dictionary element in which lastName=XYZ, that is, the array element:
{#"firstName":#"xyz", #"lastName":#"XYZ"}
is there an easy way to get it without a lot of loops? Thanks.

NSArray *people = ...;
NSUInteger chosenIndex = [people indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
NSDictionary *person = obj;
return [person[#"lastName"] isEqualToString:#"XYZ"];
}];
if (chosenIndex != NSNotFound) {
NSDictionary *chosenPerson = people[chosenIndex];
NSLog(#"I chose %#", chosenPerson);
}

Related

Get index of last repeated string in NSMutableArray

I have a NSMutableArray with multiple repeated strings, I am trying to get last string index.
NSMutableArray *arrWithRepeatedStrings=[[NSMutableArray alloc]initWithObjects:#"iOS",#"apple",#"iOS",#"apple",#"apple",#"iOS", nil];
here iOS and apple are the repeated strings in my array. I think it's possible and am on the way to it. Can anyone help me.
You can get the index of last occurrence of string by enumerating in the reverse direction. Use the code below. You can change the matching string to #"iOS" if you want it's index and not #"apple".
NSMutableArray *arrWithRepeatedStrings=[[NSMutableArray alloc]initWithObjects:#"iOS",#"apple",#"iOS",#"apple",#"apple",#"iOS", nil];
NSInteger index = [arrWithRepeatedStrings indexOfObjectWithOptions:NSEnumerationReverse passingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
if([obj isEqualToString: #"apple"])
return YES;
else
return NO;
}];
I understand the question that you want the index of the last object that is more than once in the array. This is quite different from what #Gandalf's solution does, so here's my take:
NSInteger index = [array indexOfObjectWithOptions:NSEnumerationReverse
passingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
BOOL match = [arrWithRepeatedStrings indexOfObject:obj] != idx;
if (match) *stop = YES;
return match;
}];

iOS: sorting an NSArray of core data objects [duplicate]

This question already has answers here:
How do I sort an NSMutableArray with custom objects in it?
(27 answers)
Closed 8 years ago.
I have a NSArray with core data entities called:
"Struct"
thy have an attribute called "value" that can be a double
Struct *struct = [array objectAtIndex:0];
double val = [struct value];
I have to sorting this array by this value but I don't understand what's the way to do it, because I have not a key to access to the object.
Do you have any suggests?
NSMutableArray *sortedArray;
sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSNumber *s1 = #([(Struct *)a value]);
NSNumber *s2 = #([(Struct *)b value]);
return [s1 compare:s2];
}];

sort an NSMutableArray that contains NSString objects [duplicate]

This question already has answers here:
How to sort a NSArray alphabetically?
(7 answers)
Closed 9 years ago.
I am new to objective-c, ios.
I'm trying to sort in alfabetic order a NSMutableArray called filteredList that contains objects of type NSString.
so if my mutable array contains : [Mary, Bill, John] I would like to have [Bill, Mary, John]
I did the following:
[filteredList sortUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
but I do not see any change. I did read and tried other solutions like compare: instead of localizedCaseInsensitiveCompare but still nothing.
Updated
NSArray *sortedArray =[unSortedArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];//unSortedArray is NSMutableArray
unSortedArray = [[NSMutableArray alloc]initWithArray:sortedArray];
I would do something like:
NSArray *sortedArray = [filteredList sortedArrayUsingComparator:^(NSString *str1, NSString *str2) {
return (NSComparisonResult)[str1 compare:str2];
}];
filteredList = [sortedArray mutableCopy];
Please refer the below code:-
filteredList = (NSMutableArray*)[filteredList sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];

NSArray, how get items based on a property's value [duplicate]

This question already has answers here:
Filter NSArray of custom objects
(3 answers)
Closed 9 years ago.
I have NSMutableArray which contains a list of people , now I need to get a list of all people where gender = male, how can I do that? should I get into NSPredicates to do that?
Copy this NSArray category to to somewhere in your code
#implementation NSArray (My)
-(NSArray*)arrayWithPredicate:(BOOL(^)(id obj))predicate {
NSMutableArray* objs = [NSMutableArray array];
[self enumerateObjectsUsingBlock:^(id o, NSUInteger idx, BOOL *stop) {
if (predicate(o)) {
[objs addObject:o];
}
}];
return objs;
}
#end
Then where you need to get the male ones:
NSArray* males = [people arrayWithPredicate:^BOOL(id obj) {
// Gender check
}];
The advantage over NSPredicate is that you don't have to use a literal string to specify the criteria (quite a mess if the criteria is complex).
yes you can use like this,
NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.gender == %#", #"male"]];
NSLog(#"%#",filtered);

What is the execution flow of blocks? [duplicate]

This question already has answers here:
can anyone tell flow of execution of blocks in objective c? [closed]
(3 answers)
Closed 9 years ago.
I am working with Objective-C blocks, but I'm having troubles understanding the below code execution.
Here is the code :
NSArray *array = #[#"A", #"B", #"C", #"A", #"B", #"Z", #"G", #"are", #"Q"];
NSSet *filterSet = [NSSet setWithObjects: #"A", #"Z", #"Q", nil];
BOOL (^test)(id obj, NSUInteger idx, BOOL *stop);
test = ^(id obj, NSUInteger idx, BOOL *stop) {
if (idx < 5) {
if ([filterSet containsObject: obj]) {
return YES;
}
}
return NO;
};
NSIndexSet *indexes = [array indexesOfObjectsPassingTest:test];
NSLog(#"indexes: %#", indexes);
Output:
indexes: <NSIndexSet: 0x10236f0>[number of indexes: 2 (in 2 ranges), indexes: (0 3)]
In this method, [array indexesOfObjectsPassingTest:test];, the test block is the parameter I passed.
But in the above block, test = ^(id obj, NSUInteger idx, BOOL *stop) what are the values of the parameters obj, idx and stop it can take? Where are they from?
You have 9 items in your array. So the test block is executed 9 times.
Each time, obj will be the object from the array. And idx will be the index.
First time: obj=#"A" idx=0
Second time: obj=#"B" idx=1
etc.
stop is a value you can write to, if you wanted to exit early. So if on the 5th time through the block, you didn't want to do it anymore. you could do *stop=YES;

Resources