NSMutableArray - 2D array access trouble
I'm having a bit of trouble accessing what I think element should be in my array. I am getting results from a webservice and trying to make sense of them.
The service returns an NSMutableArray called GroupOfHouses.
I have set up my code to set an array as such
NSMutableArray *myHousesGroup;
Then populate it
myHousesGroup = result
NSLog(@"Result = %@", result);
NSLog(@"A开发者_如何学Crray Count: %d",[myHousesGroup count]);
So the above outputs
2010-07-23 23:03:33.628 demo4[40880:207] Result = (
<GroupOfHouses: 0x4c2f410>,
<GroupOfHouses: 0x4c303b0>
)
2010-07-23 23:03:33.631 demo4[40880:207] Array Count: 2
I want to see what's inside the NSMutableArray which seems to be inside the result array. I come from a PHP background, so the array structure is familiar, but not the method of accessing! I tried NSLog(@"Array Count: %d",[myHousesGroup[0] count]);
but that didn't work.
How can I set new arrays based on the number of results? It's basically that the result from the webservice could return 10 arrays of house information.
Thanks for any information.
You access the first element with:
[myHousesGroup objectAtIndex:0]
You create a new array with
NSMutableArray* list = [[NSMutableArray alloc] init];
You add with:
[list addObject:o]; // where o is an object you want in the array
You get the count with
[list count];
http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html
I'm quite new myself to this coming from a web developer background. I'm not 100% sure this is the best direction to go but it's a good starting place.
Over the last couple of days I've been in a similar position trying to make sense of NSMutableArrays.
It would appear that from your code that 'result' has some sort of structure that is automatically translated into the array myHousesGroup as an object.
Through working with coredata, I've worked out that to get values from arrays that consist of objects you need to create a new, and populate, an object of the same structure as the object that is an element in the array that you need to get the values out of.
Along the lines of:
Recipes *selectedRecipe = [entityArray objectAtIndex:indexPath.row ];
//Recipes is a class.
//selectedRecipe is an instance of the class Recipes which has the structure I need to get values out of
//entityArray is my NSMutableArray similar to your 'myHousesGroup'
recepeTitle.text = selectedRecipe.RecipieName;
//recipeTitle.text is a label displaying a value from my object
cookingTime.text = selectedRecipe.CoookingTime;
//I think you can see the pattern now
To get the upper bounds (the number of elements) of the array you might try (using your example)
NSLog(@"myHousesGroup Array Count: %i",[myHousesGroup count]);
I'm not sure if I can help with creating arrays based on the number of results. You might need a bit more detail of the logic you need. Are you reading values from the webservice and populating arrays based on values etc? What's the end result of this portion of code in your app?
Ask me if you need info on the class Recipes and how it is constructed, what properties are present
Hope this helps a bit
精彩评论