使用NSUserDefaults与结构类型的数组 [英] use NSUserDefaults with array of struct type

查看:68
本文介绍了使用NSUserDefaults与结构类型的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚如何使用NSUserDefaults保存 RiskEntry类型的字符串。我已经看过其他文章,但是还是无法解决这个特殊问题。

I have problems to figure out how to save my string of type "RiskEntry" with NSUserDefaults. I already went through some other posts, but somehow I still did not manage to solve this particular issue.

现在让我解释一下下面的代码是做什么的:我得到了以下代码段中来自我的类​​CustomCell的一些数据。在这里,我首先使用标识符检查要使用新数组值结果更新的数组。

Let me explain what the code from below does right now: I get some data from my class CustomCell in the following code snippet. Here I first check with an "identifier" which array to update with the new array value "consequences".

一切正常,更新后的数组存储在riskEntry中。
但是,我现在不知道如何使用NSUserDefaults存储它。当我尝试例如 riskItemDefaults.set(riskEntry,forKey: riskItem)我收到异常错误。

It all works fine and the updated array is stored in riskEntry. However, I cannot work out how to store this with NSUserDefaults now. When I try it with e.g. riskItemDefaults.set(riskEntry, forKey: "riskItem") I get an exception error.

任何想法我

SWIFT3 (我删除了与此问题无关的所有代码)

SWIFT3 (I removed all code not relevant for this question)

class: RiskPlan: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomCellUpdaterDelegate {

 var riskEntry = [RiskEntry]()
 var riskItemDefaults = UserDefaults.standard

// ------ start of delegate function (receiving from CustomCell) ---------
      func transferData(consequencesTranferred: String, identifier: String) {
        if let index = riskEntry.index(where: {$0.title as String == identifier}) {
            riskEntry[index].consequences = consequencesTranferred
    } else {
        print ("nothing")
    }

// save with NSUserDefaults
        riskItemDefaults.set(riskEntry, forKey: "riskItem")
  }
}

这是我的结构:

public struct RiskEntry {
    let title: String
    var consequences: String
}

我的自定义单元格

// ---------------- delegate to transfer entered data to VC -----------------
protocol CustomCellUpdaterDelegate {
    func transferData(consequencesTranferred: String, identifier: String)
}

// ---------------- start of class CellCustomized -----------------
class CustomCell: UITableViewCell, UIPickerViewDataSource, UIPickerViewDelegate, UITextViewDelegate {

    var delegate: CustomCellUpdaterDelegate?

    // text fields, text views and picker views
    @IBOutlet weak var riskTitle: UITextView!
    @IBOutlet weak var consequences: UITextView!

    // ---------------- listener for text view to save input in string when editing is finished -----------------
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.tag == 1 {
            textConsequences = consequences.text
            nameIdentifier = riskTitle.text
            delegate?.transferData(consequencesTranferred: self.textConsequences, identifier: nameIdentifier)
        } else {
            print ("nothing")
        }
    }
}


推荐答案

将对象保存在 UserDefaults 有非常具体的限制:

Saving objects in UserDefaults have very specific restrictions:


set(_:forKey :)参考:

value参数只能是属性列表对象:NSData,NSString,NSNumber ,NSDate,NSArray或NSDictionary。对于NSArray和NSDictionary对象,它们的内容必须是属性列表对象。

The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects.

您需要使用NSCoding或作为替代使用JSON来序列化模型,可以通过 UserDefaults 映射到支持的值。

You need to serialize your model, either using NSCoding or as an alternative using JSON, to map to a supported value by UserDefaults.

这篇关于使用NSUserDefaults与结构类型的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