ios - 委托(delegate)中的可选返回

标签 ios swift delegates protocols

我的单元格内有一个委托(delegate)方法,当我单击单元格内的批准按钮时调用该方法。

现在我希望此委托(delegate)方法具有返回类型 Bool,我将使用它来更新单元格中的 UI。

optional func didTapApproveButton(indexpath: NSIndexPath ) -> Bool

这是委托(delegate)方法。 我想将返回类型保留为可选,因为我将在 VC 的方法内进行 Web 服务调用,并根据响应返回 true 或 false。

如何将返回的 BOOL 保持为可选

编辑

这就是我在VC中使用方法的方式

func didTapApproveButton(indexpath: NSIndexPath) -> Bool? {
        let user = self.dataCtlr.arrayOfUsers[indexpath.row];

        if(Reachability.isConnectedToNetwork()){
            self.dataCtlr.approveRejectUser(true, forUser: user, onSuccess: { (shouldRefreshView) in
                   return true
                }, onFailure: { (result) in

                    return false
            });
        }
        else{

            UserDefaultsManager.sharedInstance.addApproveReject((user.id)!, isApproved: true);
            return true

        }
        return nil
    }

现在 return true 或 false 不会返回到单元格,以便我可以更新单元格。

PS:我已经将协议(protocol)更新为快速实现

 protocol PendingRequestsTableViewCellDelegate{
     func didTapApproveButton(indexpath: NSIndexPath ) -> Bool?
    }

最佳答案

在你的情况下你不能使用 Bool? 因为你的协议(protocol)是@objc。所以你将无法使用像可选值这样的快速特定能力。

我会建议您删除 @obj-cOptional 方法 协议(protocol) CellDelegate { func didTapApproveButton(indexpath: IndexPath ) -> Bool?

关于ios - 委托(delegate)中的可选返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44198438/

相关文章:

ios - 什么情况下JSONSerialization.data(withJSONObject :) throw a catchable error?

Swift 4 推送 viewController 而不是 Present

swift - NSPopover 按钮获得焦点

c# - 如何在 C# 中为委托(delegate)类型的参数提供默认值?

groovy - 使用 .delegate 的 Groovy 闭包中出现问题 "overriding"值

ios - SwiftUI VStack 右对齐单个元素

ios - 如何将 UILabel 添加到 UIToolbar?

ios - 当最小部署目标设置为 iOS 12.2 时,如何有条件地使用 Combine @Published?

ios - 为什么MKMapView的mapView是:viewForOverlay: not called when userlocation is enabled?

ios - 从扬声器获取音频样本