views:

462

answers:

2

What is the equivalent for Vector's of Java in Objective-C?

+2  A: 

Try using NSMutableArray.

Jim
what can i write equivalent of this in Objective-C? ***In Java: Vector getMethod();*** ***In Objective-C: ???// what can be written??? Is this correct **-(Vector)getMethod;*****
suse
@suse - It's a bit unclear from your statement what you mean by this, but given @Jim's answer, how does `- (NSMutableArray *)getMethod;` sound?
asveikau
hey ya.Even i tried same thing :). it solved .. thanks..
suse
A: 

The closest thing you will find is NSMutableArray, execpt that contrary to java Vector, it is not thread safe. If you do not need thread safety, NSMutableArray is nice. I suspect that if you use java vector instead of List, it is that you need thread safaty, then in objective-C, you should probably use NSArray. THe API is slightly different, since the add operation of an element to a NSArray returns a new array instance, but it is thread safe, see http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html

NSArray: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/cl/NSArray NSMutableArray: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableArray

tonio
Well, calling Vector "thread safe" is exagerating: it is synchronized, but in some cases this will not be sufficient for thread safety, e.g. when iterating over the Vector
tonio
If you're doing operations from multiple threads, why not just protect the thing with a mutex or similar? Are these tangential discussions of synchronization really the kind of detail you need to bring up to answer this question?
asveikau
Well, I raised the synchronization thing here because if you do not pay attention to those things, then the "equivalent" thing you use has a very different behavior. Yes, protecting the whole thing with a mutex will work, but it is error prone, possibly inefficient (an efficient version requires some thought), and a good solution is already available (and it is what Vector provides in the java world: if you so not need this, use java List).
tonio
Yes, an efficient version requires some thought, but since when does programming not require some thought? I am certain scalable concurrent use of Java's `Vector` requires some thought too. There is nothing magic about Java's `synchronized` construct, all it is doing is acquiring a per-object lock at every block marked as such, which is essentially what I meant by "protect the thing with a mutex". As such I think your answer views things through a very narrow lense, orthogonal to the actual question.
asveikau