gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 10:15:38 26 4
gpt4 key购买 nike

所以我的表格 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com