0

So my table view is not loading anything and I think it's because of this warning that I get. It saids the save function is not being used so how can it load something that is not saved. What I am saving is the indexPath and Section of the row that the user selected via a button action in the row.

Warning:

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

Code:

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

NSCoder Class for DataHandling / ordering.save

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

  }

}

Other code I'm using:

// 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
}

Saving it with:

saveSorting() { "\($0)" }

Loading it in 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 Code:

// 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
}
WokerHead
  • 947
  • 2
  • 15
  • 46
  • 2
    Please always [search on the error](http://stackoverflow.com/search?q=%5Bswift%5D+Result+of+call+to+is+unused) before posting. – rmaddy Jan 30 '17 at 05:54
  • [@discardableResult](https://github.com/apple/swift-evolution/blob/master/proposals/0047-nonvoid-warn.md) can suppress your warning – Inder Kumar Rathore Jan 30 '17 at 06:37
  • Better use `@discardableResult` https://stackoverflow.com/questions/38192751/swift-3-0-result-of-call-is-unused – Hassy May 17 '19 at 09:37

1 Answers1

5

The DataHandling instance's save(defaults:) function technically returns a value, even if you don't use it. To silence this warning, assign it to _ to signify that you don't intend to use the result value, e.g.:

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

or

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

Just to be clear, this is almost definitely not why your tableview is not loading data. It should be pretty insignificant. The indexPath.defaultsKey is being saved (assuming the API works).

Michael Fourre
  • 3,005
  • 3
  • 15
  • 26
  • So am I loading it correctly if its being saved? Any idea on why the table doesn't load? I would appreciate the help. – WokerHead Jan 30 '17 at 06:00
  • It's hard to say since I'm not familiar with this "`DataHandling`" API. Have you thrown in some breakpoints to see where the code is and isn't reaching? I wouldn't be surprised if there are some parse errors considering all of the archiving / unarchiving and coding / decoding logic. It's hard to debug something like this without running the code locally. @BroSimple – Michael Fourre Jan 30 '17 at 06:05
  • I included the DataHandling code if you need it, I could try using breakpoints. I have the project on github if you dont mind helping me out? You can receive the credit for this question and another one I have open regarding the same issue? – WokerHead Jan 30 '17 at 06:09
  • I don't, different questions, other one was a general question around the empty table, this one is about the warning – WokerHead Jan 30 '17 at 06:24
  • If you have the github URL better to post it as an edit to the question itself – Michael Fourre Jan 30 '17 at 06:25
  • You're right I posted it at the bottom, I thought it was better to post the code – WokerHead Jan 30 '17 at 06:28