I want to share an NSMutableDictionary between several UITableViews. The point is so that in one view I can add an array as the value and a corresponding key to the dictionary, and then set the dictionary property of the SingletonObject. Then in another view I can access the array in the dictionary via the SingletonObject's property.
For the SingletonObject, in the header file, I have this:
@property(nonatomic) NSMutableDictionary * dict;
+(SingletonObject *) sharedManager;
In the implementation file for the SingletonObject I have this:
@synthesize dict;
+(SingletonObject *) sharedManager { static SingletonObject * sharedResourcesObj = nil;
@synchronized(self)
{
if (!sharedResourcesObj)
{
sharedResourcesObj = [[SingletonObject alloc] init];
}
}
return sharedResourcesObj;
}
Then I do the following in one of my UITTableView classes
// instantiate the SingletonObject
sharedResourcesObj = [SingletonObject sharedManager];
// instantiate array
NSMutableArray *courseDetails = courseDetails = [[NSMutableArray alloc]init];
// put textview value into temp string
NSString *tempString = tempString = [[NSString alloc]initWithString:[_txtBuildingRoom text]];
// put textview value into array (via temp string)
[courseDetails addObject:tempString];
// set dictionary property of SingletonObject
[sharedResourcesObj.dict setObject:courseDetails forKey:_lblCourse.text];
The problem is that when I print everything out to the console, line by line, everything has a value and is working correctly, EXCEPT the dictionary's new value is not there.
When I check the value or the count of the dictionary with the code below, the count is 0 and there are no objects in the dictionary.
// dictionary count
NSLog(@"%i", sharedResourcesObj.dict.count);
// get from dictionary
NSMutableArray *array = [sharedResourcesObj.dict objectForKey:_lblCourse.text];
// display what is in dictionary
for (id obj in array)
{
NSLog(@"obj: %@", obj);
}
I am using the correct concept for sharing a dictionary between UITableViews?
Is there some issue with my implementation of my SingletonObject?
I've used this implementation of the SingletonObject before to share integer values between tabs and there were absolutely no issues. The only difference now is that the property of the SingletonObject is not an integer, but an NSMutableDictionary.
Can anyone help?