Equivalent of C++ STL container "pair<T1, T2>" in Objective-C?

10,439

Solution 1

You can write your own data structure object - for such a simple case, it would be pretty easy:

@interface Pair : NSObject 
{
    NSInteger integer;
    BOOL      boolean;
}
@property (nonatomic, assign) integer;
@property (nonatomic, assign) boolean;
@end

And a matching implementation, then you stick your Pair objects into the NSArray problem free.

Solution 2

Using anonymous struct and struct literals, you might be able to do something like

NSValue * v = [NSValue valueWithBytes:(struct {NSInteger i; bool b;}){i,b} objCType:(struct {NSInteger i; bool b;})];

and then to read,

struct {NSInteger i; bool b;} foo;
[v getValue:&foo];

It's a bit cleaner if you name your struct though.

Share:
10,439
Michael Eilers Smith
Author by

Michael Eilers Smith

Updated on June 08, 2022

Comments

  • Michael Eilers Smith
    Michael Eilers Smith almost 2 years

    I'm new to Objective-C, so please don't judge me too much. I was wondering: Is there an equivalent of the C++ STL pair container I can use in Objective-C?

    I want to build an array that contains an NSInteger associated to an NSBool. I know I could use an array with each entry being a NSDictionary with a single key-value but I find it to be a little overkill.

    Any ideas?

    Thanks.