gpt4 book ai didi

ios - 如何在 Swift 中对两种类型进行协议(protocol)扩展约束

转载 作者:搜寻专家 更新时间:2023-10-31 08:05:00 24 4
gpt4 key购买 nike

我有一个协议(protocol)及其相应的扩展,看起来像这样:

import Foundation


protocol HelpActionManageable {
typealias ItemType : UIViewController,HelpViewControllerDelegate
var viewController : ItemType {
get
}
}

extension HelpActionManageable {
func presentHelpViewController() {
let helpViewController = HelpViewController(nibName: HelpViewController.nibName(), bundle: nil)
viewController.presentViewController(helpViewController, animated: true, completion:nil)
helpViewController.delegate = viewController
}
func dismissSuccessfulHelpViewController(helpViewController:HelpViewController) {
helpViewController.dismissViewControllerAnimated(true) { () -> Void in
self.viewController.showAlertControllerWithTitle(GlobalConstants.Strings.SUCCESS, message: GlobalConstants.Strings.VALUABLE_FEEDBACK, actions: [], dismissingActionTitle: GlobalConstants.Strings.OK, dismissBlock: nil)
}
}
}

因此,在确认此协议(protocol)的随机 View Controller 中,我正在做这样的事情:

class RandomViewController : UIViewController, HelpViewControllerDelegate,HelpActionManageable {
//HelpViewControllerDelegate methods...
var viewController : RandomViewController {
return self
}
}

这工作正常,但如果扩展 HelpActionManageable 方法仅适用于确认 UIViewControllerHelpViewControllerDelegate 的类型,那将非常整洁.

是这样的:

extension HelpActionManageable where Self == ItemType

这行不通。我如何在 Swift 中实现这一目标?

最佳答案

我认为现在支持了。

extension HelpActionManageable where Self == ItemType  {
}

extension HelpActionManageable where Self: ItemType  {
}

对我有用。

关于ios - 如何在 Swift 中对两种类型进行协议(protocol)扩展约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34368278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com