gpt4 book ai didi

swift - 理解函数算法

转载 作者:行者123 更新时间:2023-11-30 10:19:43 25 4
gpt4 key购买 nike

在学习教程期间,我使用函数来显示可见矩形,其中包含所有注释(在 map View 上),如下所示:

// 1
let rectToDisplay = self.treasures.reduce(MKMapRectNull) { (mapRect: MKMapRect, treasure: Treasure) -> MKMapRect in
// 2
let treasurePointRect =
MKMapRect(origin: treasure.location.mapPoint,
size: MKMapSize(width: 0, height: 0))
return MKMapRectUnion(mapRect, treasurePointRect)
// 3
}
// 4
self.mapView.setVisibleMapRect(rectToDisplay, edgePadding: UIEdgeInsetsMake(74, 10, 10, 10), animated: false)

一切正常,但我不明白它到底是如何工作的。

在第三条车道上,我们正在执行以下操作:

return MKMapRectUnion(mapRect, treasurePointRect)

在此之前,我们声明

mapRect: MKMapRect

因此,mapRect 没有任何初始值,并且假设不包含任何值。我对吗?

如果 mapRect 的值为零,那么 MKMapRectUnion 到底是如何计算的?有什么方法可以使用某种 NSLog 语句查看函数的每一步吗?

如果您好心,请详细解释一下该功能是如何工作的。据我了解,函数尝试使 map 矩形结合“零”值 map 点和其他 map 点,并具有正确的值。

最佳答案

你已经正确理解了一切。至于你的问题

How exactly MKMapRectUnion calculated, if mapRect have zero values?

苹果文档说:

If either rectangle is null, this method returns the other rectangle. The origin point of the returned rectangle is set to the smaller of the x and y values for the two rectangles. Similarly, the size and width of the rectangle are computed by taking the maximum x and y values and subtracting the x and y values for the new origin point.

具体是如何计算的,你可以询问苹果工程师或对mapkit进行逆向工程。如果该函数确实工作正常,那么您就不必花这么长的时间来偏离您的实际任务。

关于swift - 理解函数算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27729292/

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