Objective-c primitive arrays

I want to have a mutable array with primitives in obj-c (selectors). What's the recommended way to do this? NSArray

and those can only contain objects.

+2


source to share


4 answers


To wrap a selector or any other primitive type you need to use NSValue

. In Cocoa, SEL is some kind of pointer, so you can use [NSValue valueWithPointer:whatever]

to create it and [value pointerValue]

to get it. Or more generally, you can use [NSValue valueWithBytes:&whatever objCType:@encode(SEL)]

; it works for any type.



+7


source


If you want to store an array of objects SEL

, the easiest way is to convert SEL

to NSString

with a function NSStringFromSelector()

, store them in NSMutableArray

, and then convert them back to SEL

when you pull them out with a function NSSelectorFromString()

.



+6


source


Apart from managing the C-style array yourself (this is definitely not the best option, IMO), the only option is to use NSArray

/ NSMutableArray

and store the numbers with NSNumber

. It's a little more annoying to get a value than with an actual numeric type, but it frees you from managing the array's memory yourself.

0


source


Since primitive types are just numbers (be they integers or floating point) or pointers, what's the problem with using the classes used to port them for your purposes? NSMutableArray

NSNumbers

, eg?

0


source







All Articles