-2

i have an NSMutableDictionary declared in a class but i want to print get access to the contents of it in another class for example

@interface MyClass0 : NSObject
{

}

@property (nonatomic, strong) NSMutableDictionary *valuee;
@end

and in implementation i do

@implementation MyClass0

- (void)viewDidLoad{
  [super viewDidLoad];

[valuee setObject:@"name" forKey:@"Aryan"];

}

@end

Now i create a new class called MyClass1 where i want to access these

  @interface MyClass1 : NSObject
    {
    }

    @property (nonatomic, strong) NSMutableDictionary *dict;

    @end

and the implementation

@implementation MyClass1
@synthesize dict;

- (void)viewDidLoad{
  [super viewDidLoad];

 self.dict = [[NSMutableDictionary alloc] init];
 MyClass0 *c = [[MyClass0 alloc] init];

 self.dict = c.valuee;

  // dict is not nil but the contents inside is nil so it clearly creates a new instance


}

@end
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Aryan Kashyap
  • 121
  • 1
  • 12

2 Answers2

1

If it's just a simple NSMutableDictionary that has the same contents every time you can create a class method in MyClass0 like so:

+ (NSMutableDictionary *) getDict {
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:@"name" forKey:@"Aryan"];//did you mean [dict setObject:@"Aryan" forKey:@"name"]?
    return dict;
}

To access this, declare the method in the MyClass0.h file like so: + (NSMutableDictionary *) getDict; and simply call [MyClass0 getDict]; in your MyClass1.m file.

If it doesn't have the same contents every time, you'll have to pass the dictionary forward to each view controller in prepareForSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Make sure your segue name in storyboard is the same as this next line
    if ([[segue identifier] isEqualToString:@"MySegue"]) {

        MyClass1 *mc = [segue destinationViewController];
        mc.dict = self.valuee;
    }
}
John Farkerson
  • 2,543
  • 2
  • 23
  • 33
1

You are creating the instance of MyClass0 and valuee is declared but not initialized.

The closest solution to your code is

MyClass0 *c = [[MyClass0 alloc] init];
c.valuee = [[NSMutableDictionary alloc] init];

self.dict = c.valuee;

If a value is assigned to a declared property then an explicit initialization is not necessary.

vadian
  • 274,689
  • 30
  • 353
  • 361