Maintaining an index in a NSArray


I have a “current index” pointing to an object into a NSArray (making this object the “current” one). I’m not satisfied with my present solution, using

    set theCurrentIndex to gArray's indexOfObject_(currentObject)


   doSomething (gNavigatorArray's objectAtIndex_(theCurrentIndex))

because I’m continuously parsing my array to find either the object, or its index. It works, but this is not very OOP. A better solution would be a gArray’s currentIndex_() or a gArray’s currentObject_(), and why not a set of methods similar to the existing lastObject, which could be firstObject, previousObject and nextObject. Is it possible in ApplescriptObjC?


I’m not sure what you’re getting at here. An array doesn’t really have a concept of a current object or current index. It’s not like a file pointer that points to a read location in a file. Current object or current index would only have meaning in the sense that “current” is the one you most recently accessed or added, and the way you are doing that now seems appropriate.

What you mean by “parsing” here? That sounds more like a filtering operation that might be better done using predicates, though that depends on what you are looking for.


Hello rdelmar, thank you for the answer.

For me, an array is an ordered collection of things, not a “cluster” in which I must search. An array should be used like an Applescript list, and we can tell, for example, “item n of myList”, or “item n+1 of myList”.

I access to any object of my list with the two procedure I mentioned, and of course I could be satisfied so, I only wanted to know if it could be possible to do something like (pseudocode):

class myIndexedArray : parent class (NSMutableArray) {
var currentIndex: integer;
method firstObject;
method nextObject;


I suppose it must be possible in Objective-C or other OOP language, but with ApplescriptObjC?


Normally it’s not necessary to subclass NS(Mutable)Array.
You are responsible anyway to maintain the variable currentIndex, thus you can define it in a custom class

Ok Stefan, in this case the gain seems insignificant beside a subclassing. I’ll stick to my procedure (after all it was just a cosmetics matter :))

Thank you, post closed for me.

Hello again Fiz,

I think you’re doing something wrong.

In fact what you’r saying is something like this

set theCurrentObject to "b"
set a to offset of theCurrentObject in "abcd"
set b to character a of "abcd" --result: "b"; same as the first line 

the result is the the same as the first line of code. So I can’t see why you need code like that. and is the same as

set b to "b"

Also what you’re saying about Objective-C and ASOC isn’t right. Objective-C doesn’t have arrays on it’s own (it uses C arrays), it also uses NSArrays, NSDictionaries or NSSets just like ASOC. maybe you can look into those NSArray alternatives or look into a NSEnumerator.

If this isn’t an option then describe your problem more detailed so we can help you further.

Hello again DJ,

These two methods (accessing to gArray’s indexOfObject_(currentObject) and gArray’s objectAtIndex_(theCurrentIndex) are not used in succession of course ”they are each other’s complement, and are used in different points of the program. In fact, what I was saying is that these methods do exactly what I need, I was just wondering if there were another (and maybe smarter) way to do the same thing.

I looked for NSSet and NSEnumerator, these are collections of unordered objects, not what I am looking for.

Imagine I have an ordered INT objects like {1,3,8,10,246}, these are, say, object IDs the used can browse in succession. He can delete some IDs, or create new ones. To browse them, I need an ordered array to apply a “first”, “previous” or “next” command. The program must also be able to propose the first free ID when the user create a new one, 2 in the example above.

The question I ask myself now is: wouldn’t be smarter to stick to an applescript list?


The question I ask myself now is: what are you trying to do when you say it is possible in a list?

I can tell you two things:
Everything that is possible with a list is also possible with a NSArray
Everything that is possible with a NSArray is not possible with a list

Yes, like sorting, which is essential to my app…:confused:

But even that, with a (urgh) bubble sort, of course…

Are you using a bubble sort instead of NSArray’s sort methods? If so, Why?

The main reason I’ve chosen to store my integers into a NS(Mutable)Array was it’s sorting facility, with its “compare:” selector. And because AS list are immutable, too. But I initialize my NSMutableArray with an AS list, because I tell the Finder to set this list to “name of files of folder xxx”…

wrong, AS list is one of the few objects that are mutable unlike text objects for example.