0

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?

Zolt
  • 2,761
  • 8
  • 43
  • 60

2 Answers2

1

You have to actually create the dictionary in your singleton object, otherwise it'll just be nil. You'd typically do that in the singleton's init method.

- (id)init
{
    self = [super init];
    if (self) {
        dict = [NSMutableDictionary new];
    }
}
omz
  • 53,243
  • 5
  • 129
  • 141
  • That's sort of what I was guessing the issue might be, but wasn't really sure or what to do ... What do you mean by init method? Where would I put that, in the SingletonObject implementation file, after the +(SingletonObject *) sharedManager {} method? – Zolt Feb 19 '13 at 06:43
  • Do you mean put it in this method of my UITabableView class - (id)initWithStyle:(UITableViewStyle)style? – Zolt Feb 19 '13 at 06:49
  • Or in the ViewDidLoad? – Zolt Feb 19 '13 at 06:49
  • Never mind I got it working. The method goes in the SingletonObject implementation file. Thanks very much for the help. – Zolt Feb 19 '13 at 06:52
1
 @synchronized(self)
{
 if (!sharedResourcesObj)
  {
    sharedResourcesObj = [[SingletonObject alloc] init];

  }
}

 return sharedResourcesObj;
}

 - (id)init 
{
  if (self = [super init]) 
  {
    _dict = [NSMutableDictionary alloc]init];
   }
  return self;
}
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110