简体   繁体   中英

How to save data from two UITextViews to UserDefaults

For each UITextView using UserDefaults , I've made a function to save and a function to display.

Whatever text is added needs to be displayed at the time of adding, saved and then displayed again when opening the app again.

If I install the app with ONLY the function to save, quit the app and add the function to display then reinstall without deleting the installed app everything works perfectly.

If I install the app with both functions added it doesn't work.

There has to be a simple solution for this, I'm obviously doing something wrong.

The data from one textView is used to calculate results and then to display them on the other textView . All data is added with other functions, none by the user.

    numberHistoryView.isEditable = false
    numberHistoryView.isSelectable = false

func saveHistoryTextView()
{
    let defaults = UserDefaults.standard
    let numberHistory = numberHistoryView.text
    defaults.set(numberHistory, forKey: "combos")
}
func displaySavedHistory()
{
    let defaults = UserDefaults.standard
    let savedCombos = defaults.object(forKey: "combos") as? String ?? ""
    numberHistoryView.text = savedCombos
}

func saveFrequencyTextView()
{
    let defaults = UserDefaults.standard
    let numberFrequency = numberFrequencyCount.text
    defaults.set(numberFrequency, forKey: "frequency")
}
func displaySavedFrequency()
{
    let defaults = UserDefaults.standard
    let savedFrequency = defaults.object(forKey: "frequency") as? String ?? ""
    numberFrequencyCount.text = savedFrequency
}


override func viewWillDisappear(_ animated: Bool)
{
    saveHistoryTextView()
    saveFrequencyTextView()
}
override func viewWillAppear(_ animated: Bool)
{
    displaySavedHistory()
    displaySavedFrequency()
}

This depends on the order and timing in which you are calling save and display methods.

  1. When you're installing a fresh app, there will be no data in saved in UserDefaults . So when you call displaySavedHistory() and displaySavedFrequency() methods in viewWillAppear(_:) , nothing will be fetched because nothing is saved yet.

  2. Now, when you save the data using saveHistoryTextView() and saveFrequencyTextView() methods in viewWillDisappear(_:) and then you kill and run the app again, the saved data will be fetched and displayed.

Also, since you're saving the data in UserDefaults , and UserDefaults are saved within the sandbox , so the data won't persist when you delete the app. You've to save the data in iCloud or keychain etc. if you want to persist the data even after app deletion.

Once I put my brain into a theta state with the right frequency I managed to figure it out.

Thanks to @Naresh and all other contributors for trying to help as you may have assisted me a little.

The solution basically just required a simple if statement.

Everything now works perfectly.

func saveHistoryTextView()
{
    if numberHistoryView.text?.count != nil
    {
        let defaults = UserDefaults.standard
        defaults.set(numberHistoryView.text!, forKey: "combos")
    }
}
func displaySavedHistory()
{
    let defaults = UserDefaults.standard
    if let savedCombos = defaults.string(forKey: "combos")
    {
        numberHistoryView.text = savedCombos
    }
}

func saveFrequencyTextView()
{
    if numberFrequencyCount.text?.count != nil
    {
        let defaults = UserDefaults.standard
        defaults.set(numberFrequencyCount.text!, forKey: "frequency")
    }
}
func displaySavedFrequency()
{
    let defaults = UserDefaults.standard
    if let savedFrequency = defaults.string(forKey: "frequency")
    {
        numberFrequencyCount.text = savedFrequency
    }
  }
}


override func viewWillDisappear(_ animated: Bool)
{
    saveHistoryTextView()
    saveFrequencyTextView()
}
override func viewWillAppear(_ animated: Bool)
{
    displaySavedHistory()
    displaySavedFrequency()
}

You can do it with property observer as:

private let DATA_KEY = "Saved Data"

//After initialising the outlet we can set the data
@IBOutlet weak var textView: UITextView! {
    didSet {
        textView.text = self.data
    }
}

private var data: String {
    set {
        //Save data in user defaults
        UserDefaults.standard.set("The value you will assign", forKey: DATA_KEY)
    }
    get {
        //get the data from user defaults.
        return UserDefaults.standard.value(forKey: DATA_KEY) as? String ?? ""
    }
}

//UITextViewDelegate: set the text data on end of UITextView editing
func textViewDidEndEditing(_ textView: UITextView) {
    self.data = textView.text
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM