1

I just need some help in creating an array which can store multiple dictionaries. I have :

.h

NSArray * projects;
NSDictionary *project1;
NSDictionary * project2;

.m

projects = /* What should I write here to store the
              above two dictionaries in my array.   */

Thanks,

4 Answers 4

6
projects = [[NSArray alloc] initWithObjects:project1, project2, nil];
Sign up to request clarification or add additional context in comments.

Comments

1

Basically (and this will be right or not quite right depending on you actual code, declarations, properties, etc). Because you declared projects as an NSArray, you could do the following:

projects = [NSArray arrayWithObjects: project1, project2, nil];

This variable will go away magically when it goes out of scope unless you retain it.

Alternatively, if you declare projects as an NSMutableArray and make a property with attributes (retain, nonatomic), you could do something like this:

NSMutableArray *a = [[NSMutableArray alloc] initWithObjects:project1, project2, nil];
self.projects = a;
[a release];

There are several ways to achieve your goal, depending on your overall needs for the array.

4 Comments

You have completely wrong understanding of what mutable objects for.
@kovpas: I disagree; I simply offered an alternative approach which was intended to explore the possibilities of how you can initialize arrays, since the OP didn't really give any clues as to how the array was going to be used. Perhaps the OP doesn't understand the difference between mutable and non-mutable objects, and now they hav something else to go learn about. Perhaps you'll un-ding my answer...
so, in this case you could suggest to use UITableView instead of NSArray. Just in case if we don't know something about what OP really wanted to say ;).
But, yeah, I agree, probably I was wrong with my conclusions about your understanding of mutable objects :)
1

I'm not sure if what you are looking for is more complex then this, but:

projects = [[NSArray alloc ] initWithObjects:project1,project2, nil];

should work.

If you are after something more complex then this let me know I will revise my answer.

4 Comments

lets say i have n numbr of dictioanries and i want all of them stored in NSArray like item 0, item1 etc.
Isn't that just what he wrote? You will be able to obtain the NSDictionary from [projects objectAtIndex:n];
Do you know exactly how many Dictionaries you will have or will the number vary each time this class is called. If you know n then you can use a loop to add each dictionary to an NSMutableArray by calling InsertObject developer.apple.com/library/mac/documentation/Cocoa/Reference/…
That i need to get on load. How i should go about that?
0
arr = [[NSArray alloc ] initWithObjects:dict1,dict2, nil];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.