Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C - KeyValuePair class?

I'm looking for a class in Objective C similar to C#'s KeyValuePair (even without the generics). Just anything that has a first/second object. I can create my own with no problem, but I figure if one is already there, then there is no need to re-invent the wheel. I have not had any luck findind one myself... Does anyone know of one?

like image 574
Kyle Avatar asked Aug 20 '10 13:08

Kyle


People also ask

How do I create an NSDictionary in Objective C?

Creating NSDictionary Objects Using Dictionary Literals In addition to the provided initializers, such as init(objects:forKeys:) , you can create an NSDictionary object using a dictionary literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:forKeys:count:) method.

What is NSDictionary?

Dictionaries. NSDictionary. NSDictionary is a class used to organize objects in lists using keys and values. NSDictionary can maintain an index of objects and let you retrieve an object if you have the right key. Usually, the key will be an NSString object while the value will be whatever type of object you are ...

How do you use key-value pairs?

A key-value pair consists of two related data elements: A key, which is a constant that defines the data set (e.g., gender, color, price), and a value, which is a variable that belongs to the set (e.g., male/female, green, 100). Fully formed, a key-value pair could look like these: gender = male. color = green.


3 Answers

What about using plain C structs? It's "quicker" than writing an entire class.

typedef struct KeyValuePair {
     const char *key;
     const char *value;
} KeyValuePair;

//init like this
KeyValuePair kvp = {"yourkey", "yourvalue"}

Things to keep in mind:

  • Structs are passed by value.
  • ARC prevents using ObjC objects with structs but C primitives are still supported.
  • You cannot add structs to ObjC collection classes (NSArray, NSDictionary, etc).
like image 112
Jay Q. Avatar answered Oct 03 '22 05:10

Jay Q.


How about a simple NSArray?

NSArray* kvp = [NSArray arrayWithObjects: key, value, nil];
// or, using boxed literals,
NSArray* kvp = @[key, value];
...
NSObject* key = [kvp firstObject];
NSObject* value = [kvp lastObject];

You could make a function to wrap +arrayWithObjects: (and handle the case of a nil key or value which would trip up the simple approach)

Accessing the elements of an NSArray will probably be quicker than an NSDictionary.

like image 20
Peter Hull Avatar answered Oct 03 '22 05:10

Peter Hull


So basically like a hashmap, right?

Use NSMutableDictionary

Example from here:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; 

// Add objects to a dictionary indexed by keys

[dictionary setObject:@"A Book about the Letter A" forKey:@"A"];
[dictionary setObject:@"A Book about the Letter B" forKey:@"B"];
[dictionary setObject:@"A Book about the Letter C" forKey:@"C"];

// Retrieve an object from a dictionary with a key

NSLog([dictionary objectForKey:@"B"]);

// Release a dictionary

[dictionary release];
like image 33
mbrevoort Avatar answered Oct 03 '22 07:10

mbrevoort