Objective-C: block enumeration for array in REVERSE order? [duplicate] - objective-c

This question already has answers here:
How can I reverse a NSArray in Objective-C?
(18 answers)
Closed 9 years ago.
There is the enumerateWithBlock method of NSArray, but I need to enumerate in a reverse order, is there a block for doing that, or do I have to use for loop instead, not even the fast loop, but the traditional for (int i = array.count - 1 ; i >= 0 ; i--) one?
Thanks!

You can use enumerateObjectsWithOptions:usingBlock: passing NSEnumerationReverse as the options parameter.

You can use reverseObjectEnumerator
for (id someObject in [myArray reverseObjectEnumerator])
{
// print some info
NSLog([someObject description]);
}
Hope this helps!

Related

How can I keep track of array index in for loop? [duplicate]

This question already has answers here:
how can I get the array index within an "for (id item in items)" objective-c loop?
(2 answers)
Closed 8 years ago.
If I write:
for (NSString* word in self.words)
{
}
And I want to keep track of my position in the array, how can I do that? Of course, I know I can just create an int and increment it as I loop, a.k.a. the old-fashioned way. I guess I'm just looking to see if there's an opportunity here to learn something. Like in python we have the handy enumerate() function for this sort of thing which gives you indices paired with objects.
You could use [words indexOfObject:word]; but caution: if you have equal object in an array, it will return the first object's index. also this shows, that it is awfully inefficient — if will iterate over the array for each call.
better:
[words enumerateObjectsUsingBlocks:^(NSString *word,
NSUInteger idx,
BOOL *stop)
{
//
}];
as it gives you enumeration AND the index at the same time.
documentation

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

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];

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.

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).