Lovely Lovely - 1 month ago 5
iOS Question

Update User Details and save it into existing dictionary with user details

I have a dicionary on ViewControllerA,I am passing that dictionary to ViewController B through Segue.The detailsDictionary contains user details as follows:

{
City = abc;
Country = Indi;
DOB = "12/3/2016 12:00:00 AM";
EmailID = "abc@gmail.com";
FName = dhruv;
LName = kumar;
MName = Singh;
Password = "";
State = Chandigarh;
UserID = "f2266e5d-c709-42fb-ad98-b75873ac2fd1";
}


Now on View Controller B there are texfields with which the user can update their details and post the updated details to server.I have used the following code for it

-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSInteger field = textField.tag;
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[_detailsDictionary setObject:textField.text forKey:@"fname"];

}
if(field ==2)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[_detailsDictionary setObject:textField.text forKey:@"mname"];


}

if(field ==3)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[_detailsDictionary setObject:textField.text forKey:@"lname"];
}

if(field ==4)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[_detailsDictionary setObject:textField.text forKey:@"city"];
}

if(field ==5)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[_detailsDictionary setObject:textField.text forKey:@"state"];
}

if(field ==6)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[_detailsDictionary setObject:textField.text forKey:@"country"];
}

if(field ==7)
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
[_detailsDictionary setObject:textField.text forKey:@"dob"];
}
}


I want that if user updates their detail then new updated details get saved in detailsDictionary ,else if user doesn't make any change detailsDictionary keeps the previous details.But,I am not able to achieve this.I am not understanding that how can I check the dictionary for new details and change the values in case of new details or keep the old values in case of no change.Kindly help and thanks in advance!

Answer

You are doing it correctly, just you need to make sure that the key will be same which user is updating as the keys are case sensitive so you need to make sure you will use the same key, as in your code you are using lowercase while the key contains some uppercase letters

-(void)textFieldDidEndEditing:(UITextField *)textField
    {
        NSInteger field = textField.tag;
        if(field ==1)
        {

        NSLog(@"cell1.userDetailTextField.text %@",textField.text);
            [_detailsDictionary setObject:textField.text forKey:@"FName"];

        }
        if(field ==2)
        {
            NSLog(@"cell1.userDetailTextField.text %@",textField.text);
            [_detailsDictionary setObject:textField.text forKey:@"MName"];


        }

        if(field ==3)
        {
            NSLog(@"cell1.userDetailTextField.text %@",textField.text);
            [_detailsDictionary setObject:textField.text forKey:@"LName"];
        }

        if(field ==4)
        {
            NSLog(@"cell1.userDetailTextField.text %@",textField.text);
            [_detailsDictionary setObject:textField.text forKey:@"City"];
        }

        if(field ==5)
        {
            NSLog(@"cell1.userDetailTextField.text %@",textField.text);
            [_detailsDictionary setObject:textField.text forKey:@"State"];
        }

        if(field ==6)
        {
            NSLog(@"cell1.userDetailTextField.text %@",textField.text);
            [_detailsDictionary setObject:textField.text forKey:@"Country"];
        }

        if(field ==7)
        { 
            NSLog(@"cell1.userDetailTextField.text %@",textField.text);
            [_detailsDictionary setObject:textField.text forKey:@"DOB"];
        }  
    }
Comments