Searching an object (by an attribute) in a NSMutableArray [duplicate] - objective-c

This question already has answers here:
Finding a particular instance in an array by a property
(5 answers)
Closed 8 years ago.
I need to get a better way to find an object in a NSMutableArray.
At the moment i do it this way:
for(classOfTheObject *thisItem in arrayOfObjects){
if(thisItem.foreign_key == serchThisObject.foreign_key){
// found it
}
}
but this is a very bad way i think. Is it possible to get the object without a for loop?

In an array it will always require some type of loop/enumaration to actually find it. If foreign_key is the only search/identification criteria that you use then you may consider using an NSDictionary with the value of foreign_key as key.

If I were you,I would use NSPredicate :
NSPredicate *applePred = [NSPredicate predicateWithFormat:
#"employer.name == 'Apple'"];
NSArray *appleEmployees = [people filteredArrayUsingPredicate:applePred];

Related

How do I query an NSArray when I know the attribute name and value? [duplicate]

This question already has answers here:
How to quickly search an array of objects in Objective-C
(4 answers)
NSPredicate for property of object in NSArray of NSArray
(1 answer)
Iterate through custom objects in a UIView and find matching properties
(2 answers)
Closed 5 years ago.
I am having a hard time getting the correct query type in my NSArray search, I tried the following which failed because its not searching for the correct value
BOOL isCashTender = [_tenderRows containsObject: #"Cash"];
I want to search inside the NSArray where there is a type attribute that contains the value Cash
I don't care how many rows/objects are inside the array I simply need to know if there is at least one type attribute containing the value Cash in there

Objective-C - sort array of strings based on string slice [duplicate]

This question already has answers here:
How to sort a NSArray alphabetically?
(7 answers)
How to do a natural sort on an NSArray?
(5 answers)
Closed 9 years ago.
NSMutableArray looks like ("xxx_20", "xxx_18", "xxx_16", "xxx_19", "xxx_17")
I want to sort it into ("xxx_16", "xxx_17", "xxx_18", "xxx_19", "xxx_20")
I've tried to use selectors, but I can't even get proper syntax. Does anyone know how to do this properly?
Try this,
NSMutableArray *myarray = [ NSMutableArray arrayWithObjects:#"xxx_20", #"xxx_18", #"xxx_16", #"xxx_19", #"xxx_17", nil];
NSArray *sortedArray = [myarray sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSLog(#"%#",sortedArray);
It worked fine at my end.

Sort an array of NSDictionary based on dates [duplicate]

This question already has answers here:
How can I sort an NSArray containing NSDictionaries?
(3 answers)
Closed 10 years ago.
I'm looking for a way to sort an NSMutableArray of dictionaries based on dates contained in the dictionaries. Each one looks like this:
{
name: tom,
surname: smith,
date of birth (NSDate): 2013-02-21 15:25:27
}
I know of the methods earlierDate etc., but since they only compare two dates I'm just a bit stuck on how to iterate through the array so that I come out with a fully ordered array of dictionaries?
using sortUsingComparator this can be solved easily:
[yourArray sortUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
return [a[#"dateOfBirth"] compare:b[#"dateOfBirth"]];
}];

incrementing variable names in xcode? [duplicate]

This question already has answers here:
Objective C Equivalent of PHP's "Variable Variables" [duplicate]
(2 answers)
Closed 6 years ago.
Incrementing variable names
In xcode I want to create 100 images one with the name1,2,3,4 ect. And i can do it all except for the variable names. For these I was wandering is it possible to increment the variables programmatically or whether it must be done manually?
Instead of creating a bunch of variable names, you can use a dictionary object with the names as keys and the image object as the data.
If you really want to/have to use 100 different variables and avoid an array you could use Key Value Coding in combination with a for loop:
NSString *name = #"myVariable";
for (int i = 0; i< 100; i++)
{
// get aValue from somewhere...
NSString *key = [NSString stringWithFormat:#"%#%d", name, i];
[self setValue:aValue forKey:key];
}
This sort of thing is best achieved with an array.
Similar question here: Objective C Equivalent of PHP's "Variable Variables"
Create them in an NSMutableArray. Doesn't matter if all of them have the same object name, since they have different locations in the array. Just access them with [arrayName objectAtIndex:index]; Use a for.

How to slice NSMutableArray [duplicate]

This question already has an answer here:
How to slice an NSArray in Objective-C?
(1 answer)
Closed 8 years ago.
I have a NSMutableArray and need objects [0:5] only. Is there a simple way to slice? Can I drop all objects after index? Can I copy a sub-array to another NSMutableArray?
Use the instance method - (NSArray *)subarrayWithRange:(NSRange)range.
For example:
NSArray* slicedArray = [wholeArray subarrayWithRange:NSMakeRange(0, 5)];
I see James Bedford already answered how to extract a range of indexes. To delete the objects in a range from an NSMutableArray, you can use [wholeArray removeObjectsInRange:...]. To delete all objects after a particular index, you can create an appropriate range as NSMakeRange(index, wholeArray.count - index).