winston
winston

Reputation: 3100

Cannot convert value of type Int to expected argument type Bool

I'm trying to filter Firebase query results with three conditions:

let posts = snapshot.childSnapshots.map {
                    Post(snapshot: $0)
                    }.reversed().filter {
                        post?.isGlobal == dataFilter && (self.dependents.contains($0.postedBy) || self.currentUser?.uid == $0.postedBy)
                }

The first condition (post.isglobal == datafilter) must be met. Then, I want to filter posts further when either one of the remaining two conditions are met.

The above code returns an error: Binary operator == cannot be applied to operands of type NSNumber? and Int

Any help would be greatly appreciated. Thanks!

EDIT: the dataFilter variable is defined as a global variable within the viewcontroller class:

var dataFilter = 0

Upvotes: 0

Views: 6111

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236340

You can just unwrap your optional post and access its isGlobal intValue property, NSNumber has an intValue property that returns an Int that you can compare against your dataFilter (Int) value. Or use '??' nil coalescing to unwrap your optional binding and at the same time provide a default value for it in case of nil.

So if you unwrap your post object:

post.isGlobal.intValue == dataFilter

Or using the optional biding with the nil coalescing operator:

(post?.isGlobal.intValue ?? 0) == dataFilter

Upvotes: 1

Related Questions