views:

381

answers:

4

I wanna have a mutable array with primitives in obj-c (selectors). What's the recommended way to do this? NSArray and those can only hold objects.

A: 

Other than managing a C-style array yourself (which is definitely not the best option, IMO), your only option is to use NSArray/NSMutableArray, and store the numbers using NSNumber. It's slightly more annoying to get the value out than with the actual numeric type, but it does free you from managing the array's memory yourself.

Andy
Actually, what I want to store is a SEL. What type is that, really?
quano
A: 

Since the primitive types are generally just numbers (be they integer or floating-point) or pointers, what's the problem with using the classes used to wrap those up for your purposes? An NSMutableArray of NSNumbers, for example?

Carl Norum
+5  A: 

If you want to store an array of SEL objects, the easiest thing would be to convert the SELs to NSStrings using the NSStringFromSelector() function, store them in an NSMutableArray, and then convert them back to SELs when you pull them out using NSSelectorFromString() function.

Dave DeLong
Hm, I suppose that would work. Thanks.
quano
+4  A: 

You should use an NSValue to wrap the selector or any other primitive type you need. In Cocoa SEL is some kind of pointer, so you can use [NSValue valueWithPointer:whatever] to construct it and [value pointerValue] to get it out. Or, in general you can use [NSValue valueWithBytes:&whatever objCType:@encode(SEL)]; this works for any type.

newacct
Thanks, I didn't know about NSValue.
quano