How can I get my object index? I have a dictionary containing arrays and inside the arrays i have multiple dictionary.
My data structure(eg):
Student —— NSDictionary
Item 0 ------ NSArray<br> Name ----- Grace<br> Age ----- 20<br> Item 1 ------ NSArray<br> Name ----- Anne<br> Age ----- 21<br>
So for example, I have the value of the name, Grace, and I want to get the value of the object index array (in this case, item 0). How can I do so?
I’ve used the indexOfObject however the results I got back is 2147483647, which i think it means nsnotfound. So i think it doesnt work for this case.
This are my codes:
NSMutableDictionary* namevalue = [[NSMutableDictionary alloc] init];
namevalue = [somedict objectForKey:@"Name"];
int arryindex;
arryindex = [somearray indexOfObject:namevalue];
NSLog(@"Array Index:%i", arryindex);
Can anyone help? Thank you so much!
If I understood correctly, you’re looking for a way to find object index in NSDictionary based on the value of property that object has, right?
E.g. your
studentsdictionary has a student with nameGraceand you would like to know at which index the student object with nameGraceis stored…If above is true, here’s my (incomplete and rough) solution which works only for objects with
NSStringproperties, but once you get the idea below, I think you’ll be able to modify code to match your needs.and don’t forget to include runtime header:
My code may and probably have some issues:
isEqual:method which needs to compare properties’ values.I hope my “answer” will make it easier for you to implement what you need.