Is there an easy way to iterate over an NSArray backwards? - objective-c

I've got an NSArray and have to iterate over it in a special case backwards, so that I first look at the last element. It's for performance reasons: If the last one just makes no sense, all previous ones can be ignored. So I'd like to break the loop. But that won't work if I iterate forward from 0 to n. I need to go from n to 0. Maybe there is a method or function I don't know about, so I wouldn't have to re-invent the wheel here.

To add on the other answers, you can use -[NSArray reverseObjectEnumerator] in combination with the fast enumeration feature in Objective-C 2.0 (available in Leopard, iPhone):
for (id someObject in [myArray reverseObjectEnumerator])
{
// print some info
NSLog([someObject description]);
}
Source with some more info: http://cocoawithlove.com/2008/05/fast-enumeration-clarifications.html

Since this is for performace, you have a number of options and would be well advised to try them all to see which works best.
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:…]
-[NSArray reverseObjectEnumerator]
Create a reverse copy of the array and then iterate through that normally
Use a standard C for loop and start and work backwards through the array.
More extreme methods (if performance is super-critical)
Read up on how Cocoa implements fast object enumeration and create your own equivalent in reverse.
Use a C or C++ array.
There may be others. In which case, anyone feel free to add it.

From here:
NSEnumerator* myIterator = [myArray reverseObjectEnumerator];
id anObject;
while( anObject = [myIterator nextObject])
{
/* do something useful with anObject */
}

[NsArray reverseObjectEnumerator]

