gpt4 book ai didi

swift - 在一个(if)语句中一次检查许多字符串

转载 作者:行者123 更新时间:2023-12-02 10:52:56 25 4
gpt4 key购买 nike

我正在尝试检查10个字符串是否为空以运行我的脚本。我如何合理地将其分解为一个功能?我的脚本依赖于所有这些内容,而且相当长,因此我无法逐一询问每个脚本。它可以使用6个或更少的名称。但是从名称7到10,构建应用程序时出现错误。

if (name[1] != "") && (name2[1] != "") && (name3[1] != "") && (name4[1] != "") && (name5[1] != "") && (name6[1] != "") && (name7[1] != "") && (name8[1] != "") && (name9[1] != "") && (name10[1] != "")

我收到此错误:

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

最佳答案

像这样对变量名进行编号是一个很大的危险信号。它们应该在数组中。

您要查找的是 Sequence.allSatisfy(_:) ,如果序列中的所有成员都满足提供的谓词,则返回true:

let names = [name[1], name1[1], name2[1], ... name10[1]] // this is madness, get rid of these numbered variables

let allNamesArentEmpty = names.allSatisfy { !$0.isEmpty }
if allNamesArentEmpty { ... }

关于swift - 在一个(if)语句中一次检查许多字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61462566/

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