ios - 将 NSUserDefaults 与结构类型的数组一起使用

标签 ios arrays swift struct nsuserdefaults

我在弄清楚如何使用 NSUserDefaults 保存类型为“RiskEntry”的字符串时遇到问题。我已经浏览了一些其他帖子,但不知何故我仍然无法解决这个特定问题。

让我解释一下下面的代码现在做了什么:我在下面的代码片段中从我的类 CustomCell 中获取了一些数据。在这里,我首先使用“标识符”检查要使用新数组值“结果”更新哪个数组。

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

知道我在这里做错了什么吗?

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

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")
        }
    }
}

最佳答案

问题是您无法将自定义数组保存在 NSUserDefaults 中。为此,您应该将它们更改为 NSData,然后将其保存在 NSUserDefaults 中 这是我在项目中使用的代码,它采用 swift 2 语法,我认为将其转换为 swift 3 并不难

let data = NSKeyedArchiver.archivedDataWithRootObject(yourObject);
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "yourKey")
NSUserDefaults.standardUserDefaults().synchronize()

对于获取部分使用这个组合

if let data = NSUserDefaults.standardUserDefaults().objectForKey("yourKey") as? NSData {
    let myItem = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? yourType
}

希望对你有帮助

关于ios - 将 NSUserDefaults 与结构类型的数组一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40062835/

相关文章:

ios - 检测对已激活的 UITextField 的点击

ios - 栏按钮项目无法执行操作 Swift

python - 使用 numpy 数组将值分配给另一个数组

xcode - 使用#selector 而不是显式构造选择器

ios - 如何在 Today 扩展中调整 UITableView 的大小

ios - Swift 中的弹出 View

ios - 将 JSON 解析为 TableView

ios - 让 UITextField 更改 Webview URL

c - 在函数 C 中返回 "char *tab[]"

ios - 如何在 UITableView 中显示数组中的 x 个对象,然后在向下滚动时显示更多对象?