2017-07-03 17 views
0

我目前正在爲IOS創建一個框架。我的框架必須發送請求到我的服務器,解析響應,然後更新應用程序主頁面的UITableView。 我已經創建了一個Singleton類「MyUpdater」,它有一個靜態func「addRow」 在我的應用程序中,我有2個對象:tableView和我用於tableView的對象列表。 我希望(我的FrameWork)的「addRow」方法異步地將對象添加到應用程序的對象列表中。 所以我創建了以下方法:異步函數中的Swift(IOS)更新參數

public static func addRow(tableView: UITableView, list: inout Array<Product>){ 
var my_object = Object() 
list.insert(my_object, at: 1) 
tableView.beginUpdates() 
tableView.insertRows(at: [IndexPath(row: 1, section: 0)], with: .automatic) 
tableView.reloadData() 
tableView.enUpdates()} 

,我得到了以下錯誤:封逃逸只能捕捉INOUT。

我該如何異步更新從pod(框架)的應用程序列表? 謝謝。 親切的問候

+0

不相關,但不要在'insertRows'之後調用'reloadData'。插入操作更新用戶界面,你將不會得到動畫。而'begin-/endUpdates'對單個插入/刪除/移動操作沒有影響。 – vadian

+0

好的,謝謝你 –

回答

0

爲什麼使用inout?我認爲這在這裏沒有意義,因爲inout不是通過參考。

你試過類似的東西嗎?

public func addRow(tableView: UITableView, list: Array<Product>){ 
    Dispatch.main.async { 
     //update the list + tableview 
    } 
} 
+0

當然可以。但是,當我插入一個值到列表中,我得到一個錯誤:「不能對不可變值使用變異成員:」products「是一個」let「常量 –