Reputation: 1041
I have an array of objects that are all of class PFObject
.
I have a subclass of PFObject
called GameCategory
where some of the PFObject
s in my array are GameCategory
.
I was wondering how to write a first/where statement that would check each object to see if it is of class GameCategory, then check for a matching ID.
Here is my non-working attempt to do this:
for id in Ids {
if let object = self.matchObjects.first(where: {
if let gameCategory = $0 as? GameCategory {
gameCategory.id == id
}
}) {
//object successfully cast as GameCategory, and the GameCategory id was a match
}
}
This code returns the error 'Missing return in a closure expected to return 'Bool'.
Upvotes: 1
Views: 503
Reputation: 1652
Your code is missing the return in that first(where:
closure and casting as GameCategory
(your cast is only existing in the scope of that closure). I wrote it a little bit different but it will achieve the same:
for id in Ids {
if let object = self.matchObjects.first(where: { ($0 as? GameCategory)?.id == id }) as? GameCategory {
//object cast as GameCategory and the id was a match
}
}
Upvotes: 1