Reputation: 1532
I want to check for conditions in my UITextFields
, basically, if it changed, if so, I want to change data and save to database. My code looks something like this:
if (![self.nomeTextField.text isEqualToString:self.usuario[@"nome"]]) {
self.usuario[@"nome"] = self.nomeTextField.text;
}
if (![self.sobrenomeTextField.text isEqualToString:self.usuario[@"sobrenome"]]) {
self.usuario[@"sobrenome"] = self.sobrenomeTextField.text;
}
if (![self.nascimentoTextField.text isEqualToString:self.usuario[@"nascimento"]]) {
self.usuario[@"nascimento"] = self.nascimentoTextField.text;
This code is inside button
action method
. When user presses button
, it checks the UITextFields
that changed and update the values to my object
. That way, I make only one network connection to save the object
at once.
Now, my question is if that is okay to have repeated if statements
like that and if there's a simpler way to do this. I'm not concerned about the UITextFields
, I know I can use delegate
to change each textField
at once and save it later. But what I am looking for is to understand if what I am doing is right and if there's an alternative to these repeated if statements
.
Upvotes: 0
Views: 94
Reputation: 17612
I would implement a UITextFieldDelegate
's textFieldDidEndEditing:
method, and keep track of which fields were edited there. Then, when the button is clicked, you could go through that list and update the related fields.
Or you could modify this approach and update your object right in textFieldDidEndEditing:
.
Upvotes: 1
Reputation: 51
What about using a dictionary, and then looping through each key-value pair in the dictionary to check the fields?
Set each field you want to check as a key (self.nomeTextField, for example), and the test value as the value (self.usario[@"nome"]).
NSDictionary *myDictionary = @{
self.nomeTextField : self.usario[@"nome"],
self.sobrenomeTextField : self.usario[@"sobrenome"],
AND SO ON AND SO FORTH . . .
};
Then, just iterate through the dictionary running something along these lines
for ($KEY in myDictionary)
if (!$KEY.text isEqualToString:$VALUE) {
DO SOMETHING HERE
}
This has the benefit of being easy to expand, should you want more fields in the future.
I apologize if there are gaps in my code/concepts here. I'm not able to test specific code to verify functionality at the moment.
Hope this helps! Good luck!
Upvotes: 1
Reputation: 119021
In your case a switch statement won't work because you always want to process all of the text fields. In this case you could place all of the text fields into a dictionary where the keys are the names of the keys in your self.usuario
dictionary. Then, you can iterate the array and have the names of your keys and the associated text fields to verify.
If you wanted to check each text field as it changed then you need to be able to correlate the text filed passed as the parameter with the key name explicitly. This could be done by searching, or you could consider using objc_setAssociatedObject
to associate the key string.
In either case, using some data structure can help to reduce your code. But it's a tradeoff. Reduced code usually means greater memory usage, but it's usually easier to understand and often less error prone.
Upvotes: 2