2016-12-20 67 views
0

我想用三個條件來篩選火力地堡查詢結果:無法轉換int類型的值與預期的參數類型布爾

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

第一個條件(post.isglobal == datafilter)必須得到滿足。然後,我想在滿足其餘兩個條件中的任何一個時進一步過濾帖子。

上面的代碼返回一個錯誤:Binary operator == cannot be applied to operands of type NSNumber? and Int

任何幫助,將不勝感激。 謝謝!

編輯:dataFilter變量定義爲視圖控制器類中的一個全局變量:

var dataFilter = 0

+0

比較與'做'==,而不是與'=' –

+0

如果我將其更改爲'==',我會收到錯誤'Binary operator ==不能應用於NSNumber類型的操作數?和Int' – winston

+0

嘗試'(post?.isGlobal.intValue ?? 0)=='或者在訪問它的isGlobal intValue屬性之前打開你的可選文章 –

回答

1

您只需解開您可選的後並訪問其isGlobal的intValue財產,NSNumber的有返回的intValue財產一個Int,你可以比較你的dataFilter(Int)值。或者使用'??'無合併打開您的可選綁定,並同時提供默認值爲零的情況下。

所以,如果你解開你的帖子對象:

post.isGlobal.intValue == dataFilter 

或者使用可選的招標與無合併運算符:

(post?.isGlobal.intValue ?? 0) == dataFilter 
相關問題