2017-01-30 27 views
0

所以我的表視圖沒有加載任何東西,我認爲這是因爲我得到這個警告。它說save函數沒有被使用,所以它如何加載一些沒有保存的東西。我保存的是用戶通過行中的按鈕操作選擇的行的indexPath和Section部分。Swift編譯器警告:調用'save(defaults :)'的結果未被使用

警告:

調用的結果 '保存(默認:)' 是未使用的

代碼:

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班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 

    } 

} 

其他代碼我使用:

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

在viewDidLoad中加載它:

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

與保存它

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 
} 
+2

在發佈之前,請務必[在錯誤上搜索](http://stackoverflow.com/search?q=%5Bswift%5D+Result+of+call+to+is+unused)。 – rmaddy

+0

[@discardableResult](https://github.com/apple/swift-evolution/blob/master/proposals/0047-nonvoid-warn.md)可以壓制你的警告 –

回答

4

DataHandling研究所ance的save(defaults:)函數在技術上會返回一個值,即使您不使用它。要消除此警告,將其分配給_,以表示你不打算使用的結果值,例如:

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

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

只是要清楚,這幾乎是肯定不會爲什麼你的tableview沒有加載數據。它應該是非常微不足道的。 indexPath.defaultsKey正在保存(假設API工作)。

+0

所以我加載它正確,如果它被保存?任何想法爲什麼表不加載?我將不勝感激。 – BroSimple

+0

很難說,因爲我不熟悉這個「DataHandling」API。你有沒有拋出一些斷點來看代碼在哪裏,並沒有達到?考慮到所有的歸檔/解碼和編碼/解碼邏輯,如果存在一些解析錯誤,我不會感到驚訝。沒有在本地運行代碼就很難調試這樣的東西。 @BroSimple –

+0

如果你需要它,我包含DataHandling代碼,我可以嘗試使用斷點。如果你不介意幫助我,我在github上有這個項目嗎?你可以收到這個問題的信用和另一個我已經打開關於同一問題? – BroSimple