- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在进行简单的转换(例如英制到公制)。我创建了一个包含 (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/
我是一名优秀的程序员,十分优秀!