I’m getting a name and a phone number from text fields, till here all is ok.
When I’m trying to add these data to my NSMutableDictionary nothing happens.
ViewController.h
@property(strong,nonatomic) NSMutableDictionary *contacts;
ViewController.m
- (IBAction)btnAdd:(id)sender {
self.userName = self.name.text;
self.userPhoneNumber = self.phoneNumber.text;
[self addToDictionary:self.userName :self.userPhoneNumber];
NSLog(@"ADDED: Key = %@, Value = %@", self.userName, self.userPhoneNumber);
[self clearTextFields];
}
- (void)addToDictionary: (NSString*) _name: (NSString*) _phone{
[self.contacts setObject:_name forKey:_phone];
}
Error message
local declaretion of ‘_name’ hides instance variable
You most likely are not initializing your dictionary.
Add this to your
viewDidLoadmethod:As far as the warning at the end, when you have a declared property, an iVar will automatically be created for you with the same name, but an _ added before. I.E. if you have
nameas a declared property, then an instance variable called_nameis created for you. When you created youraddToDictionary:method, you used the same name. This prevents you from accessing the iVar so it warns you about it. To get rid of the warning, replace_nameand_phonewith something else (likenewNameandnewPhone).