How can I overcome the "Use of unresolved identifier "myTextField" error?

I have an app that requires the user to enter data into four textfields of View1. When the data is entered in View1, the data is saved to userDefaults. No problem with this.


After the data is entered, View1 flips to View2 which displays calculated data based on the originally entered data. View2 shows the original textfields that were shown in View1 along with the calculated data based on the previously entered textfields. At this point, the user is free to change these same textfields if she so wishes. If she changes any of the data within these textfields it is saved to userDefaults.


The problem I have is that I am having to create two saveData() functions to do this, one in View1 and another in View2. I created a Utilities class in the Utilities.swift file to consolidate my code, but when I attempt to use the following code line:


defaults.set(foodnDrinkTextField.text, forKey: "foodnDrinkTextField")


I get the this error: 'Use of unresolved identifier 'foodnDrinkTextField'.


I understand that this textfield is not present in my Utilities.swift file which generates the error. Is it possible to make this textfield "visible" in the Utilities.swift file?


Thanks for any help anyone can provide.

Is it possible to make this textfield "visible" in the Utilities.swift file?


The visibility of the properties or methods can be controlled with access modifiers such as `private`, `filepriavate`, `internal`, `public` or `open`.

But I wonder if you really understand what properties are. the `foodnDrinkTextField` in View1 and `foodnDrinkTextField` in View2 are completely different things,

even if they have the same name, the same type and the same visual repesentation.


You need to make the instance of View1 and View2 visible to your Utilities.swift and then access `view1.foodnDrinkTextField` or `view2.foodnDrinkTextField` separately.


Protocols or delegate patterns would help to reduce redundant codes, but you have not shown enough code to show an example.

Thank you for your thought provoking response to my question.


You are absolutely correct in that I did overlook the fact that the similarly named text fields are entirely different objects. Lesson learned.


If I have interpreted your comments correctly, does this then mean that saving these identically named textfields from different view controllers to UserDefaults are being saved as separate entities?


If so, then this may account for some odd behavior I am experiencing when printing out the stored UserDefault values. I will leave it at that until I hear back from you.


Thanks again for your help.

If I have interpreted your comments correctly, does this then mean that saving these identically named textfields from different view controllers to UserDefaults are being saved as separate entities?


Seems you have completely misinterpreting my comment. You save the texts from the textfileds into `UserDefaults`, you do not save the textfileds themselves.


So, you need to write 4 things:


For View1.

- Saving the texts of `view1.foodnDrinkTextField` and some other textfields of View1 into `UserDefaults`

- Retrieveing the texts from `UserDefaults` and set them into `view1.foodnDrinkTextField` and some other textfields.


For View2.

- Saving the texts of `view2.foodnDrinkTextField` and some other textfields of View2 into `UserDefaults`

- Retrieveing the texts from `UserDefaults` and set them into `view2.foodnDrinkTextField` and some other textfields.


Maybe very similar to what you were trying before introducing your Utilities.swift.


Please show more code, if you want better advice.

How can I overcome the "Use of unresolved identifier "myTextField" error?
 
 
Q