Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jules2010

macrumors member
Original poster
Apr 7, 2010
34
0
I need to store multiple values in an NSDictionary, I've discovered that you can use a container class, see this link.

http://www.cocoabuilder.com/archive/cocoa/282022-store-more-complex-values-into-nsdictionary.html

Heres an example container class
Code:
@interface My_Pair : NSObject
{
id first;
id second;
}
+ (My_Pair *)pairWith:(id)_first and:(id)_second;

- (id)initWith:(id)_first and:(id)_second;

- (id)first;
- (id)second;
@end

Heres the code I would normally use to add one value to an NSDictionary.
Code:
NSMutableDictionary *dictResult = [[[NSMutableDictionary alloc] init] retain];
[dictResult setValue:sWavResult			forKey:@"sWav"];

I realise I'd need to use setObject instead of setValue.

I've read that id can be used for any type of object, however I'm not sure how to use the container class with an NSDictionary and how to add the values to it.

I really need some kind person to provide me with an example.

Many thanks in advance.
 
Well I need to store value(s) and key(s) which won't be unique. Also the values / keys need to be in a specific order.

Granted I won't change this order, but I'm concerned this ordering may be lost.

I have got some code to iterate the dictionary.
Code:
NSMutableDictionary* aDictionary = (initialized with some objects)

for (id theKey in aDictionary) {
	id anObject = [[aDictionary objectForKey:theKey] retain];
}

On the premise that I can't have none-unique keys, I'm going to need to extract a value, so it just made sense to have the values separate to start with.

I need to store the name of a file, a number related to that file and as I say something unique for the key.
 
I'm really new to Obj-C and using mutli-dimensional arrays (I'm assuming thats what your suggestion) seems a big step at the moment.

Besides which I don't have any examples of those either.
 
Sorry, I meant arrays and structures / classes, not multi-dimensional array.
 
Maybe it's time to step back from the real coding then and make sure you understand the basics of Objective-C.

Fair enough, but if I can't find an example of what I want to do, re. nsdictionay's / arrays with structures / classes, I won't be any further ahead.
 
Any decent Objective-C educational material is going to show you how to create an array of objects, how to use dictionaries, etc., maybe not just specifically for how you'll be using them. Learning the basics is a matter of learning the building blocks. It's up to you and your imagination to put them together to solve your problems. You can not always rely on finding examples to show you how things are done. Sometimes you'll find something similar that can serve as inspiration. Other times, you're on your own.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.