gpt4 book ai didi

swift - 协议(protocol)扩展中的变异函数,其中 Self 是 UIViewController

转载 作者:搜寻专家 更新时间:2023-11-01 05:36:53 24 4
gpt4 key购买 nike

我写了一个协议(protocol)和相应的扩展,它使用一个简单的 StringStack"<origin>@<destination>" 形式的命名约定相结合在我的 Swift 应用程序中的嵌套 View 之间执行 segues。我是 swift 的新手,所以如果有更好的方法来实现跨多个 View 的前进和后退按钮,我愿意接受。但是,由于我在下面详述的原因,以下代码存在问题。

struct StringStack {
var stack: Array = [String]()

mutating func push(str: String)->Void {
self.stack.append(str)
}

mutating func pop()->String {
self.stack.popLast()
}
}

protocol SegueForwardAndBackward {
var thisIdentifier: String {get set}
var segueStack: StringStack {get set}
}

extension SegueForwardAndBackward where Self: UIViewController {

mutating func performForwardSegue(nextIdentifier: String) {
let identifier: String = thisIdentifier + "@" + nextIdentifier
segueStack.push(identifier)
performSegueWithIdentifier(identifier, sender: self)
}

mutating func performBackwardSegue() {
let divided = segueStack.pop().componentsSeparatedByString("@")
// reverses destination and origin of identifier
let segueIdentifier: String = divided[1] + "@" + divided[0]
performSegueWithIdentifier(segueIdentifier, sender: self)
}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if var destinationViewController: SegueForwardAndBackward = segue.destinationViewController as? SegueForwardAndBackward {
destinationViewController.segueStack = segueStack
}
}
}

我最初的问题是协议(protocol)扩展中不允许使用变异函数,详见 bug report .该报告中给出了一个建议的解决方法,但是以下导致错误声称 'SegueForwardAndBackward' is not a subtype of 'UIViewController' .

class A: UIViewController, SegueForwardAndBackward {

mutating func testFunc() {
var p: SegueForwardAndBackward = self
p.performBackwardSegue()
}
}

我确实遇到了一个潜在的解决方案,我可以在其中制作 SegueForwardAndBackward another question 中详述的类协议(protocol).但是,实现类协议(protocol)会导致段错误,我认为这是因为我将我的扩展声明为 where Self: UIViewController。 ,不幸的是,我不知道如何解决这个问题。

目前,我对 Swift 中的另一个错误感到非常沮丧,因此,如果您能帮助我改进我的方法或破译这些错误,我们将不胜感激。

预先感谢您的回复!

最佳答案

question似乎解决了嵌套前进和后退按钮的替代解决方案,因此在没有任何其他响应的情况下,这就是我要解决的问题。然而,我提出的问题仍然是 swift 中一个非常奇怪的错误。

关于swift - 协议(protocol)扩展中的变异函数,其中 Self 是 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36489853/

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