Swift 快速检查两个对象是否具有相同的值
我有一个基于位置的应用程序,我有两种类型的帖子,Swift 快速检查两个对象是否具有相同的值,swift,firebase,Swift,Firebase,我有一个基于位置的应用程序,我有两种类型的帖子,Fixed和Dynamic我正在尝试设置它,这样如果Dynamic Post和Fixed Post位于同一位置,它就不会显示Dynamic Post,而只显示Fixed Post。我只是不明白背后的逻辑。。。谢谢你的帮助 以下是我能得到的信息: let ref = Database.database().reference().child("posts") ref.observeSingleEvent(of: .value) { (snap
Fixed
和Dynamic
我正在尝试设置它,这样如果Dynamic Post
和Fixed Post
位于同一位置,它就不会显示Dynamic Post
,而只显示Fixed Post
。我只是不明白背后的逻辑。。。谢谢你的帮助
以下是我能得到的信息:
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value) { (snapshot) in
guard let dictionary = snapshot.value as? [String: Any] else { return }
dictionary.forEach { (key, value) in
guard let dict = value as? [String: Any] else { return }
guard let type = dict["type"] as? String else { return }
guard let latitudeString = dictionary["latitude"] as? String else { return }
guard let longitudeString = dictionary["longitude"] as? String else { return }
let latitude = (latitudeString as NSString).doubleValue
let longitude = (longitudeString as NSString).doubleValue
var dynamicEvents = [Post]()
Database.fetchPost(postId: key) { (post) in
if post.type == "dynamic" && post.latitude == post.latitude && post.longitude == post.longitude {
let coords = [CLLocation(latitude: latitude, longitude: longitude)]
DispatchQueue.main.async {
self.addAnnotation(post: post, title: post.category, subtitle: nil, coords: coords)
}
} else {
return
}
}
}
}
我认为您只需要创建一个扩展来比较这些对象。
或者,您可以使这两个对象从同时包含“纬度”和“经度”的对象继承。通过这种方式,您可以实现equalable协议来比较它们是否相同。您的逻辑是检查相同的变量是否等于相同的变量: post.latitude==post.latitude&&post.longitude==post.longitude 我想你的意思是
post.latitude==latitude&&post.longitude==longitude您甚至没有尝试比较两个post,您的代码所做的只是不必要地检查post的lat/lon是否等于它们自己。@Gereon这就是我提出这个问题的原因。我不确定它为什么不起作用