I have added several class Objects into a NSMutableArray. It seems to have worked however I would like to now access the variables that are inside the class object I have stored into the array, however I am now running into some errors.
Initially I have a NSDictionary of entries that are all NSString based the purpose of the method I am passing this NSArray of NSDictionary/s too is to take each entry in the dictionary and place it into a variable of the correct type. Then this NSObject of variables is passed back to where it was called from.
Below is the code I am using to achieve this.
response.m
// initalise NSOject Class
SearchResultList *searchResultList = [[SearchResultList alloc]init];
// Create mutableArray with compacity (compacity is based of the current array of NSDictionarys (entries are strings))
NSMutableArray *searchObjectArray = [[NSMutableArray alloc] initWithCapacity:[filteredArray count]];
// count to track progress of the array
int myCount = 0;
// for loop goes through the array passing each object in the array over to the search class object
for (id obj in filteredArray) {
// Pass current array object over to the NSObject Class Method, this method assigns the entires of the NSDictionary object to the variables of the object class coorect type values
[searchResultList assignSearchData:filteredArray[myCount]];
// This is where I capture the returning NSObject Class (at least I think thats whats happening.
[searchObjectArray addObject:searchResultList];
// increments count
myCount ++;
}
//..
This is the class method that being called inside the for loop
SearchResultList.m
//return is of type, SearchResultList which is the object class itself... not sure if this is 100% correct.
- (SearchResultList *)assignSeriesSearchData:(NSMutableDictionary*)tempDict
{
//add all of the NSDictionary entries into their own variables of the correct type such as
// initalize DOORID - NSInteger
doorID = [[tempDict valueForKey:@"DOORID"] integerValue];
// initalize DOORDESC - NSString
doorDesc = [tempDict valueForKey:@"DOORDESC"];
// initalize DOOROPEN - BOOL
doorOpen = [[tempDict valueForKey:@"DOOROPEN"] boolValue];
// initalize DOORLETTER - char
doorLetter = [[tempDict valueForKey:@"DOORLETTER"] UTF8String];
//...
//then I return the NSObject Class to the place where it was called
return self;
}
So from here, I end up back in the for Loop of response.m where I call searchObjectArray addObject:searchResultList]; to capture the the returning class object.
At this point I have two questions.
First, am I capturing the the NSObject Class correctly
Second, Once I have added all of the Class Objects to the array how could I then acces the variables of a specific object in the array?
The reason I ask the second question Is because I am wanting to pass this array to a sorting method, which sorts based off one or more variables of the objects that are in the array.
Any help would be greatly appreciated.
To your first question: you should alloc/init a new instance of your
SearchResultListclass within your loop, otherwise you will just be reusing and overwriting the same object. In your question you keep referring to is as anNSObject, which it technically is, being a subclass ofNSObject, but it is really an instance of your custom class. (As a side note I would suggest using a class name likeSearchResultIteminstead rather than SearchResultList, since it is not really a list and this could be confusing to someone looking at your code, but I’ve left it the way you had it.)Also note that since you are using fast iteration for your loop you don’t need your myCount counter, since fast iteration pulls out the next object in your array for you to use with each iteration of the loop.
For your second question, you need to first cast the objects coming out of your array as your custom class (SearchResultList) in order to access the specific property. For example: