ios - Swift 编译器警告 : Result of call to 'save(defaults:)' is unused

标签 ios swift warnings nsuserdefaults nscoding

所以我的表格 View 没有加载任何东西,我认为这是因为我收到了这个警告。它说保存功能没有被使用,所以它怎么能加载没有保存的东西。我要保存的是用户通过行中的按钮操作选择的行的 indexPath 和 Section。

警告:

Result of call to 'save(defaults:)' is unused



代码:
func saveSorting(_ dataIdBlock: (Any) -> String) {

    guard let items = self.items else { return }

    for (section, rows) in items.enumerated() {
        for (row, item) in rows.enumerated() {
            let indexPath = IndexPath(row: row, section: section)
            let dataId = dataIdBlock(item)
            let ordering = DataHandling(dataId: dataId, indexPath: indexPath)

            // Warning is here
            ordering.save(defaults: indexPath.defaultsKey)
            }
        }
    }
}

DataHandling/ordering.save 的 NSCoder 类
DataHandling.swift

class DataHandling: NSObject, NSCoding {

var indexPath: IndexPath?
var dataId: String?

init(dataId: String, indexPath: IndexPath) {
    super.init()
    self.dataId = dataId
    self.indexPath = indexPath
}

required init(coder aDecoder: NSCoder) {

    if let dataId = aDecoder.decodeObject(forKey: "dataId") as? String {
        self.dataId = dataId
    }

    if let indexPath = aDecoder.decodeObject(forKey: "indexPath") as? IndexPath {
        self.indexPath = indexPath
    }

}

func encode(with aCoder: NSCoder) {
    aCoder.encode(dataId, forKey: "dataId")
    aCoder.encode(indexPath, forKey: "indexPath")
}

func save(defaults box: String) -> Bool {

    let defaults = UserDefaults.standard
    let savedData = NSKeyedArchiver.archivedData(withRootObject: self)
    defaults.set(savedData, forKey: box)
    return defaults.synchronize()

}

convenience init?(defaults box: String) {

    let defaults = UserDefaults.standard
    if let data = defaults.object(forKey: box) as? Data,
        let obj = NSKeyedUnarchiver.unarchiveObject(with: data) as? DataHandling,
        let dataId = obj.dataId,
        let indexPath = obj.indexPath {
        self.init(dataId: dataId, indexPath: indexPath)
    } else {
        return nil
    }

}

class func allSavedOrdering(_ maxRows: Int) -> [Int: [DataHandling]] {

    var result: [Int: [DataHandling]] = [:]
    for section in 0...1 {
        var rows: [DataHandling] = []
        for row in 0..<maxRows {
            let indexPath = IndexPath(row: row, section: section)
            if let ordering = DataHandling(defaults: indexPath.defaultsKey) {
                rows.append(ordering)
            }
            rows.sort(by: { $0.indexPath! < $1.indexPath! })
        }
        result[section] = rows
    }

    return result

  }

}

我正在使用的其他代码:
// Number of Rows in Section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.items?[section].count ?? 0
}

// Number of Sections
func numberOfSections(in tableView: UITableView) -> Int {

    return self.items?.count ?? 0
}

保存它:
saveSorting() { "\($0)" }

在 ViewDidLoad 中加载它:
func fetchData() {

    // Load Data from Server to testArray
    retrieveData()

    // request from remote or local
    data = [testArray]

    // Update the items to first section has 0 elements,
    // and place all data in section 1
    items = [[], data ?? []]

    // apply ordering
    applySorting() { "\($0)" }

    // save ordering
    saveSorting() { "\($0)" }

    // refresh the table view
    myTableView.reloadData()
}

加载代码:
// Loading
func applySorting(_ dataIdBlock: (Any) -> String) {

    // get all saved ordering
    guard let data = self.data else { return }
    let ordering = DataHandling.allSavedOrdering(data.count)

    var result: [[Any]] = [[], []]

    for (section, ordering) in ordering {
        guard section <= 1 else { continue } // make sure the section is 0 or 1
        let rows = data.filter({ obj -> Bool in
            return ordering.index(where: { $0.dataId == .some(dataIdBlock(obj)) }) != nil
        })
        result[section] = rows
    }

    self.items = result
}

最佳答案

DataHandling实例的save(defaults:)函数在技术上返回一个值,即使你不使用它。要使此警告静音,请将其分配给 _表示您不打算使用结果值,例如:

_ = ordering.save(defaults: indexPath.defaultsKey)

或者
let _ = ordering.save(defaults: indexPath.defaultsKey)

需要明确的是,这几乎绝对不是您的 tableview 不加载数据的原因。它应该是微不足道的。 indexPath.defaultsKey正在保存(假设 API 有效)。

关于ios - Swift 编译器警告 : Result of call to 'save(defaults:)' is unused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41929744/

相关文章:

ios - 如何在 TableViewController 中使用 indexPathForItemAtPoint?

ios - 快速制作播放列表(开始下一首歌曲)

ios - Realm Swift - 保存对另一个对象的引用

ios - 通过数组 Firebase 实现 TableView 图像异步

ios - 将 CALayer 移动到另一个 View 或更改 CALayer.contents,哪个更有效?

swift - Swift中didSet和willSet是如何实现的

iOS/Swift : Imported . 框架不完整或显示错误类

c - 如何在 C90 中分配可变大小的数组?

javac 和 scalac 警告仅针对特定类型的错误

iphone - Xcode - 警告问题