How do I load multiple arrays into an NSDictionary object (Objective-C; iPhone SDK)?
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: @"Cin开发者_如何转开发cinnati, 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.
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:
[NSArray arrayWithObjects:…
is the format for the particular class method you are trying to call.[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.- 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.
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...
Help Still Needed!
Well, I believe my dictionary is set-up very well now, but yet I continue to stumble when accessing the values of the dictionary...my table view data source just decides to crash...here is the next set of code...in snippets.
SettingsDataObject *myDataObject = [[SettingsDataObject alloc] init];
self.myNewDictionary = [myDataObject returnDictionary];
[myDataObject release];
self.myShopNameArray = [myNewDictionary objectForKey:@"Shop Name"];
self.myShopLocationArray = [myNewDictionary objectForKey:@"Shop Location"];
self.myShopImageArray = [myNewDictionary objectForKey:@"Shop Image"];
self.mySelectedShopArray = [myNewDictionary objectForKey:@"Shop Selected"];
The above occurs first in my cellForRowAtIndexPath: method. Then once I determine if a cell exists and all that other hoopla, I then set the textlabel.text of the cell using the shop name array which is stored within the dictionary at key @"Shop Name".
Now, I am not sure if it is the above code or the below...but when get rid of both and go with a locally defined array and plug that in...everything works fine. Again, something missing here. I tried this and the second line below...error must be in the above.
cell.textLabel.text = [[myNewDictionary objectForKey:@"Shop Name"] objectAtIndex:indexPath.row]
cell.textLabel.text = [myShopNameArray objectAtIndex:indexPath.row];
Change
[[NSDictionary alloc] retain]
to
[[NSMutableDictionary alloc] retain]
精彩评论