[英]Why can't I use min() on a tuple of doubles in Swift?
var coordinatesDouble = (Double(latitudeTextField.text!), Double(longitudeTextField.text!))
var boundingBoxLat: (Double, Double)
var boundingBoxLon: (Double, Double)
boundingBoxLat = (coordinatesDouble.0! - Constants.Flickr.SearchBBoxHalfHeight, coordinatesDouble.1! + Constants.Flickr.SearchBBoxHalfHeight)
boundingBoxLon = (coordinatesDouble.1! - Constants.Flickr.SearchBBoxHalfWidth, coordinatesDouble.1! + Constants.Flickr.SearchBBoxHalfWidth)
if min(boundingBoxLat) < -90.0 {
I am trying to use min() in an if statement, but am getting the error: 我正在尝试在if语句中使用min(),但出现错误:
"Cannot invoke 'min' with an argument list of type (Double, Double)"
“无法使用类型(双精度,双精度)的参数列表调用'min'”
I thought this was specifically what this function was for. 我认为这正是该功能的目的。 Am I missing something?
我想念什么吗?
The equation for each double is one double being subtracted or added to another, resulting in a double. 每个双精度型的方程是一个双精度型被减去或加到另一个,从而得到一个双精度型。 The if statement should then be determining if the lowest value in the tuple is lower than -90.0.
然后,if语句应确定元组中的最小值是否低于-90.0。
Am I missing something? 我想念什么吗?
YES. 是。
Please check this: SE-0029 Remove implicit tuple splat behavior from function applications 请检查以下内容: SE-0029从函数应用程序中删除隐式元组splat行为
In the versions of Swift which include this SE-0029, Swift does not accept a tuple of two members as an actual parameter for function with two arguments . 在包含此SE-0029的Swift版本中,Swift不接受两个成员的元组作为带有两个参数的函数的实际参数。
(There is another overload for min
, but it's irrelevant here.) (
min
还有另一个重载,但是在这里无关紧要。)
As commented by JAL, you need to pass two arguments to call min(_:_:)
: 正如JAL所说,您需要传递两个参数来调用
min(_:_:)
:
min(boundingBoxLat.0, boundingBoxLat.1)
Some addition. 一些补充。
I wonder if this line is really what you intend: 我想知道这行是否真的是您想要的:
boundingBoxLat = (coordinatesDouble.0! - Constants.Flickr.SearchBBoxHalfHeight, coordinatesDouble.1! + Constants.Flickr.SearchBBoxHalfHeight)
It should be like this, no? 应该是这样,不是吗? (Please find the small difference in the latter half.):
(请查找后半部分的细微差别。):
boundingBoxLat = (coordinatesDouble.0! - Constants.Flickr.SearchBBoxHalfHeight, coordinatesDouble.0! + Constants.Flickr.SearchBBoxHalfHeight)
But it is not important my guess is right or not. 但是,我的猜测是否正确并不重要。 Just you should know that this sort of code using tuples too much is less readable.
只有您应该知道,过多使用元组的这种代码不太可读。
I recommend you to define your own structure to represent boundingBox : 我建议您定义自己的结构来表示boundingBox :
struct CoordinateBox {
var minLatitude: Double
var maxLatitude: Double
var minLongitude: Double
var maxLongitude: Double
init(latitude: Double, longitude: Double, height: Double, width: Double) {
assert(height > 0.0)
assert(width > 0.0)
minLatitude = latitude - height
maxLatitude = latitude + height
minLongitude = longitude - width
maxLongitude = longitude + width
}
}
let boundingBox = CoordinateBox(latitude: Double(latitudeTextField.text ?? "") ?? 0,
longitude: Double(longitudeTextField.text ?? "") ?? 0,
height: Constants.Flickr.SearchBBoxHalfHeight,
width: Constants.Flickr.SearchBBoxHalfWidth)
if boundingBox.minLatitude < -90.0 {
//...
}
You would never be confused with .0
s and .1
s. 您将不会与
.0
s和.1
s混淆。
A tuple can have different data types mixed together. 元组可以将不同的数据类型混合在一起。 You should use an array if all the datatypes are the same.
如果所有数据类型都相同,则应使用数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.