for (int i = ((int)[array count] - 1); i > -1; i--) {
NSLog(#"element: %#",array[i]);
}

Related

How do Objective-C comparison blocks know their arguments for the sortUsingComparator method?

I'm in the process of learning Objective-C, and am trying to get my head wrapped around the idea of using blocks.
In an example I'm seeing, there is a method meant to sort an NSArray named book in alphabetical order by name:
-(void) sort
{
[book sortUsingComparator:
^(id obj1, id obj2) {
return [[obj1 name] compare: [obj2 name]];
} ];
}
How does the method pass the objects to the obj1 and obj2 parameters? Does the sortUsingComparator somehow automatically pass 2 objects to its comparator?
sortUsingComparator: iterates over the array and calls the block with various pairs of objects to determine how to sort them. There's no magic here — it's a normal sorting function, but it uses your block instead of > to compare the values.
Well, depending on the implementation of the sort, every time the sort algorithm needs to compare 2 elements this will get called. You don't really need to know what will get passed to it, just define an order relationship for the types that get passed. It'll ask what he needs to know.
Some extra information about blocks that answers your question, a block can be assigned to a variable and called like a function. The sortUsingComparator: method signature is:
- (void)sortUsingComparator:(NSComparator)cmptr
cmptr is the variable that holds your block. The sortUsingComparator: method will run every element in the array through its sort algorithm and, as Fernando pointed out, every time the sort algorithm needs to compare 2 elements it's called like this:
NSComparisonResult result = cmptr(obj1, obj2);
And your block code is executed. So the answer to your question:
Does the sortUsingComparator somehow automatically pass 2 objects to its comparator?
is yes :)
The sort method does something like this (I'm not claiming the sort: method does this exactly, but the idea remains):
for (i = 0; i < [book count]; i++) {
id currentObject = [book objectAtIndex:i]; // obj1
for (j = 0; j < i; j++) {
id sortedObject = [book objectAtIndex:j]; // obj2
NSComparisonResult result = sortBlock(currentObject, sortedObject);
// Insert currentObject where appropriate; perform the rest of the sort
}
}
In this case, I'm assuming that the block's parameter name is ^sortBlock (the caret denotes a block). You invoke blocks just like regular functions, with parentheses and arguments.
So to answer your question…it's automatic in the sense that you don't have to think about it, but there's no magic going on.
Hope this helps!

Objective C Keyword 'in'

Im having some problems understanding some code in a programme I have inherited.
CGPoint tapLocation = [gesture locationInView:self.view];
for (UIView *view in self.view.subviews){
if (!CGRectContainsPoint(view.frame, tapLocation)){
//do something
}
}
The problem is that I have no idea what the keyword 'in' is doing. I've searched around and can only find some obscure reference to it and a post here.
The post says that:
in: argument is an input argument only and won’t be referenced later
I don't really see how this applies to the code above. Any help would be greatly appreciated.
I think apple calls this fast enumeration.
In other languages a similar functionality is provided by a "for each"-loop.
The "in" you're seeing there is part of Fast Enumeration.
Here is some documentation for it.
EDIT: And Derek pointed out another bit of documentation in his comment below.
It's a succinct way to iterate through a collection. Where it says:
for (object in collection)
it means "this code happens once for each object in the collection".
The in in the link you've specified in your question is entirely different from the in in the for loop. The farmer comes under #encoding and the later comes in the context of for loops. This kind of for loop is called for-each loop in general, and in Objective-C it is called fast enumeration.
It creates an enumerator automatically for you such that you can iterator over the collection. So the collection has to conform to NSFastEnumeration.
in is used to iterete through an array.
For instance:
NSArray *values = [NSArray arrayWithObjects:#"val1", #"val2", #"val3", nil];
for (NSString *val in values) {
NSLog(#"Value = '%#'", val);
}
Basically, this is an extension of the for loop built for objective-c.
Think of this code like this:
for (NSArray *arr = UIViewGetSubviews(UIViewControllerGetView(self)), i = 0; i < arr.count; i++) {
}
Note that the actual implementation uses a NSEnumerator, not a for with integer variable loop.

in statement in for loop

i have a for loop state ment as under:
for(NSString* name in nameArray)
nameArray is NSArray.
In the above statement, what does it mean for: NSString* name in nameArray
Iterate through all NSString* in nameArray.
Can be written less cleanly:
for (int i=0;i<[nameArray count];++i) {
NSString *name = [nameArray objectAtIndex:i];
// Do stuff
}
Keep in mind: Don't iterate a mutable array and mutate it (and make sure no other thread does). In such a case you need to call count every iteration like displayed above.
This is fast enumeration syntax introduced in Objective-C 2.0. Check this tutorial for the details. Also you can Google "objective c fast enumeration" for many other resources available online.
It means that the code inside the parenthesis will be executed for every object in the nameArray, which you will access through the NSString *name variable.

Objective-C Fast Enumeration Bubble Sort

I'm trying to integrate some GCD into my code, and have found that a severe bottleneck is a bubble comparison I am performing between objects in a large array. Here is the original code:
NSUInteger count = [arrayToDoWorkOn count];
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
[[arrayToDoWorkOn objectAtIndex:i] compare:[arrayToDoWorkOn objectAtIndex:j]];
}
}
Get my drift? So a lot of other fast enumeration tasks can be easily GCD'd by converting
for (id obj in array)
{
[obj aMessage:stuff];
}
to:
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
[obj aMessage:stuff];
}];
Is there a way to convert my look-ahead-sorta-bubble-sorta-algorithm-thing to something that I can feed to a GCD block implementation?
I wouldn't recommend implementing your own sort if NSArray already has a built in method for it that will most likely sort faster than anything you can come up with. You can just use this:
NSArray *sortedArray = [arrayToDoWorkOn sortedArrayWithComparator:^(id firstObject, id secondObject) {
/* comparison code (e.g. return [[firstObject title] compareTo:[secondObject title]], or something) */
}];
Now, if you need to use the objects during the sort, you're in for a pickle, but I'd recommend looking into sorts more efficient than a bubble sort (quick sort is a pretty good one).
Besides this, I think you're a bit confused about GCD. Writing and using a block does not inherently execute it with GCD; that has to be done manually (strictly speaking, a block is simply a collection of lines of code and does not inherently have anything to do with GCD; GCD simply uses blocks for execution). NSArray's enumerateObjectsUsingBlock: method most likely does not use GCD to enumerate the array (at least the reference gives no insight on this, so please prove me wrong), and if it does, it's not because you're supplying it with a block, but rather because that's how Apple chose to implement it. Most methods taking blocks do not use GCD to execute them.
I recommend you read the Grand Central Dispatch (GCD) Reference as well as Cocoa Samurai's A Guide to Blocks and GCD to get greater insight into the specifics of the topic.

comparing arrays in objective-c

