1

I want to save the user default value. but if there is no user default value then I want to assign a value. I found a tutorial that showed me the below method to do this but I am getting an error - Attempt to insert non-property list object. Any idea what's triggering this and how I can fix it. Thank you!

     func saveUserDefaultValues(){
        defaults.set(lenderName.text, forKey: PenaltyKeys.lenderName)
        defaults.set(mortgageAmount.text, forKey: PenaltyKeys.mortgageAmount)
        defaults.set(orginalDiscount, forKey: PenaltyKeys.orginalDiscount)
        defaults.set(orginalStartDate, forKey: PenaltyKeys.orginalStartDdate)
        defaults.set(mortgageRate, forKey: PenaltyKeys.mortgageRate)
        defaults.set(cashback, forKey: PenaltyKeys.cashback)

    }

    func checkForDefaultValues () {
        let name = defaults.value(forKey: PenaltyKeys.lenderName) as? String ?? "Bank of Montreal"
        lenderName.text = name
        let amount = defaults.value(forKey: PenaltyKeys.mortgageAmount) as? String ?? "$300,000"
        mortgageAmount.text = amount

        let dateformatter = DateFormatter()
        dateformatter.dateStyle = .medium
        let now = dateformatter.string(from: (Calendar.current.date(byAdding: .year, value: -3, to: Date()))!)

        let StartDate = defaults.value(forKey: PenaltyKeys.orginalStartDdate) as? String ?? now
        orginalStartDate.text = StartDate

        let term = defaults.value(forKey: PenaltyKeys.mortgageTerm) as? String ?? "5 Year"
        mortgageTerm.text = term

        let discount = defaults.value(forKey: PenaltyKeys.orginalDiscount) as? String ?? "1.70%"
        orginalDiscount.text = discount

        let rate = defaults.value(forKey: PenaltyKeys.mortgageRate) as? String ?? "3.0%"
        mortgageRate.text = rate

        let cashbackValue = defaults.value(forKey: PenaltyKeys.cashback) as? String ?? "$0.00"
        cashback.text = cashbackValue


    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
k.thomas
  • 51
  • 7
  • 1
    Surely one of [these search results](https://stackoverflow.com/search?q=%5Bswift%5D+Attempt+to+insert+non-property+list+object) answers your question. – rmaddy Mar 18 '19 at 02:58
  • 2
    And don't use `value(forKey:)` with `UserDefaults` unless you have a clearly understood need to use key-value coding. Look at the documentation for `UserDefaults` and use one of the listed methods. In your case, most appear to best suited to `string(forKey:)`. – rmaddy Mar 18 '19 at 03:00
  • 1
    Possible duplicate of [Attempt to insert non-property list object when trying to save a custom object in Swift 3](https://stackoverflow.com/questions/41355427/attempt-to-insert-non-property-list-object-when-trying-to-save-a-custom-object-i) – El Tomato Mar 18 '19 at 03:22
  • 2
    Im assuming orginalStartDate, orginalDiscount are labels? In your saveUserDefaultValues did you forget to add orginalStartDate.text when saving just like you did with lenderName? – adri Mar 18 '19 at 03:31

0 Answers0