gpt4 book ai didi

swift 语法错误?

转载 作者:行者123 更新时间:2023-11-28 05:34:28 25 4
gpt4 key购买 nike

为什么我得到一个[AnyObject]!此处无法转换为“Void” 错误?我只是没有看到一些小的语法错误还是什么??

    var popClosure = {()->Void in presentingController!.popToViewController(dest!, animated: false)}

在此方法中只调用一次:

src!.dismissViewControllerAnimated(true, completion: popClosure)

完整代码如下:

class ColorfulUnwindSegue : UIStoryboardSegue
{
private let ANIMATION_DURATION = 0.5

override func perform()
{
var dest = self.destinationViewController as? UIViewController
var src = self.sourceViewController as? UIViewController

if src && dest
{
dest!.view.frame = CGRectMake(-dest!.view.frame.size.width, 0, dest!.view.frame.size.width, dest!.view.frame.size.height)

var srcIsModal = false

var presentingController = src!.navigationController as? ColorfulNavigationController

if src!.presentingViewController as? ColorfulNavigationController
{
srcIsModal = true
presentingController = src!.presentingViewController as? ColorfulNavigationController
}

if presentingController
{
if srcIsModal
{
var popClosure : ()->Void = {()->Void in presentingController!.popToViewController(dest!, animated: false)}

src!.dismissViewControllerAnimated(true, completion: popClosure)
}
else
{
src!.view.layer.addSublayer(dest!.view.layer)

UIView.animateWithDuration(ANIMATION_DURATION,
animations:
{()->Void in
src!.view.frame = CGRectMake(dest!.view.frame.size.width, 0, src!.view.frame.size.width, src!.view.frame.size.height)
},
completion:
{(finished:Bool)->Void in
if finished
{
presentingController!.popToViewController(dest!, animated: false)
}
})
}
}
}
}
}

最佳答案

似乎因为您在闭包中只有一个方法调用,所以它试图将该方法存储在 popClosure 中。

 func popToViewController(viewController: UIViewController!, animated: Bool) -> [AnyObject]!

因为一个方法返回 [AnyObject]!,popClosure 返回 [AnyObject]! 与其声明的类型 () ->无效

您可以通过在闭包中添加另一行来解决这个问题。它不需要做任何事情。正如 rickster 所建议的,在这个例子中我只是在闭包中添加了一个 return。

var popClosure: () -> Void = { () -> Void in
presentingController!.popToViewController(dest!, animated: false)
return
}

然后编译器似乎理解您正在定义一个调用该方法的闭包。

关于 swift 语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25129223/

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