man*_*112 0 closures ios completionhandler swift
我正在尝试重构我的代码,并想在中返回Bool一个closure。当我尝试它时,它说它是未使用的并且不起作用。我可以用另一种方式来做,但是我要重复不想做的代码。我该怎么办。
func tableView(_ pTableView: UITableView, canEditRowAt pIndexPath: IndexPath) -> Bool {
// These lines are the one that work but would like to get rid of them
if let rowConversation = self.objectAtIndexPath(pIndexPath) as? Conversation {
if rowConversation.isGroupChat && rowConversation.expired {
return true
}
}
self.getRowConversation(pIndexPath: pIndexPath) {
// how to return true here
}
return false
}
private func getRowConversation(pIndexPath: IndexPath, completion pCompletion: () -> Void) {
if let rowConversation = self.objectAtIndexPath(pIndexPath) as? Conversation {
if rowConversation.isGroupChat && rowConversation.expired {
ConversationManager.shared.deleteConversationID(rowConversation.conversationID)
pCompletion()
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可能对此考虑过度。这里不需要“关闭”;不需要“完成处理程序”。没有异步发生。只需将其getRowConversation转换为返回Bool的普通函数即可;调用它,并将结果传回给您。
private func getRowConversation(pIndexPath: IndexPath) -> Bool {
if let rowConversation = self.objectAtIndexPath(pIndexPath) as? Conversation {
if rowConversation.isGroupChat && rowConversation.expired {
ConversationManager.shared.deleteConversationID(rowConversation.conversationID)
return true
}
}
return false
}
Run Code Online (Sandbox Code Playgroud)
并这样称呼它:
func tableView(_ pTableView: UITableView, canEditRowAt pIndexPath: IndexPath) -> Bool {
return self.getRowConversation(pIndexPath: pIndexPath)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
352 次 |
| 最近记录: |