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.
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.