设置用户默认错误-尝试插入非属性列表对象

时间:2019-03-18 02:53:57

标签: swift nsuserdefaults

我要保存用户默认值。但是,如果没有用户默认值,那么我想分配一个值。我找到了一个教程,向我展示了执行以下操作的以下方法,但出现错误-尝试插入非属性列表对象。不知道是什么触发了这一问题以及如何解决。谢谢!

     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


    }

0 个答案:

没有答案