PDA

View Full Version : Quick hand with array of strings




teengohan
May 22, 2011, 03:56 PM
I have the following code in my viewDidLoad meathod:

for (id key in dictionary) {
NSString *name;
name = [dictionary valueForKey:@"name"];
[listOfNames addObject:name];
NSLog(name);

}


list of names is declared in the header file as:

@interface RootViewController : UITableViewController {
NSMutableArray *listOfNames;
}
@property (nonatomic, retain) NSMutableArray *listOfNames;


@end


For some reason the console output is what I expected, a list of names but no values are being added to the array. I put a breakpoint in at the end of viewDidLoad and the array has zero entries. Does anyone know where i am going wrong here?



robbieduncan
May 22, 2011, 03:58 PM
Where are you creating listOfNames? Declaring a property is declaring a variable: a space to store the object in. It does not create a NSMutableArray object for you. You must create the object somewhere and assign that to the property.

teengohan
May 22, 2011, 04:36 PM
Looks like I was misunderstanding a bit of objective c there. I thought it was initialised when I first assigned it values here:
[listOfNames addObject:name];
is my problem that I have declared the array but not initialised it? If so how do I initialise it? Sorry for newbie question, I'm new to this :P

Scott90
May 22, 2011, 04:40 PM
Looks like I was misunderstanding a bit of objective c there. I thought it was initialised when I first assigned it values here:
[listOfNames addObject:name];
is my problem that I have declared the array but not initialised it? If so how do I initialise it? Sorry for newbie question, I'm new to this :P


NSMutableArray *listOfNames = [[NSMutableArray alloc] initWithCapacity:[dictionary count]]


If you put this right before your for loop, it should work. You now have initialized (initWithCapacity) the variable so you can add objects to it.

teengohan
May 22, 2011, 05:02 PM
Ok, I did that but now I get a yellow warning that the "Local declaration of 'listOfNames' hides instance variable". I ned to be able to access this array after this meathod is done as I need to use its contents in another part of my app, one example is I later use this array to populate a table..

Scott90
May 22, 2011, 05:05 PM
Ok, I did that but now I get a yellow warning that the "Local declaration of 'listOfNames' hides instance variable". I ned to be able to access this array after this meathod is done as I need to use its contents in another part of my app, one example is I later use this array to populate a table..

I'm sorry, forgot it was an ivar. The correct code is:


listOfNames = [[NSMutableArray alloc] initWithCapacity:[dictionary count]];

teengohan
May 22, 2011, 05:27 PM
Excelent :D that did the trick, thanks for the help.

dejo
May 23, 2011, 03:36 PM
for (id key in dictionary) {
...


Are you really wanting to do enumeration on a dictionary, a mechanism that's meant for arrays?

robbieduncan
May 23, 2011, 03:59 PM
Are you really wanting to do enumeration on a dictionary, a mechanism that's meant for arrays?

He probably wanted to do this:


for (id key in [dictionary keyEnumerator])


Which is apparently legal (http://cocoawithlove.com/2008/05/fast-enumeration-clarifications.html)