简体   繁体   English

如何快速将 NSIndexPath 添加到 NSUserDefaults

[英]How to add NSIndexPath to NSUserDefaults in swift

I am trying to save NSIndexPath at NSUserDefaults , but got the following exception:-我正在尝试将NSIndexPath保存在NSUserDefaults ,但出现以下异常:-

Attempt to set a non-property-list object {length = 2, path = 0 - 12} as an NSUserDefaults/CFPreferences value for key LastSelectedIndeX尝试将非属性列表对象 {length = 2, path = 0 - 12} 设置为键 LastSelectedIndeX 的 NSUserDefaults/CFPreferences 值

My code:-我的代码:-

let selectedIndexPath = tableView.indexPathForSelectedRow
   NSUserDefaults.standardUserDefaults().setObject(selectedIndexPath, forKey: "lastSelectedIndex")
   NSUserDefaults.standardUserDefaults().synchronize()

So how can I save NSIndexPath so that I can use it later on at another view controller.那么如何保存NSIndexPath以便稍后在另一个视图控制器上使用它。

You can't save directly indexPath in NSUserDefault.你不能在 NSUserDefault 中直接保存 indexPath。 You can Store NSArray , NSDictionary , NSNumber , NSString and NSDictionary in NSUserDefault.您可以在 NSUserDefault 中存储 NSArray 、 NSDictionary 、 NSNumber 、 NSString 和 NSDictionary 。

Store IndexPath using NSKeyedArchiver :使用NSKeyedArchiver存储 IndexPath :

let data = NSKeyedArchiver.archivedDataWithRootObject(selectedIndexPath)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "indexKey")

Get IndexPath using NSKeyedArchiver :使用NSKeyedArchiver获取 IndexPath :

let data1 = NSUserDefaults.standardUserDefaults().objectForKey("indexKey") as? NSData
let indexP1 = NSKeyedUnarchiver.unarchiveObjectWithData(data1!) as! NSIndexPath

As mentioned by the others (NS)IndexPath is not property list compliant.正如其他人所提到的(NS)IndexPath不符合属性列表。

A solution is to use this simple extension of UserDefaults to save the IndexPath as an array of two integers一种解决方案是使用这种简单扩展UserDefaults到保存IndexPath为两个整数的数组

extension UserDefaults {

  func indexPath(forKey key : String) -> IndexPath?
  {
    guard let indexArray = array(forKey: key) as? [Int] else { return nil }
    return IndexPath(row: indexArray[0], section: indexArray[1])
  }

  func set(_ indexPath : IndexPath, forKey key: String)
  {
    set([indexPath.row, indexPath.section], forKey: key)
  }
}

You can find the syntax for an older Swift version in the edit history.您可以在编辑历史记录中找到旧 Swift 版本的语法。

You can do something like this,你可以做这样的事情,

Save like,保存喜欢,

  var myDefaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
    var indexPath: NSIndexPath = self.myTableView.indexPathForSelectedRow
    var data: NSData = NSKeyedArchiver.archivedDataWithRootObject(indexPath)
    myDefaults["lastSelectedIndex"] = myDefaults

Retrieve like,检索喜欢,

    var data2: NSData = myDefaults.dataForKey("lastSelectedIndex")
    var indexPath2: NSIndexPath = NSKeyedUnarchiver.unarchiveObjectWithData(data2)

You have to convert indexpath to data then you can save and retrieve.您必须将 indexpath 转换为数据,然后才能保存和检索。

Hope this will help :)希望这会有所帮助:)

This Swift code would work for any depth of IndexPath此 Swift 代码适用于任何深度的 IndexPath

extension UserDefaults {
    func indexPath(forKey key: String) -> IndexPath? {
        if let data = data(forKey: key), let indexPath = try? JSONDecoder().decode(IndexPath.self, from: data) {
            return indexPath
        }
        return nil
    }
    func set(_ indexPath: IndexPath, forKey key: String) {
        if let data = try? JSONEncoder().encode(indexPath) {
            set(data, forKey: key)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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