Reputation: 121
I have a custom object array of type LeaderboardUser()
. This object contains a string variable of "uid", "email".
I am trying to make a statement to recognize if the uid given matches the uid of the LeaderBoard
if self.allFriends.contains() {
// Remove user
Database.database().reference().child("Users").child(Auth.auth().currentUser!.uid).child("Friends").child(self.uid).removeValue()
} else {
// Add user
let key = Database.database().reference().child("Users").childByAutoId().key
let friends = ["uid" : "\(self.uid)"] as [String : Any]
let totalList = ["\(key!)" : friends]
Database.database().reference().child("Users").child(Auth.auth().currentUser!.uid).child("Friends").updateChildValues(totalList)
}
Is there any way that I can search the UID parameter? I tried a contains() bool, but I don't know what and how how that works.
Upvotes: 0
Views: 2014
Reputation: 589
Param of contents func is a closure that takes an element of the sequence as its argument and returns a Boolean value that indicates whether the passed element represents a match
@inlinable public func contains(where predicate: (Element) throws -> Bool) rethrows -> Bool
you can using it like:
if allFriends.contains(where: { (leaderboardUser) -> Bool in
return leaderboardUser.uid == idYouWantToFind
}) {
}
or with sort version:
if allFriends.contains(where: { $0.uid == idYouWantToFind}) {
}
and if you make your LeaderboardUser comfort to Equatable protocol, you can use like this:
class LeaderboardUser: Equatable {
var uid: String = ""
var email: String = ""
static func == (lhs: LeaderboardUser, rhs: LeaderboardUser) -> Bool {
return lhs.uid == rhs.uid
}
}
if allFriends.contains(userWantToFind) {
}
Upvotes: 1
Reputation: 16381
You just need to use contains(where:
method here.
if self.allFriends.contains(where: { $0.uid == uidOfLeaderBoard }) {
//...
}
Upvotes: 1