gpt4 book ai didi

ios - Swift中使用数组简化代码(核心位置)

转载 作者:搜寻专家 更新时间:2023-11-01 06:19:21 24 4
gpt4 key购买 nike

我正在做一个小项目:如果人在给定坐标附近,它会输出“你好”。它正在工作,但我认为如果我继续制作这么多变量,代码太多了。有没有办法组织它或使其更容易添加坐标。我尝试使用数组但无法正常工作。什么可能是最好的解决方案?我对 Swift 语法非常陌生。非常感谢任何帮助。

let addressLocation:CLLocation = CLLocation(latitude: 37.334421, longitude: -122.037987)
let addressLocation2:CLLocation = CLLocation(latitude: 37.334395, longitude: -122.041173)


let meters:CLLocationDistance = location!.distanceFromLocation(addressLocation)
let meters2:CLLocationDistance = location!.distanceFromLocation(addressLocation2)


if (meters <= 50.00000000 || meters2 <= 50.0000000){
print("Hello")
}else{
print("No")
}

最佳答案

这就是您将如何使用数组来保存位置。

var location = CLLocation(latitude: 37.334421, longitude: -122.037987)
var locations:[CLLocation] = [CLLocation(latitude: 37.334421, longitude: -122.037987),
CLLocation(latitude: 37.334395, longitude: -122.041173)]
var distances = locations.map{location.distanceFromLocation($0)}
let maxDistance = 50.0
if (distances.contains{$0 < maxDistance}) {
print("Hello")
} else {
print("No")
}

关于ios - Swift中使用数组简化代码(核心位置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37159103/

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