gpt4 book ai didi

arrays - 闭包数组简写 : Xcode Swift: Expression too complex to be solved in a reasonable time

转载 作者:行者123 更新时间:2023-11-28 12:39:11 26 4
gpt4 key购买 nike

我正在进行简单的转换(例如英制到公制)。我创建了一个包含 (String, Closure) 元素的元组数组。这让我可以将一个字符串用于转换(例如“英里到公里”到选择器控件中,并在一行中引用关联的公式,传递要转换的值。我遇到的问题是当我使用 $0闭包速记。我收到错误“表达式太复杂,无法在合理的时间内解决。”

这是有效的声明和代码:

在初始类语句下面用 ViewController 属性声明:

var formulaTuple = [(convString: String, convFormula: ((Double) -> Double))]()

在 viewDidLoad() 中赋值

    formulaTuple = [("miles to kilometers", {(a: Double) -> Double in return (a / 0.62137) }),
("kilometers to miles", {(a: Double) -> Double in return (a * 0.62137) }),
("feet to meters", {(a: Double) -> Double in return (a / 3.2808) }),
("yards to meters", {(a: Double) -> Double in return (a / 1.0936) }),
("meters to feet", {(a: Double) -> Double in return (a * 3.2808) }),
("meters to yards", {(a: Double) -> Double in return (a * 1.0936) }),
("inches to centimeters", {(a: Double) -> Double in return (a / 0.39370) }),
("centimeters to inches", {(a: Double) -> Double in return (a * 0.39370) }),
("fahrenheit to celsius", {(a: Double) -> Double in return ((a - 32) * (5/9)) }),
("celsius to fahrenheit", {(a: Double) -> Double in return (a * (9/5) + 32) }),
("quarts to liters", {(a: Double) -> Double in return (a / 1.05669) }),
("liters to quarts", {(a: Double) -> Double in return (a * 1.05669) }) ]

代码中的工作调用,其中 row 是选择器中被点击的行,inputValue 是传递给要转换的内容,outputValue 是转换的结果。

outputValue = formulaTuple[row].convFormula(inputValue)

当我尝试在 viewDidLoad() 中使用此语法而不是上面的语法来精简声明时出现问题:

    formulaTuple = [("miles to kilometers", {$0 / 0.62137 }),
("kilometers to miles", {$0 * 0.62137 }),
("feet to meters", {$0 / 3.2808 }),
("yards to meters", {$0 / 1.0936 }),
("meters to feet", {$0 * 3.2808 }),
("meters to yards", {$0 * 1.0936 }),
("inches to centimeters", {$0 / 0.39370}),
("centimeters to inches", {$0 * 0.39370 }),
("fahrenheit to celsius", {($0 - 32) * (5/9) }),
("celsius to fahrenheit", {$0 * (9/5) + 32 }),
("quarts to liters", {$0 / 1.05669 }),
("liters to quarts", {$0 * 1.05669 }) ]

我原以为这样会更流畅,但它似乎破坏了 Xcode。想法?我的方法从根本上来说是不合理的,会推荐一种不同的方法吗?谢谢!

最佳答案

当你给 Swift 大数组字面量然后期望它解释类型时,Swift 表现不佳。在你的情况下,我希望它能工作,因为你的 formulaTuple 属性已经有一个确定的类型。

作为解决方法,您可以先初始化一个常量数组,然后将其分配给您的属性:

let temp: [(String, (Double) -> Double)] = [
("miles to kilometers", {$0 / 0.62137 }),
("kilometers to miles", {$0 * 0.62137 }),
("feet to meters", {$0 / 3.2808 }),
("yards to meters", {$0 / 1.0936 }),
("meters to feet", {$0 * 3.2808 }),
("meters to yards", {$0 * 1.0936 }),
("inches to centimeters", {$0 / 0.39370}),
("centimeters to inches", {$0 * 0.39370 }),
("fahrenheit to celsius", {($0 - 32) * (5/9) }),
("celsius to fahrenheit", {$0 * (9/5) + 32 }),
("quarts to liters", {$0 / 1.05669 }),
("liters to quarts", {$0 * 1.05669 })
]

formulaTuple = temp

替代答案

元组实际上用于临时存储并从一个函数传回多个结果。您可能需要考虑使用 struct 作为数组值的类型:

struct Conversion {
let string: String
let formula: (Double) -> Double
}

var conversions = [Conversion]()

conversions = [
Conversion(string: "miles to kilometers", formula: {$0 / 0.62137 }),
Conversion(string: "kilometers to miles", formula: {$0 * 0.62137 }),
Conversion(string: "feet to meters", formula: {$0 / 3.2808 }),
Conversion(string: "yards to meters", formula: {$0 / 1.0936 }),
Conversion(string: "meters to feet", formula: {$0 * 3.2808 }),
Conversion(string: "meters to yards", formula: {$0 * 1.0936 }),
Conversion(string: "inches to centimeters", formula: {$0 / 0.39370}),
Conversion(string: "centimeters to inches", formula: {$0 * 0.39370 }),
Conversion(string: "fahrenheit to celsius", formula: {($0 - 32) * (5/9) }),
Conversion(string: "celsius to fahrenheit", formula: {$0 * (9/5) + 32 }),
Conversion(string: "quarts to liters", formula: {$0 / 1.05669 }),
Conversion(string: "liters to quarts", formula: {$0 * 1.05669 })
]

outputValue = conversions[row].formula(inputValue)

Swift 对此更满意,temp 解决方法不是必需的。

关于arrays - 闭包数组简写 : Xcode Swift: Expression too complex to be solved in a reasonable time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39946729/

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