tags:

views:

37

answers:

4

This method doesn't work...but when I move away from returning a dictionary object and return a single array to the table data source, it works perfectly. so the error of my ways is in how i am creating this dictionary...

Any Help?

- (NSDictionary *) returnDictionary {
self.shopNameArray = [[NSArray alloc] arrayWithObjects: @"Item 1", @"Item 2", @"Item 3", nil];
self.shopLocationArray = [[NSArray alloc] arrayWithObjects: @"Cincinnati, OH", @"Phoenix, AZ", @"Tuscon, AZ", nil];
self.shopImageArray = [[NSArray alloc] arrayWithObjects: @"image1", "image2", @"image3", nil];

NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] retain];

[theDictionary setObject:shopNameArray forKey:@"Shop Name"];
[theDictionary setObject:shopLocationArray forKey:@"Shop Location"];
[theDictionary setObject:shopImageArray forKey:@"Shop Image"];

return theMechanicDictionary;
}

Thanks.

A: 

Change

[[NSDictionary alloc] retain]

to

[[NSMutableDictionary alloc] retain]

tadej5553
It changed things, but did not solve my problem entirely. Good catch, by the way. Here is my new console output - though the application doesn't bomb out, it doesn't display anything in the table cells.-[__NSPlaceholderArray arrayWithObjects:]: unrecognized selector sent to instance 0x3906470
newDeveloper
`[[AnyClass alloc] retain]` is wrong and will result in a leaked, half-baked object that will probably crash the first time you do anything to it.
Jonathan Grynspan
A: 

You need to call arrayWithObjects: on NSArray directly, not on an instance of NSArray. So use [NSArray arrayWithObjects: @"Item 1", @"Item 2", @"Item 3", nil]; instead of [[NSArray alloc] arrayWithObjects: @"Item 1", @"Item 2", @"Item 3", nil];.

Also change [[NSDictionary alloc] retain] to [NSMutableDictionary arrayWithCapacity:0], so you're returning an autoreleased NSMutableDictionary. Also, you should always call [[SomeClass alloc] init] or [[SomeClass alloc] initWith...], not [[SomeClass alloc] someMethod], that doesn't work.

It looks to me like you don't really understand what methods are supposed to be called on what exactly...

Douwe Maan
Thank you Douwe. I do appreciate it. And yes, you are absolutely correct - I don't fully understand just yet...hence why I post under the name "newDeveloper". The name alone is supposed to inform you of just that. But none the less, I really appreciate the lesson today. Thank you.
newDeveloper
A: 

Fixed block of code:

- (NSDictionary *) returnDictionary {
    self.shopNameArray = [NSArray arrayWithObjects: @"Item 1", @"Item 2", @"Item 3", nil];
    self.shopLocationArray = [NSArray arrayWithObjects: @"Cincinnati, OH", @"Phoenix, AZ", @"Tuscon, AZ", nil];
    self.shopImageArray = [NSArray arrayWithObjects: @"image1", "image2", @"image3", nil];

    NSMutableDictionary *theDictionary = [NSMutableDictionary dictionary];

    [theDictionary setObject:self.shopNameArray forKey:@"Shop Name"];
    [theDictionary setObject:self.shopLocationArray forKey:@"Shop Location"];
    [theDictionary setObject:self.shopImageArray forKey:@"Shop Image"];

    return theDictionary;

}

Changes made:

  1. [NSArray arrayWithObjects:… is the format for the particular class method you are trying to call.
  2. [NSMutableDictionary dictionary] is the easiest way to get an autoreleased mutable dictionary. Like @Douwe suggested, you may want to init this object with a starting size of 3, but that's not necessary. It only makes a difference if you're loading a butt-ton of objects into your dictionary and you don't want it to continuously be resizing itself.
  3. When adding the arrays to your dictionary, you shouldn't reference them by the name of the iVar. Since you set the arrays using properties (self.shopNameArray =) the name of the iVar will not necessarily be the same.
kubi
"Kubi" - thank you as well. Very much appreciated. This forum is great (as long as I keep thick skin). Take care!
newDeveloper
A: 
newDeveloper
You should either add this to your origin question or ask a new one. It's not appropriate to "answer" your question with another question. Also: don't worry about it, everyone does this.
kubi
Lord knows I can't answer my own questions! LOL. Thanks!
newDeveloper