gpt4 book ai didi

ios - 在swift中确定数组是否包含另一个数组的一个或多个元素

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:11 25 4
gpt4 key购买 nike

我有以下代码返回用户当前位置附近的地点

import UIKit
import GooglePlaces
import CoreLocation

struct GlobalVariables {
static var acceptedEstablishments = ["bakery", "bar", "cafe", "food", "meal_takeaway", "meal_delivery", "night_club", "restaurant", "school", "university"]
}

class ViewController: UIViewController, CLLocationManagerDelegate {

var placesClient: GMSPlacesClient!
var locationManager: CLLocationManager!

// Add a pair of UILabels in Interface Builder, and connect the outlets to these variables.
@IBOutlet var nameLabel: UILabel!
@IBOutlet var addressLabel: UILabel!


override func viewDidLoad() {
super.viewDidLoad()
placesClient = GMSPlacesClient.shared()

locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}

// Add a UIButton in Interface Builder, and connect the action to this function.
@IBAction func getCurrentPlace(_ sender: UIButton) {

placesClient.currentPlace(callback: { (placeLikelihoodList, error) -> Void in
if let error = error {
print("Pick Place error: \(error.localizedDescription)")
return
}

if let placeLikelihoodList = placeLikelihoodList {
for likelihood in placeLikelihoodList.likelihoods {
let place = likelihood.place
// only return places that are relevant to me
for placeType in place.types {
if (GlobalVariables.acceptedEstablishments.contains(placeType)) {
print("Current place name: \(place.name)")
print("Place type: \(placeType)")
}
}

}
}
})
}
}

place.types 在我底部的回调函数中为每个地点实例返回一个字符串数组,它看起来像这样:

["health", "point_of_interest", "establishment"]

我有一个全局字符串数组,其中还包含 bakerybar 等标签。

当用户按下按钮时,回调函数被触发并根据附近的位置返回位置。

输出看起来像这样:

Current place name: LOCAL SUPERMARKET
Place type: food
Current place name: LOCAL GRILL
Place type: cafe
Current place name: LOCAL GRILL
Place type: food
Current place name: LOCAL SCHOOL
Place type: school
Current place name: LOCAL TAKEAWAY
Place type: meal_takeaway
Current place name: LOCAL TAKEAWAY
Place type: restaurant
Current place name: LOCAL TAKEAWAY
Place type: food

同一个机构被重复多次,因为一个机构有多个与之关联的标签。

例如:

LOCAL TAKEAWAYplace.types 返回数组是:["meal_takeaway", "restaurant", "food"]

并且因为我的 GlobalVariables.acceptedEstablishments 数组包含所有这三个字符串,所以 print 命令将被执行三次。

如果 place.types 数组包含一个或多个匹配的字符串,如何修改这段代码,使其只显示一次建立?我似乎无法找到解决方案。

最佳答案

关键是使用Set,不会有重复项。 Set 是一个集合类,可以像数组一样使用。你可以遍历它,它有countmapfiltercontains

let acceptedPlaces: Set = ["home", "pub", "hospital"]
let availablePlaces: Set = ["home", "pub", "mountains"]
let inBoth = acceptedPlaces.intersection(availablePlaces) // ["home", "pub"]

您可以轻松地从 Arraylet someSet = Set(someArray) 创建 Set,反之亦然 let someArray = 数组(someSet)。您可能还想看看 Set 的以下函数:unionsubstractisSuperSet是子集

关于ios - 在swift中确定数组是否包含另一个数组的一个或多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46669655/

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