gpt4 book ai didi

swift - 如何将 case 语句放入 {}

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

好吧,所以我正在做一个项目,并试图在一切困难的地方让它变得整洁和明显易懂。据我所知,Xcode 没有 Indent Guide,但确实有能力折叠 {} 中的代码,如下所示:

Collapsed Bracket

这适用于任何 {}。我想让我的代码更易于阅读,所以我尝试在一个案例中使用这种效果,这样更容易同时看到所有案例,我这样做了:

switch transition {

case .SlideOutside:
{ /* Error */
print("Pretend that there is hundreds of lines of code here.")
}
break
default: //The Default will be Fade
break
}

但是我得到了错误

Braced block of statements is an unused closure

第 4 行带有左括号。我不知道如何解决这个问题,有人可以帮忙吗。

最佳答案

这句话

and trying to make it neat and visibly understandable

还有这个

Pretend that there is hundreds of lines of code here.

不太兼容:)

方案一

写作

{
/* Error */
print("Pretend that there is hundreds of lines of code here.")
}

您创建了一个闭包但没有运行它。要执行该 block ,您需要添加 (),如下所示

{
/* Error */
print("Pretend that there is hundreds of lines of code here.")
}()

解决方案2

如果您真的要在一个 switch case 中放入数百行代码,我认为您应该更好地构建代码。

首先要做的是将所有代码移到一个函数中,以便仅将函数的调用放在 Switch Case 中。

switch transition {
case .SlideOutside: slideOutInside()
default: fade()
}

接下来,您应该查看包含数百行的函数,并设法将其拆分为子函数(等等)。

关于swift - 如何将 case 语句放入 {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34254801/

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