0

Aiming for simplicity, how does one share data among functions within a view controller? Ideally, I would like to use a NSMutableDictionary, but my approach doesn’t seem to work (below):

In ViewController.m:

- (void) viewDidLoad{
...
  NSMutableDictionary * movietem = [[NSMutableDictionary alloc] init];
  [movieItem setValue:@“frozen” forKey:@“title” ];
  [movieItem setValue:@“PG” forKey:@“rating” ];
  [movieItem setValue:@“tom cruise” forKey:@“cast” ];  
....
}

-(IBAction) updateTitleBtn:(UIButton *)sender{
…
  [movieItem setValue:@"lion king" forKey:@"title"];
...
} 


-(IBAction) updateCastBtn:(UIButton *)sender{
…
  [movieItem setValue:@"forest whitaker" forKey:@"cast"];
...
} 

Concluding in an error: ‘Unknown receiver ‘movieItem’. Thanks for the input.

0--
  • 221
  • 1
  • 6
  • 16

4 Answers4

2

You get that error because movieItem is a local variable that's only valid within the viewDidLoad method where you created it. You need to create an ivar or property.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
1

movieitem is a local variable thus it can not be used in other methods.

Thus, a better way to share a variable between methods is to declare a property.

Try to add the code below into the head of your XXViewConttroller.m files.

@interface  XXViewConttroller()
@property(nonatomic,strong) NSMutableDictionary* movie;
@end

-(NSMutableDictionary*)movie{
   if(!_movie){
         _movie = [NSMutableDictionary dictionary];
   }
   return _movie;
}

In which I declared a "private property" in .m files and initialized the variable in the getter of the propery.

you can use self.movie to call the property in other part of your code.

Exia
  • 2,381
  • 4
  • 17
  • 24
0

movieItem is local variable and will be accessible only in viewDidLoad method's scope. to access it in another function please make it global or use property.

Viper
  • 1,408
  • 9
  • 13
0

You have created your dictionary at -(void)viewDidLoad method in .m file.
In this sense your MutableDictionary is only accessible within viewDidLoad method. You can make your * movietem Dictionary public (accessible by all methods)
Here is a solution

Add this code below @interface ViewController : UIViewController in your ViewController.h file

 @property (nonatomic, strong) NSMutableDictionary *movietem; 
 // This line will create a public mutableDictionary which is accessible by all your methods in ViewController.m file

Now add this code below @implementation ViewController in ViewController.m file

@synthesize movietem; // This line will create getters and setters for movietem MutableDictionary


Now you can use your movietem dictionary anywhere you want
As per your code,

- (void) viewDidLoad {
...
      movietem = [[NSMutableDictionary alloc] init]; // allocating memory for movietem mutable Dictionary
      [movieItem setValue:@“frozen” forKey:@“title” ];
      [movieItem setValue:@“PG” forKey:@“rating” ];
      [movieItem setValue:@“tom cruise” forKey:@“cast” ];  
....
}
-(IBAction) updateTitleBtn:(UIButton *)sender {
...
[movieItem setValue:@"lion king" forKey:@"title"];
...
} 

-(IBAction) updateCastBtn:(UIButton *)sender{
...
[movieItem setValue:@"forest whitaker" forKey:@"cast"];
...
} 
DevdDexter
  • 63
  • 8