Ok a pretty simple question.. in c++ it seems to work but in objective-c i seem to struggle with it :S ..
If you want to compare two arrays it should be something like this right
for ( int i = 0; i < [appdelegate.nicearray count]; i++ )
{
if ( appdelegate.nicearray[i] == appdelegate.exercarray[i] )
{
NSLog(#"the same elements in this selection");
}
}
what's the problem exactly ?
These are Cocoa array objects (instances of NSArray), not C arrays or C++ vectors, and remember that Objective-C does not have operator overloading. The only things you can do with an object are pass it around, store it in variables, and send messages to it.
So the array-subscript operator is wrong with Objective-C objects. I don't think it's even linguistically valid to dereference a pointer to an Objective-C object, so this code should be giving you a compiler error. I may be misremembering, though. If it does make it to runtime, that code will crash sooner or later, since you're accessing memory beyond the ends of the array objects.
(EDIT from the year 2013: Objective-C now supports subscripting of objects. This ultimately translates into the appropriate objectAtIndex: or replaceObjectAtIndex:withObject: message. So, the code in the question would actually work now, although it's still not the proper way to simply walk an array, much less to compare two arrays.)
The proper way to retrieve an object from an NSArray object by its index is not to use the array-subscript operator, but to send the array object the objectAtIndex: message:
[myArray objectAtIndex:i]
The proper way to iterate on the elements of an array object, assuming you don't really need the index for something else (such as replacing objects in a mutable array), is to loop on it directly (this is called “fast enumeration”):
for (MyObject *myObject in myArray) {
…
}
NSArray also responds to objectEnumerator and reverseObjectEnumerator, which return a similarly-iterable object. Of the two, reverseObjectEnumerator is the more useful in new code, since you can just iterate on the array directly to iterate forward. Both of them were most useful before fast enumeration existed; that code looked like this:
NSEnumerator *myArrayEnum = [myArray objectEnumerator];
MyObject *myObject;
while ((myObject = [myArrayEnum nextObject])) {
…
}
(Yes, that's an assignment in the condition. Deliberately, hence the extra (). We coded boldly back then, didn't we?)
For what you're doing, though, you more likely want to send one of the arrays an isEqualToArray: message, as Williham Totland suggested:
BOOL theyAreEqual = [myFirstArray isEqualToArray:mySecondArray];
This will make sure both arrays have the same length, then walk them both in lock-step, sending isEqual: to each pair of objects. It'll return YES if every isEqual: message returned YES; NO otherwise. The arrays may contain different objects, but as long as each pair is equal, the arrays themselves are equal.
That assumes you want object equality. Two separate objects are equal if one of them responds with YES when you send it an isEqual: message and pass the other object. If you meant to compare the identities of the objects, then you do need to do the lock-step loop yourself and use ==:
BOOL arraysContainTheSameObjects = YES;
NSEnumerator *otherEnum = [otherArray objectEnumerator];
for (MyObject *myObject in myArray) {
if (myObject != [otherEnum nextObject]) {
//We have found a pair of two different objects.
arraysContainTheSameObjects = NO;
break;
}
}
But that's unlikely. Most of the time, I have wanted to test the objects' equality, not identities, so isEqualToArray: is what I wanted.
You want the isEqualToArray: method. As in:
if ([arrayOne isEqualToArray:arrayTwo]) {
// Do something
}
This will recursively compare the two arrays, while having the advantage of not being needlessly circuitous and not requiring a loop.
Try telling us the result you're getting when you run this code. The approach is correct, but try this one:
for (int i =0; i< appdelegate.nicearray.count; i++)
{
if ([[appdelegate objectAtIndex:i] isEqual: [appdelegate.exercarray objectAtIndex:i]])
{
NSLog(#"the same");
}
}
Here's a little one I put together based on the top ranked example. This merely checks that the arrays contains the same values, irrespective of order and if there are any duplicates. I mainly use this to compare keys of two dictionaries (which often return their allKeys arrays in various sort orders) to see if they contain the same objects. Thanks Peter Hosley for providing the example I adapted this from.
#pragma mark - Arrays
// Check to see if arrays contain the same elements, not necessarily in the same order
// This is different from [array isEqualToArray:responseKeys] which demands the same order in both arrays
// ## Does not compensate for duplicate entries in an array
+ (BOOL)doArraysContainTheSameObjects:(NSArray *)firstArray withArray:(NSArray *)secondArray {
BOOL arraysContainTheSameObjects = YES;
for (id myObject in firstArray) {
if (![secondArray containsObject:myObject]) {
// We have found an object that is not in the other array.
arraysContainTheSameObjects = NO;
break;
}
}
return arraysContainTheSameObjects;
}
I do the following when comparing arrays:
Check to see if any of the arrays are nil when the other is not
Check to see if the lengths are the same
Iterate (using a for loop like you have) over each element checking the matching element in the other array.
To compare elements you need to define what you want to regard as being "equal". Are they equal only if the pointers in the array are equal or can they be equal if the content is equal too.
For the pointer case, you can use ==.
For the deep comparison you might need to use CompareTo or something similar.